C++ Calling a Function and Return Statement

Calling a function in C++ programming language

After writing a function in C++, we should call this function to perform the task defined inside function body. We cannot execute the code defined inside function's body unless we call it from another function.

When a function X calls another function Y, program control is transferred to function Y. Function Y performs specific task defined in function's body and when function Y terminates either by return statement or when the last statement of function body executes, program control returns back to the calling function X.

Syntax to Call a Function

function_name(argument1 ,argument2 ,...);

We can call a C++ function just by passing the required parameters along with function name. If function returns a value, then we can store returned value in a variable of same data type.

For Example :
int sum = findSum(2, 3);

Above statement will call a function named findSum and pass 2 and 3 as a parameter. It also stores the return value of findSum function in variable sum.


C++ Example Program to call a Function

#include <iostream>
using namespace std;
 
// Function Defination
int getAreaOfSquare(int side){
   // local variable declaration
   int area;
   area = side*side;
   // Return statement
   return area;
}
 
int main(){
   int side, area;

   cout << "Enter side of square\n";
   cin >> side;
   // Calling getAreaOfSquare function 
   area = getAreaOfSquare(side);
   cout << "Area of Square = " << area;

   return 0;
}

Output
Enter radius of circle
4
Area of Circle = 50.256
In above program, we defined a function "getAreaOfSquare" which takes side of a square as input parameter and return the area of the square. Inside main function, we first take side of square as input from user using cin and then call getAreaOfSquare function(by passing side of square) to get the area of square.

C++ Return Statement

The return statement in C++ language terminates the execution of a function and returns control to the calling function. A return statement can also return a single value to the calling function. The return type of a function is defined in function declaration before the name of the function. It is not necessary that function will always return a value. If a function does not return a value, the return type in the function declaration and definition is defined as void.
If no return statement is found in a function definition, then control automatically goes to the calling function after the last statement of the function body is executed.

Syntax of return Statement in C++

return expression;

Expression is optional. If present, is evaluated and then converted to the return data type specified in function declaration.


Passing Arguments to a Functionin C++

A function in C++ may needs data from calling function to perform it's task. It accepts data as function arguments which is passed by calling function. These variables to receive data from calling function are called the formal parameters of the function. A function can be called by passing zero or more parameters as per function declaration.

In C++, parameter(arguments) refers to data which is passed to function while calling function. The formal parameters are similar to local variables inside the function scope and are created when control enters into the function and gets destroyed upon exit.

A Function in C++ can be called in two ways.
  • Call by value
  • Call by reference

First of all we should understand the meaning of actual and formal parameters.

  • Formal parameter : This is the argument which is used inside body of function definition. It's is limited to the scope of function, it is visible to statements in function's body only. It gets created when control enters function and gets destroyed when control exits from function.
  • Actual parameter : This is the argument which is used while calling a function.
  • A function can have many arguments of same or different data types.
  • The data type of the actual parameter must match with the data type of formal parameter.
  • The count of formal parameter and actual parameter must be same.

Call by value

In call by value method a copy of actual arguments is passed into formal arguments in the function definition. Any change in the formal parameters of the function have no effect on the value of actual argument. Call by value is the default method of passing parameters in C++. Different memories are allocated for the formal and actual parameters.

C++ Program to Pass Arguments to Function as Call by Value
#include <iostream>
using namespace std;
  
void getDoubleValue(int F){
   F = F*2;
   cout << "F(Formal Parameter) = " << F;
}
 
int main(){
   int A;
   cout << "Enter a number\n";
   cin >> A;
   // Calling function using call by value
   getDoubleValue(A);
   /* Any change in the value of formal parameter(F)
   will not effect the value of actual parameter(A) */
   cout << "\nA(Actual Parameter) = " << A;

   return 0;
}
Output
Enter a number
5
F(Formal Parameter) = 10
A(Actual Parameter) = 5

Above program shows that, their is no change in the value of A(Actual parameter) even if the value of F(Formal parameter) changed inside function.


Call by reference

In call by reference method the address of the variable is passed to the formal arguments of a function. Any change in the formal parameters of the function will effect the value of actual argument. Same memory location is accessed by the formal and actual parameters.

C++ Program to Pass Arguments to Function as Call by Reference
#include <iostream>
using namespace std;
  
void getIncrementValue(int *ptr){
   *ptr = *ptr + 1;
   cout << "F(Formal Parameter) = " << *ptr;
}
 
int main(){
   int A;
   cout << "Enter a number\n";
   cin >> A;
   // Calling function using call by reference
   // by passing address of A
   getIncrementValue(&A);
   /* Any change in the value of formal parameter(F)
   will effect the value of actual parameter(A) */
   cout << "\nA(Actual Parameter) = " << A;

   return 0;
}
Output
Enter a number
5
F(Formal Parameter) = 6
A(Actual Parameter) = 6
In above program, we passed the address of variable A to function getIncrementValue, which incremented the value stored at location pointed by pointer ptr. Here, any change in formal parameters of function will change the value of actual parameter also because both are pointing to same memory location.

Programming Default Parameters (Arguments) in C++

C++ programming language provides flexibility to specify a default value for function arguments. We have to specify the default value of the function arguments during function declaration.

  • It is not compulsory to specify default values for all arguments, but if you provide default value for an argument X, then You must provide default values for each argument after X otherwise program will not compile.
  • A function with three parameters may be called with only two parameters. For this, the function declaration must include a default value for third(last) parameter, which will be used by the function when calling function passes fewer arguments.
For Example :
int getSum(int A, int B = 10){
    return A + B;
}

Calling function can call getSum function by either passing one or two parameters.

int sum = getSum(5); // Calling by passing only one argument.

Here, calling function is only passing value for formal parameter A(which is 5) and parameter B will take default value which is 10. Hence, the value of sum will be 15.

int sum = getSum(5, 6); // Calling by passing two argument.

Here, calling function is passing value for both parameters A and B. Both A and B will contain the actual parameters passed by the calling. In this case function will not use the default value for argument B. Hence, the value of sum will be 11.

int sum = getSum(5, 6); // Calling by passing two argument.
Points To Remember
  • If a value for that parameter is not passed when the function is called, the default given specified for that parameter will be used.
  • If a value of a parameter is passed will calling a function then the default value is ignored and the passed value is used.

C++ Function Default Argument Example Program

#include <iostream>
using namespace std;

// Default value for B is 10 
int getsum(int A, int B=10) {
   return A + B;
}

int main () {
   int sum;
 
   // Passing values for both arguments, 
   // B will not take the default value
   sum = getsum(5, 6);
   cout << "Sum = " << sum << endl;

   // Passing value only for one parameter(A)
   // B will take the default of 10 
   sum = getsum(5);
   cout << "Sum = " << sum << endl;
 
   return 0;
}

Output
Sum = 11
Sum = 15
In above program, we defined a function called getSum with two input parameters A and B. We have assigned default value of 10 to parameter B. In main, we first call getSum function with two arguments as getsum(5, 6);. In this case, formal parameters A and B takes 5 and 6 respectively. In second call to getSum function, we pass only one argument getsum(5);. In this case, formal parameter A becomes 5 and B takes default value which is 10.