If you like my posts please leave a comment.

Thursday, November 29, 2012

Returning from a function



Returning from a function:     

As invoking or calling a function is important, returning from a function is equally important. As it not only terminates the function’s execution but also passes the control back to the calling function. A function terminates when either a return statement is encountered or the last statement of the function is executed. return statement is used to terminate a function whether it returns a value or not.
            A function can have more than one return statements, but only one of them can get executed, because execution of the function terminates as soon as return statement is encountered.

Returning Values:

All functions except those, whose type is declared as void, must return a value. The data type of the value which is being returned by the return statement of the function should be same as that of return type, mentioned for the function in the function prototype. If the function is about to return an integer value then its return type should be mentioned as int similarly for the float, double, char etc.
Example:

/*Program demonstrating - function returning values (Concept used: Function overloading)*/

#include<iostream.h>
#include<conio.h>
int cal(int, float);
char cal(int);
float cal(float, float);
void cal(void);
//------------------------------
int cal(int a, float B)
{
return (a+B);
}
//--------------------------------
char cal(int a)
{
return (a);
}
//-------------------------------------
float cal(float A, float B)
{
return (A/B);
}
void cal(void)
{
cout<<"\n Call to a funtion whose return type is void\n\n";
return;
}
//----------------------------------------------
void main()
{
clrscr();
int a,ans1;
float A,B;
A=5.5, B=2.2;
a=5;
float ans2;
char ans3;
ans1=cal(a,B);
cout<<"call to int cal(int, float) = "<<ans1<<"\n";
ans2=cal(A,B);
cout<<"call to float cal(float, float) = "<<ans2<<"\n";
ans3=cal(65);
cout<<"call to char cal(int) = "<<ans3<<"\n";
cal();
getch();
}
Output:
 

Thus just now we have seen that, all functions do their assigned job(s), however some return value some don’t.

Returning (by) Reference:    

A function may also return a reference, which can be considered as an Alias. Consider the following Example:

//Function returning reference

#include<iostream.h>
#include<conio.h>
float & max(float &, float &);  //Function prototype
//-------------------------------------------------
float & max(float & a, float & b)//Funciton definition
{
      if(a>b)
      return a;
      else
      return b;

}
//----------------------------------------------------
void main()//main() function definition
{
clrscr();
float A,B;
A=7.5F,B=5.2F;
cout<<"\n Address of A = "<<&A<<" and the value of A = "<<A;
cout<<"\n Address of B = "<<&B<<" and the value of B = "<<B;
float &ans=max(A,B);
cout<<"\n Returned Address = "<<&ans<<" and the value is = "<<ans;
cout<<"\n\nThus: Maximum of "<<A<<" & "<<B<<" = "<<ans;

getch();
}
Output:
 
As you can see that, return type of above function max() is  float &, which means that the function returns a reference to a variable of type float. The function returns reference to either a or b instead of returning value. In the above program the address returned is same as that if variable A (0x8f3dfff2) as it is having the greater value i.e. 7.5. It is important to note: The function returning reference can appear on the left-hand side of the assignment operator, since it returns reference to a variable. Example:
max(A,B) = 100; Below you can see the modified program and the related output.

Example:

//Function returning reference

#include<iostream.h>
#include<conio.h>
float & max(float &, float &);  //Function prototype
//-------------------------------------------------
float & max(float & a, float & b)//Function definition
{
      if(a>b)
      return a;
      else
      return b;

}
//----------------------------------------------------
void main()//main() function definition
{
clrscr();
float A,B;
A=7.5F,B=5.2F;
cout<<"\n Address of A = "<<&A<<" and the value of A = "<<A;
cout<<"\n Address of B = "<<&B<<" and the value of B = "<<B;
float &ans=max(A,B);
cout<<"\n Returned Address = "<<&ans<<" and the value is = "<<ans;
cout<<"\n\nThus: Maximum of "<<A<<" & "<<B<<" = "<<ans;

max(A,B) = 100;
cout<<"Variable with maximum value is assigned with 100 ";
cout<<"\nA = "<<A;
cout<<"\nB = "<<B;

getch();
}
Output:
 
 

Function Returning Pointers

The way a function can return an int, a float, a double, a char or reference etc, it can return a pointer. The function declaration must be explicitly mentioned. The general form of a function prototype returning a pointer: type * function_name(argument_list);

     The return type of the function returning a pointer must be known to the compiler prior to the invocation of the function because the pointer arithmetic is always relative to the base type of the variable whose pointer is to be returned and thus compiler must know what type of data the pointer is pointing to in order to point to the data item contained in that variable. Follow the example given below to in detail in order to understand this fact more.
Example:


//Function returning pointer

#include<iostream.h>
#include<conio.h>
float * max(float &, float &);  //Function prototype
//-------------------------------------------------
float * max(float & a, float & b)//Function definition
{
      if(a>b)
      return (&a);
      else
      return (&b);

}
//----------------------------------------------------
void main()//main() function definition
{
clrscr();
float A,B;
A=7.5F,B=5.2F;
cout<<"\n Address of A = "<<&A<<" (A)and the value of A = "<<A;
cout<<"\n Address of B = "<<&B<<" (B) and the value of B = "<<B;
float *ans=max(A,B);
cout<<"\n Address of pointer variable (*ans) = "<<&ans<<"\n";
cout<<"-------------------------------------------------------\n";
cout<<"\n Returned Address = "<<ans<<" and the value is = "<<*ans;
cout<<"\n\nThus: Maximum of "<<A<<" & "<<B<<" = "<<*ans<<" At the address = "<<ans<<" and stored at "<<&ans<<" (Addrss of pointer variable *ans)";

getch();
}
 Output:

No comments:

Post a Comment