Function in C++ Programming
Default Arguments
When declaring a function we can specify a default value for each of the last parameters which are called default arguments. This value will be used if the corresponding argument is left blank when calling to the function. To do that, we simply have to use the assignment operator and a value for the arguments in the function declaration. If a value for that parameter is not passed when the function is called, the default value is used, but if a value is specified this default value is ignored and the passed value is used instead.For example:
// default values in functions
using namespace std;
int divide (int a, int b=2)
{
int r; r=a/b;
return (r);
}
int main ()
{
cout << divide (12);
cout << endl;
cout << divide (20,4);
return 0;
}
As we can see in the body of the program there are two calls to function divide. In the first one:
divide (12)
We have only specified one argument, but the function divide allows up to two. So the function divide has assumed that the second parameter is 2 since that is what we have specified to happen if this parameter was not passed (notice the function declaration, which finishes with int b=2, not just int b). Therefore the result of this function call is 6 (12/2).
In the second call: divide (20,4) There are two parameters, so the default value for b (int b=2) is ignored and b takes the value passed as argument, that is 4, making the result returned equal to 5 (20/4).
Inline Functions
Function call is a costly operation. During the function call it’s execution our system takes overheads like: Saving the values of registers, Saving the return address, Pushing arguments in the stack, Jumping to the called function, Loading registers with new values, Returning to the calling function, and reloading the registers with previously stored values. For large functions this overhead is negligible but for small function taking such large overhead is not justifiable. To solve this problem concept of inline function is introduced in c++.The functions which are expanded inline by the compiler each time it’s call is appeared instead of jumping to the called function as usual is called inline function. This does not change the behavior of a function itself, but is used to suggest to the compiler that the code generated by the function body is inserted at each point the function is called, instead of being inserted only once and perform a regular call to it, which generally involves some additional overhead in running time. Example:
#include<isostream.h>
#include<conio.h>
inline void sum(int a int b)
{
int s;
s= a+b;
cout<<”Sum=”<<s<<end1;
}Void main()
{
clrscr();
int x, y;
cout<<”Enter two numbers”<
sum(x,y);
getch();
}
Here at the time of function call instead of jumping to the called function, function call statement is replaced by the body of the function. So there is no function call overhead.
Overloaded functions
In C++ two different functions can have the same name if their parameter types or number are different. That means that you can give the same name to more than one function if they have either a different number of parameters or different types in their parameters. This is called function overloading. For example:// overloaded function
#include
int mul (int a, int b)
{
return (a*b);
}
float mul (float a, float b)
{
return (a*b);
}
int main ()
{
int x=5,y=2;
float n=5.0,m=2.0;
cout << mul (x,y);
cout << "\n";
cout << mul(n,m);
cout << "\n";
return 0;
}
In the first call to “mul” the two arguments passed are of type int, therefore, the function with the first prototype is called; This function returns the result of multiplying both parameters. While the second call passes two arguments of type float, so the function with the second prototype is called. Thus behavior of a call to mul depends on the type of the arguments passed because the function has been overloaded.
Notice that a function cannot be overloaded only by its return type. At least one of its parameters must have a different type.
Arguments passed by value and by reference
In case of pass by value, Copies of the arguments are passed to the function not the variables themselves. For example, suppose that we called our function addition using the following code: // function example#include
int addition (int a, int b)
{
int r; r=a+b;
return (r);
}
int main ()
{
int z;
int x=5, y=3;
z = addition (x,y);
cout << "The result is " << z;
return 0; }
What we did in this case was to call to function addition passing the values of x and y, i.e. 5 and 3 respectively, but not the variables x and y themselves.
When the function addition is called, the value of its local variables a and b become 5 and 3 respectively, but any modification to either a or b within the function addition will not have any effect in the values of x and y outside it, because variables x and y were not themselves passed to the function, but only copies of their values at the moment the function was called.
In case of pass by reference, Address of the variables (variable itself) not copies of the arguments are passed to the function. For example, suppose that we called our function addition using the following code: // function example
int addition (int &a, int &b)
{
int r; r=a+b;
return (r);
}
int main ()
{
int z;
int x=5, y=3;
z = addition (x,y);
cout << "The result is " << z;
return 0;
}
What we did in this case was to call to function addition passing the variables x and y themselves (not values 5 and 3) respectively.
When the function addition is called, the value of its local variables a and b points to the same memory location respectively, therefore any modification to either a or b within the function addition will also have effect in the values of x and y outside it.
Return by Reference
If we return address (refrence) rather than value from a function then it is called return by reference. #includeint& min(int &x, int &y)
{
if(x
void main()
{
clrscr();
int a,b,r;
cout<<”Enter two numbers”<
min(a,b)=0; cout<<”a=”<
Here min function return the reference of the variable which is smaller between a and b and the statement “min(a.b)=0” makes the value of the variable zero which is smaller.
Post A Comment:
0 comments so far,add yours