C++ Functions

    A function is a block of code that is used to perform a specific. In C++ we have at least one function for every program which is main(). By default in C++, we have to make a main() function because the C++ compilers search for the main() function to start the execution.

    C++ Functions

    Every statement which has parenthesis “()” associated with them are known as functions, such as main(), strcat(), memcpy(), etc. these are the C++ built-in functions and defined to perform a specific task, but we can also make our own functions, give them name whatever we want and these functions will be known as user-defined functions. The main idea of creating a function to eliminate the redundancy of code in the program.

    Objectives of making Functions

    • It reduces the code redundancy, for instance, if we want a specific task to perform multiple times in the program so instead of writing the same code, again and again, we create a function of that code one time and use that function name to call the function anywhere in the program.
    • It makes the program modular, which keeps the code clean and easy to read and use.
    • It promotes the concept of abstraction, where without worrying about the internal working of the function we use it.

    Function Declaration

    In function declaration we declare the number of parameters function takes, data-types of parameters and return type of function. The function declaration is used to tell the compiler the properties of the function. It is optional in function declaration to define the body of the function; the body contains the logic of the function. But if we declare a function, we need to define its body. In general programming we do not use the function declaration so often we directly use the function definition, which declares and define the complete function. Function Declaration Syntax

    return_type function_name( parameter list );

    Function declaration Example

    // A function that takes two integers as parameters
    // and returns an integer
    
    int max(int, int);

    Defining A function

    When we define a function we define the complete function along with its body, even if we have declared the function we have to define it with its complete body so, in C++ programming we do not declare function separately and then define it, instead we simply define the function without a declaration. When we define a function, we take care of the following things:

    • Return Type of function
    • Function Name
    • Parameters
    • Function Body
    • Return Type: the return type of the function defines the data type returned by the function if we call that function, for instance, if we make a function addition() which add two numbers, so in return, we will get a number from the function, that’s why we define a numeric data type as a return type.
    • Function Name: This will be the name of the function, it helps to refer the function, so we could call the right function.
    • Parameters: Parameters are the values that pass along with the function. It is not necessary to have parameters for every function.
    • Function body: The function body contains the logic and code, that a function supposed to do.

    Function defining syntax:

    return_type function_name( parameter list )
        {
           Function Body
        }

    Example:

    #include <iostream>
    using namespace std;
    
    int addition(int a, int b) // return_type function_name (parameters)
        {
              int c;
              c= a+b;
              return c;
        }   
    
    int main()
    {
           return 0;     
    }

    Behind the code: In the above example, we have only defined the function, and there is no output because we are not using the function addition() yet. To use the function we need to call the function inside the main() function. Here the function return type is int, function_name is addition, it accepts two int types parameters a and b, and it returns the value c which is an integer type.

    Function Call

    Once we have defined the function along with its body, now we can use the function. To use the function we need to call it, by default the compiler only reads the main () function so to use our function we need to call it inside the main() function and to call the function we use the function name. Example Let’s take the above example of function definition and call it using function call:

    #include <iostream>
    using namespace std;
    
    int addition(int a, int b) // return_type function_name (parameters)
        {
               int c;
               c= a+b;
               return c;
        }   
    
    int main()
    {
          int sum1, sum2;
       
          sum1= addition(10,20);   //function calling
          sum2 = addition(40,40);   //function calling             
    
          cout<<"The value of sum1 is: "<<sum1;
          cout<<endl;
          cout<<"The value of sum2 is: "<<sum2;           
    
         return 0;    
    
    }

    Output:

    The value of sum1 is: 30
    The value of sum2 is: 80

    Output: In this example we called our addition() function twice sum1= addition(10,20); , sum2 = addition(40,40);. The function addition() can accepts two integer types values, so when we call the function we provided it integers values (10,20) and (40,40).

    Function Arguments or Parameters

    In function, using arguments or parameters depends upon the function requirement. The parameters defined along the function definition are known as formal parameters, and they work as a local variable to the function itself. The lifespan of the formal parameters is limited to the function call. The parameters which pass along the function calls are known as actual parameters. When we call a function there are two ways, we pass the arguments or parameters to the function:

    • Call by value
    • Call by reference

    1. Call by Value

    In this method, the copies of the actual parameter passed in the formal parameters so any changes occur in the formal parameters do not affect the actual ones

    2. Call by reference

    This method copies the reference of an argument into the formal parameter. Inside the function, the reference is used to access the actual argument used in the call. This means that changes made to the parameter affect the argument. People are also reading: