In this C++ Tutorial you will learn about the Concept of Function with Arguments – What is an argument, Declaring a function, Function Definition and Calling the function.
Previous chapters of this tutorial have discussed simple function definition writing, declaration of function and function call. In this chapter, the definition, declaration and call function with arguments will be addressed.
What is an argument?
An argument is the value that is passed from the program to the function call. This can also be considered as input to the function from the program from which it is called.
How to declare a function passed with argument
Declaring a function:
The general format for declaring the function remains the same as before except the data type passed as arguments in functions are in the same order in which it is defined in function.
The format for declaring a function with arguments is:
return_datatype function_name(datatype1,datatype2,..);
In this example, the data types are the types of data passed in the function definition as arguments to the function. You must take care to mention the number of arguments and the order of arguments in the same way as in function definition.
For example, suppose a function named exforsys takes two integer values as arguments in its functions definition and returns an integer value. The function declaration of exforsys would be written:
int exforsys(int,int);
Function Definition:
The function definition has the same syntax as the function definition previously defined, but with added arguments. The general format for defining a function with arguments is written as:
return_datatype function_name(datatype1 variable1,datatype2 variable2,..)
{
...
Program statements
...
return( variable);
}
In the above example, the return data type defines the data type of the value returned by the function. The arguments are passed inside the function name after parentheses with the data type and the variable of each argument. You must take care to mention the number of arguments and the order of arguments in the same way as in function declaration.
For example, if the function exforsys takes two integer values x and y and adds them and returns the value z the function definition would be defined as follows:
int exforsys(int x,int y)
{
int z;
z=x+y;
return(z);
}
In the above program segment, a return statement takes the general format
return(variable) ;
This value specified in the return as argument would be returned to the calling program. In this example, the value returned is z, which is an integer value, the data type returned by the function exforsys is mentioned as int.
Calling the function:
The calling of function takes the same syntax as the name of the function but with value for the arguments passed. The function call is made in the calling program and this is where the value of arguments or the input to the function definition is given.
The general format for calling the function with arguments is
function_name(value1,value2,…);
In the above exforsys function suppose integer value 5 and 6 are passed, the function call would be as follows:
exforsys(5,6);
As soon as the function call exforsys is made the control, transfers to the function definition and the assignment of 5 to x and 6 to y are made as below.
int exforsys(int x,int y)
exforsys(5,6);
It is possible to store the return value of the function in a variable. In the above example, the assignment of the function call to integer variable b can be produced as follows:
int b;
b = exforsys(5,6);
int exforsys(int x,int y)
{
int z;
z=x+y;
return(z);
}
The above statement assigns the returned value of the function exforsys. The value z is then added to the value of x and y to the variable b. So, variable b takes the value 11.
Let us see the whole program to understand in brief the concept of function with arguments
#include <iostream>
using namespace std;
int exforsys(int,int);
void main()
{
int b;
int s=5,u=6;
b=exforsys(s,u);
cout << "n The Output is:" << b;
}
int exforsys(int x,int y)
{
int z;
z=x+y;
return(z);
}