Function Calls
The program executes the function statements by calling the function.We can pass the information to the called function in two ways :
- Call by Value : The following program demonstrates how the information is passed to the called function by passing the value of parameters. This program uses the function display_values to display the value of variables before and after the function call.
ex.
#include <iostream.h>
void display_values( int a, int b)
{
a = 100;
b = 200;
cout << "The values within the function are : "<< a << "and" << b << endl << endl;
}
void main()
{
int x = 1001;
int y = 2001;
cout << " Before Function Call " << endl;
cout << "The value of x and y is " << x << "and" << y << endl << endl;
display_values(x,y);
cout << " After function call " << endl;
cout << " The value of x and y is "<< x << "and" << y << endl;
}
The output is :
Before Function call
The value of x and y is 1001 and 2001
The values within the function are: 100 and 200
After function call
The value of x and y is 1001 and 2001
As you can see, the parameter values have been changed to 100 and 200 within the display-values functions. However, when the function ends, the values of the variables x and y within main() have not changed. Because when program pass a parameter to a function, C++ makes a copy of the parameter's valus and places the copy into a temporary memory location called stack. The function then uses the copy of the value. When the function ends, C++ discards the stack contents and any changes the function has made to the copy.
- Call by Reference : In Call-By-Value, the called function does not have access to the actual variables in the calling program and only works on the copies of the values. This mechanism is fine if the function does not need to alter the values of the original variables in the calling function. Sometimes, we need to change the value of variables in the calling function. For example, in the bubble sort program, we compare two adjacent elements in the list and interchange their values if the first element is greater than the second. If a function is used for bubble sort, then it should be able to alter the values of variables in the calling program, which is not possible if the call-by-value method is used.
Ex :
#include<iostream.h>
void display_values( int *a, int *b ) //creating pointers to type int
{
*a = 100;
*b = 200;
cout << "The values within the function are : "<< *a << "and" <<*b << endl << endl;
}
main (void)
{
int x = 1001;
int y = 2001;
cout << "Before function call " << endl;
cout << " The value of x and y is " << x << and << y << endl << endl;
display_values ( &x, &y );
cout << " After function call " << endl;
cout << " The value of x and y is " << x << "and" << y << endl;
}
The output is :
Before function call
The value of x and y is 1001 and 2001
The values within the function are: 100 and 200
After function call
The value of x and y is 100 and 200
This is all about calling the functions in different ways. In next post, we will discuss about the various types of functions and their usage.
Keywords: call by reference, call by value, Function

