by Dinesh Thakur Category: Functions

In order to use a function in different parts of a program, the function must be called or invoked by another function. In C++, functions are called by specifying the name of the function, followed by the parentheses. The parentheses mayor may not contain a list of arguments depending on the function definition.

The syntax for invoking a function is

function_name (argl, arg2, ... , argN);

where,

function_name = the name of the function

argl, arg2, ... , argN = the list of arguments

The function that calls another function is known as the calling function and the function that is being called is known as the called function. When a function is called, the control immediately passes from the calling function to the called function. The called function then executes its body after which the control returns to the next statement in the calling function.

To understand the concept of invoking the function, consider this example.

Example : A program to demonstrate the invoking of a function

#include<iostream>

using namespace std;

void disp () ;

int main ()

{

Cout<< "Welcome to Calling function" <<endl;

disp ( ) ;    / / invoking disp ( )

cout<<"Back to Calling function"<<endl;

return 0;

}

void disp ()

{

cout<<"Welcome to Called function"<<endl;

}

The output of the program is

 

Welcome to calling function

Welcome to Called function

Back to calling function

 

In this example, the function disp()is invoked from the function main ().Thus, the function main () is the calling function and the function disp () is the called function.

Generally, the calling function passes information to the called function through arguments. The argument(s) that appear in the function call are known as actual arguments and the argument(s) that appear in the function definition are known as formal arguments. The number of actual arguments, their order and type in the function call must match with that of the formal arguments.

 

To understand the concept of actual and formal arguments, consider this example.

 

Example: A program to demonstrate the use of actual and formal argument

 

#include<iostream>

using namespace std;

void sum (int, int) ;

int main ()

{

int a,b;

cout<<"Enter the first no. :“;

cin>>a;

cout<<"Enter the second no.:”;

cin>>b;

sum(a,b) ;

return 0;

}

void sum(int x, int y)

{

Cout<<"The sum is : "<<(X+Y);

}

The output of the program is

 

Enter the first no. 25

Enter the second no. : 25

The sum is : 50

In this example, the function sum ( ) is invoked with two actual arguments, namely, a and b. The values of these arguments are passed to two formal arguments, namely, x and y. Note that the number of actual arguments and their data types are same as the formal arguments.





About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles