The IoT Academy Blog

C++ Functions: Syntax, Types and Call Methods

  • Written By  

  • Published on August 25th, 2022

In C++, a function is a segment of code that performs a specific task. You can reuse it, which means do it more than once. Functions have a name and are helpful for reading, writing, and editing complex problems.

We shall talk about the idea of functions in this blog. Methods, subroutines, and procedures are other names for functions.
Table of Contents [show]

How are functions defined?


A function is a collection of statements that are combined to carry out a certain activity. These can be instructions that carry out routine chores or unique actions like printing.
Code can be made simpler by using functions to divide it into smaller, more manageable pieces. Using functions also save us from having to write the same code over and over again. We just write one function and then call it as needed without having to repeatedly write the same set of commands.

Why do we need functions?


Functions help us reduce code redundancy. If a function is executed in multiple places in the software, we create a function and call it everywhere instead of writing the same code again and again. This also helps with maintainability because if we make future functionality changes, we need to make the change in one place.
Functions make code modular. Imagine a large file with many lines of code. Code becomes easy to read and use when broken down into functions.
Functions provide abstraction. For instance, we don’t need to worry about the internal workings when using library functions.

Our Learners Also Read : Guide to 8 Best Web Development Languages 


Function types in c++

There are two types of functions in c++:
Standard library functions: Predefined in C++
User Defined Function: Created by users

User-defined function


User-defined functions are user/customer-defined blocks of code specially modified to reduce the complexity of large programs. They are sometimes referred to as “tailor-made functions,” developed to address the user’s issues while minimizing the program’s overall complexity.

Library functions


Library functions are also called “built-in functions”. These functions are part of a compiler package that is already defined and consists of a specific function with a unique and distinct meaning. Builtin Function gives us an advantage because we can directly use them without defining  them. In contrast, in user-defined functions, we have to declare and define the function before using them.
For example: sqrt(), setw(), strcat() etc.

Function declaration


A function declaration gives information to the compiler about the name, return type, and parameters of the function. The actual function body is provided by a function definition.

Syntax
“`
return_type function_name( parameter list );
“`
The C++ standard library contains various built-in functions that your program can call. For example, function strcat() to concatenate two strings, function memcpy() to copy one memory location to another, and many more functions.
A function is known by various names like a method, sub-routine, procedure, etc.

Defining a Function


The general form of a C++ function definition is as follows ?
Syntax
“`
return_type function_name( parameter list ) {
   body of the function
}
“`

The function body and function header make up a C++ function definition. The components of the function are listed below.
Returned type: Values can be returned type functions. The data type of the function’s value is indicated by the return type. Some functions carry out the necessary processes without giving a result. The return type keyword here is void.
Function Name: This is the function’s official name. The function signature is made up of the function name and argument list.
Parameters: Similar to a placeholder, parameter The parameter is passed a value when a function is called. It is known as an actual parameter or argument to refer to this value. The type, order, and quantity of a function’s parameters are described in the parameter list. The presence of parameters is optional; a function may not have any.
Function Body: The body of a function contains a set of statements that define what the process does.


Example


The following is the source code of a function called max(). This function takes two parameters num1 and num2, and returns the largest of the two?
“`
// function returning the max between two numbers
 
int max(int num1, int num2) {
   // local variable declaration
   int result;
 
   if (num1 > num2)
      result = num1;
   else
      result = num2;
 
   return result; 
}
“`

Calling A Function


When we have a function in a program, depending on the requirement, we need to call or invoke that function. When a function is called or invoked, it executes its set of instructions to provide the desired results.
The function can be called from anywhere in the program. It can be called from the prior function or any other function if the program uses more than one function. A function that calls another function is called a “calling function”.

For Example-
“`
#include
using namespace std;
 
// function declaration
int max(int num1, int num2);
 
int main () {
   // local variable declaration:
   int a = 100;
   int b = 200;
   int ret;
 
   // calling a function to get max value.
   ret = max(a, b);
   cout << “Max value is: ” << ret << endl;
 
   return 0;
}
 
// function returning the max between two numbers
int max(int num1, int num2) {
   // local variable declaration
   int result;
 
   if (num1 > num2)
      result = num1;
   else
      result = num2;
 
   return result; 
}
“`
Here, the max() function and the main() function are compiled into the source code. While running the final executable, it would produce the following result-
“`
Max value is: 200
“`



There are two most common methods to call functions in c++:

1. Call by Value: 

In this call method, you pass only a copy of the variable to the function, not the actual argument. When specifying variable or argument documents, any changes made to a variable in a function do not affect the actual argument.
For Example-
“`
void calc(int x);

int main()
{
    int x = 10;
    calc(x);
    printf(“%d”, x);
}

void calc(int x)
{
    x = x + 10 ;
    return x;
}
“`
Output
“`
20
“`

2. Call by Reference:

In this calling technique, you pass the address or reference of an argument, and the function receives the memory address of the argument. In this case, the value of the variable changes, or you can say it reflects the changes back to the actual variable.
For Example-
“`
void calc(int *p);

int main()
{
    int x = 10;
    calc(&x); // passing address of x as argument
    printf(“%d”, x);
}

void calc(int *p)
{
    *p = *p + 10;
}
“`
Output
“`
20
“`

Conclusion

After reading this blog on C++ functions, you will understand why you use functions in C++, what a function in C++, its syntax, and the types of functions in C++. You have also learned about function calling methods, i.e., call by value and call by reference, with the help of some examples.

About The Author:

logo

Digital Marketing Course

₹ 9,999/-Included 18% GST

Buy Course
  • Overview of Digital Marketing
  • SEO Basic Concepts
  • SMM and PPC Basics
  • Content and Email Marketing
  • Website Design
  • Free Certification

₹ 29,999/-Included 18% GST

Buy Course
  • Fundamentals of Digital Marketing
  • Core SEO, SMM, and SMO
  • Google Ads and Meta Ads
  • ORM & Content Marketing
  • 3 Month Internship
  • Free Certification
Trusted By
client icon trust pilot
1whatsapp