A function can also return values to the calling program. For such functions, the type of the value returned to the calling function should be mentioned before the function name in the definition. The general syntax of a function which returns value is:
type f1(list of arguments) {    statement( s);    return value; }
f1 denotes the name of the function and type denotes the type of the value returned by the function to the calling function. Type may be int, float, char etc. In this case, the return statement is followed by a value. A function with return value mayor may not have arguments.
We’ll be covering the following topics in this tutorial:
Functions without arguments and with return value
An example of a function with return value and no arguments is illustrated in the following program.
#include <iostream.h> int f1() { int x=5,y=17; int mult; mult = x* y; return mult; } void main() { int p; int f1(); p=f1(); cout<<"\n"; }
In the above program, no returns an int type value to the calling function. It is essential to give the type of the value being returned by the function before the function name in the definition. The prototype of the function is written as int no. The function is invoked in the main() function by simply writing the name of the function as no. Here the main() function is also returning an int type of value and hence return 0 is given as the last statement in the program.
Functions with arguments and with return value
Following is an example of a function which returns an int type of value.
int show(int a, int b) { int sum; sum = a + b; return sum; }
This function show (int a, int b) returns the value sum of int type to the calling program. Prototype for the function show () mentioned above is given in the main() function as follows :
There are two ways in which functions with arguments can be invoked as shown as follows:
int show(int a, int b) { int sum; sum = a + b; return sum; }
Call by Value
In this case, values from the calling function are passed on to the arguments in the function. The function mayor may not return values. For example,
void calc(int I, int m) { } void main() { void calc(int, int); int p,q; cin>>p; cin>>q; calc(p,q); }
In the above program segment, the values for p and q are read in the main() function and these values are passed to I and m in the function calc(). It is to be observed that the variables in the calling function are distinct from the variables in the called function. They are allocated distinct storage locations.
Even if the variable names are same in the calling function and the called function, the variables occupy distinct storage location. Only requirement is that there should be one to one correspondence between the type and the number of variables(arguments) in the calling and the called function. In the main() function, instead of variables for the arguments, the actual values may also be written as follows:
void main() { void calc(int, int); calc(10,15); } void calc(l, m) { }
In C language, there is no concept of call by reference. Hence instead, call by value is used. For instance, to swap two variables, pointers to the address of these variables are passed as values.
<strong>Example</strong> Illustrates call by value function. #include <iostream.h> void ml(int a, int b) { int c; a=a+l0; b=b-5; cout<<”\n Values of a and b in the function “; cout<<a<<” “<<b; } void main() { int 1=5,m=10; void ml(int, int); cout<<"Values of I and m before passing "; cout<<I<<" "<<m; ml(l,m); cout«"\n Values of I and m after passing "; cout<<I<<" "<<m; } <strong>Output</strong>: Values of I and m before passing 5 10 Values of a and b in the function 15 5 Values of I and m after passing 5 10
Example
Given a set of numbers, the program finds the highest common factor, using call by value function for greatest common divisor.
#include <iostream.h> #include <math.h> int r; int gcd(int a,int b) { if((a<=0) || (b<=0)) cout<<"Error"; else { while ((a % b)!=0) { r=a % b; a=b; b=r; } } return b; } void main() { int n,i,j,hf; int a[80]; int b[80]; cout<<"Please enter the number of integers "; cin>>n; cout<<"Read the Integers\n "; for(i=0;i<n;i++) { cin>>a[i] ; cout<<a[i]<<" "; } i=0; for(j=i+ 1;j<n;j++) { b[j]=gcd(a[i],a[j]); a[j]=b[j]; i=i+1; } hf=a[n-1]; cout<<"\nThe highest common factor of the given "; cout<<" numbers is : "<<hf<<"\n"; }
Call by Reference
For call by value functions, the value of the arguments in the calling function are passed on to the arguments in the called function. In the case of call by reference function, a function passes a reference as an argument to the called function. The address operator (&) is placed after the argument type in the function definition.
In the case of call by reference functions, original values of the arguments in the calling function may get altered if a modification of these values is made in the function. However in the case of call by value functions, the original values of the arguments in the calling function can be retained.
Example
The following program illustrates the use of call by reference function for swapping two variables.
#include <iostream.h> void swap2 (int& a, int& b) { int t = a; a=b; b = t; } void main() { int m=3, n=2; swap2 (m,n); cout << "m = " << m << " n = " << n; } <strong>Output</strong>: m=2, n=3
#include <iostream.h> void swapl (int a, int b) { int t = a; a=b; b = t; } void main() { int m=3, n=2; swapl (m,n); cout << "m = " << m << " n = " << n<<"\n"; } <strong>Output</strong>: m = 3 n = 2
Example
This program illustrates that call by value function is not suitable for swapping two variables.
Even though the function swapl is invoked to swap the arguments, the actual arguments are not swapped since they are passed by value