codescracker


c++

C++ Function Call



« Previous Tutorial Next Tutorial »


A function is called (or invoked, or executed) by providing the function name, followed by the parameters being sent enclosed in parentheses. For instance, to invoke a function whose prototype looks like :

float area(float, float);

The function call statement may look like as shown below :

area(x, y);

where x, y have to be float variables. The syntax of the function call is very similar to that of the declaration, except that the type specifiers are missing. Whenever a call statement is encountered, the control (program control) is transferred to the function, the statements in the function-body are executed, and then the control returns to the statement following the function call. Following program uses function prototyping, function definition and function calling :

C++ Function Calling Example

Following program print cube of a given number using a function. This program demonstrates function calling in C++:

/* C++ Function Calling or Calling a Function */

#include<iostream.h>
#include<conio.h>

float cube(float);

void main()
{
	clrscr();
	float x, y;
	cout<<"Enter number whose cube is to be calculated: \n";
	cin>>x;
	y = cube(x);	// function calling
	cout<<"\nThe cube of "<<x<<" is "<<y<<"\n";
	getch();
}

float cube(float a)
{
	float n;
	n = a * a * a;
	return n;
}

When the above C++ program is compile and executed, it will produce the following output:

c++ function calling

The above program first declares a function cube by providing its prototype. Then after accepting the number it invokes the function cube and assigns the return value of it to the variable y. As soon as the function call statement is encountered, the control gets transferred to function-body and all its statements are executed. With the execution of a return statement, the control returns to the statement immediately following the function call statement.

When a function, that expects some arguments, is invoked, its call statement must provide the argument values being sent and the number and type of argument values must be the same as defined in the function prototype. Also the order of argument values must be the same as defined in the function prototype. For instance, if a function volume expects two values : first of float type and the second as int type then its call statement must look like as:

volume(a, b) ;

where a must be a float value and b must be an int value.

A function, that does not expect any argument, is invoked by specifying empty parentheses. For instance, if a function message does not expect any argument, it will be invoked as :

message();

Function calls that return values need to have a place in main() for the returned value to reside, to be printed, or to be manipulated. Think of the function as holding out its hand to pass a value back to main(). Thus, main() must stretch out its hand (which may be a variable location) for this value to reside. Let's take another example, demonstrating function calling in C++

/* C++ Function Calling or Calling a Function */

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

void msg1(void);
int fun1(int);
int fun2(int);
void msg2(void);

void main()
{
	clrscr();
	msg1();		// function calling
	char ch;
	cout<<"\nPress y to continue...";
	cin>>ch;
	if(ch=='y' || ch=='Y')
	{
		int num;
		cout<<"\nEnter a number: ";
		cin>>num;
		cout<<"\nSquare of the number is: "<<fun1(num);		// function calling
		cout<<"\nCube of the number is: "<<fun2(num);		// function calling
		msg2();			// function calling
	}
	else
	{
		cout<<"\nexiting...Press a key...";
		getch();
		exit(1);
	}
	getch();
}

void msg1(void)
{
	cout<<"Welcome to codescracker.com\n";
	cout<<"This is C++ Function Calling Tutorial\n";
}

int fun1(int x)
{
	int temp;
	temp = x*x;
	return temp;
}

int fun2(int x)
{
	int temp;
	temp = x*x*x;
	return temp;
}

void msg2(void)
{
	cout<<"\n\nThank You..!!\n";
}

Here are two sample runs of the above C++ program:

c++ calling a function

Function Calling Method in C++

A function can be invoked in two manners :

C++ Call by Value

The call by value method copies the value of the actual parameters into the formal parameters, that is, the function creates its own copy of argument values and then uses them.

Tip - In call by value method, the changes are not reflected back to the original values.

The main benefit of call by value method is that you cannot alter the variables that are used to call the function because any change that occurs inside function is on the function's copy of the argument value. The original copy of the argument value remains intact.

C++ Call by Value Example

Following program illustrates the call by value method of function invoking. The parameters that appear in a function call statement are actual parameters. The parameters that appear in function definition are formal parameters.

/* C++ Function Calling - C++ Call by Value */

#include<iostream.h>
#include<conio.h>
int change(int);
void main()
{
	clrscr();
	int orig;
	cout<<"Enter a number: ";
	cin>>orig;
	cout<<"\nThe original value is: "<<orig<<"\n";
	cout<<"\nReturn value of function change() is: "<<change(orig)<<"\n";
	cout<<"\nThe value after function change() is over: "<<orig<<"\n";
	getch();
}
int change(int a)
{
	a = 20;         // the value of a is changed.
	return a;       // the changed value is returned.
}

When the above C++ program is compile and executed, it will produce the following output:

call by value c++

In above program, the value of the argument to change(), orig i.e., 5 (here, entered by the user) is copied onto parameter a so that a gets value 5. When the statement a = 20 takes place, the value of a is changed but not the value of orig. The orig has still the value 5.

Note - During call by value, any change in the formal parameter is not reflected back to actual parameter.

Remember that it is copy of the value of the argument that is passed to into the function. What occurs inside the function has no effect on the variable used in the function call.

C++ Call by Reference

In call by value method, the called function creates a new set of variables and copies the values of arguments into them. The function does not have access to the original variables (actual parameters) and can only work on the copies of values it created. Passing arguments by value is useful when the original values are not to be modified. In fact, call by value method offers insurance that the function cannot harm the original value.

The call by reference method uses a different mechanism. In place of passing a value to the function being called, a reference to the original variable is passed. Remember that a reference is an alias (i.e., a different name) for a predefined variable. That is, the same variables value can be accessed by any of the two names : the original variable's name and the reference name.

Tip - In call by reference method, the changes are reflected back to the original values.

The call by reference method is useful in situations where the values of the original values are to be changed using a function.

C++ Call by Reference Example

Following program swap two values by call by reference method :

/* C++ Function Calling - C++ Call by Reference */

#include<iostream.h>
#include<conio.h>
void swap(int &, int &);
void main()
{
	clrscr();
	int a, b;
	cout<<"Enter any two number: ";
	cin>>a>>b;
	cout<<"\nThe original values are:\n";
	cout<<"a = "<<a<<"\tb = "<<b<<"\n";
	swap(a, b);
	cout<<"\nThe values after swap() are:\n";
	cout<<"a = "<<a<<"\tb = "<<b<<"\n";
	getch();
}
void swap(int &x, int &y)
{
	int temp;
	temp = x;
	x = y;
	y = temp;
	cout<<"\nThe swapped values are:\n";
	cout<<"a = "<<x<<"\tb = "<<y<<"\n";
}

When the above C++ program is compile and executed, it will produce the following output:

call by reference c++

In the above program, the function swap() creates reference x for the first incoming integer and the reference y for the second incoming integer. The original values will be worked with but by using the names x and y.


« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test