Functions
Almost all programming languages have a construct called procedure/function.
A function is a group of statements which executes a specific task.
A C++ program is a group of functions. And the execution of the program starts with a function called main.
A function lives in its own stack – variables defined in a function are invisible to other functions.
To communicate with other functions, values are supplied to a function - these are called parameters. A function can have 0 or more parameters of different types.
A function can send back one value to the caller function. This is called return value of the function.
The type of this return value is the type of function and is specified in the function header.
Next let us look at syntax of a function
Syntax:
ret-type function-name(type parameter1,type par2, type par3)
{
......
body-of-function;
......
}
As shown above the function can have multiple parameters. Each of these parameters must be defined with its type followed by its name .
e.g.
int myfunction(int n1, float n2)
{
/********/
}
Here n1 and n2 are parameters to function myfunction. n1 is of type int and n2 is of type float.
A function must have a unique name in its scope. Function name, parameter names and local variables defined within the function must all be unique identifiers.
Let us look at some example functions.
In the above example the function name is sum. It has two integer parameters a and b. And the function returns an integer – hence type of function is integer.
The variable s declared inside function is visible only within the function. Even a and b are only visible within the function.
We see how the function is called in main(). p and q which are sent to function are called arguments. They are copied to a and b. The value of function call is return value. And this return value is displayed using cout.
void function
A function may not return anything. Such a function can be defined as void function.
void print_number(int num)
{
cout<<num;
}
return statement
A function can have one or more return statements. This statement terminates the function and returns a value back to caller. e.g. int product(int a, int b) { return a*b; } Can a void function use return statement at all?
A void function can still use a return statement if necessary. But this return will be empty return.
void print_number(int num)
{
if(num==0)
return;
cout<<num;
}
We are printing only non-zero numbers. So if the number is 0, we just terminate function.
Call by Value and Call by Reference
A function in C++ receives its parameters as a copies of original arguments. Parameters are stored in different locations than those of actual arguments. And because of this, the original values are unaffected after the function call. Any (one) value which needs to be sent back to caller, must be sent using return statement.
Let us look at an example.
#inlcude<iostream>
using namespace std;
void print_num(int a)
{
a++; cout<<"a in print_num function is "<<a;
}
int main()
{
int a = 12;
print_num(a);
cout<<"Now a in main is "<<a;
}
Now let us look at the output
a in print_num function is 13
Now a in main is 12
So a in print_num function is a different variable than a in main function. It does not matter whether you give the same name to parameter as that of argument or a different name. Even though in print_num a is incremented, the original value of a in main remains same.
Returning multiple values from function
What do we do if a function needs to return more than one value? Do we use multiple return statements in function?
No, that does not work. Because first return will terminate function and second return will never be executed.
The solution is to use pointers or references as parameters. Let us look at both the methods.
#include<iostream>
using namespace std;
void swap(int *ptra, int *ptrb)
{
int temp = *ptra;
*ptra = *ptrb;
*ptrb = temp;
}
int main()
{
int a=10,b=12;
swap(&a,&b);
cout<<"Now a is"<<a<<" b is "<<b<<endl;
}
Here the two parameters to swap function are pointers to integers. And we send two pointers - address of a and address of b as arguments. ptra has address of a and *ptra indirectly accesses a. So change to *ptra changes a. And change to *ptrb modifies b.
So the output is
Now a is 12 b is 10
But using pointer parameters is risky. We may crash the program if the pointer is not initialized or is NULL.
The other solution for multiple returns is using references.
Let us rewrite swap function using reference parameters.
#include<iostream>
using namespace std;
void swap(int &refa, int &refb)
{
int temp = refa; refa= refb; refb = temp;
}
int main()
{
int a=10,b=12;
swap( a, b);
cout<<"Now a is"<<a<<" b is "<<b<<endl;
}
swap function now takes two reference parameters - (int &). refa is an alias for a and refb is an alias for b. So when refa and refb are swapped in swap function, a and b are also changed in main. So
A reference parameter looks like a ordinary parameter, but behaves like a pointer parameter.
Assign a value to function?
No, I am not joking! You can in fact assign a value to function call - if the function returns a reference value.
Let us look at an example
#include<iostream>
using namespace std;
int& foo(int &a,int &b)
{
if(a>b) return a;
return b;
}
int main()
{
int p,q;
cin>>p>>q;
foo(p,q)=0;
cout<<"p is "<<p<<" q is "<<q<<endl;
}
The function foo() returns a reference to an integer - its return type is int&. In fact it returns a reference to the parameter which is larger.
In main() function, foo(p,q)=0 assigns 0 to the return value of the function. That is to say that the statement assigns 0 to the returned reference value.
So what will be the output of the program?
We clearly see that after the function call, larger of p and q is assigned to 0.
A weird way of computing? But some times you do need to assign a value to return value of a function. e.g. overloading subscript operator ([]) of an array object.
That is not the end of this discussion for functions! C++ has some more new features like overloaded functions and default arguments. We will see them in next page.