Pages

Wednesday, December 16, 2009

Return value

  • What is return value?


The value or output returned by a function is called return value. There are two ways a function can work, first is by printing the result itself within its own body and second is by returning the result to the calling function. Those functions which return a value are called functions with return value.

  • Types of functions on the basis of return value

As discussed in previous posts functions can be categorized according to different criteria like on the basis of declaration the functions are divided as library functions and user defined functions. Another criteria is on the basis of arguments, again we have two varieties, function with arguments and functions without arguments. Similarly we can divide functions on the basis of return value also.

a) functions without return value: functions which do not return a value are called functions without return value. Such functions work by printing the final output themselves. functions like printf(),scanf(),clrscr() are functions without a return value.

b) functions with return value: Although functions without return value are quite capable, there may be some conditions where a return value will be needed. In such a scenario we will have to create a function with return value. Such functions instead of managing the output themselves return the output to the calling function, letting the calling function decide what to do with the output. The calling function can print the value as it is or it can use this value for further processing. Library functions like strcmp(), strlen() are some of the examples of this type.

  • Why do we need return value functions?

Whenever a function is called it is not necessary that the output the function has achieved is the final result. Sometime it can happen that the output is an “intermediate” output and not the final output. Such intermediate output can be sent back to the calling function for final decision. The calling function will have the final say in deciding what to do with the value it has received from the called function. It can print the value as it is or can keep the value for any future processing.

  • How to distinguish between functions with and without return value?

It is quite easy to tell whether a function is returning value or not by looking at its declaration. All the functions which do not return a value are declared using the keyword “void” which means null or nothing. if a function starts with “void” then it is a kind of signal to the compiler that it won’t be returning any value. On the other hand functions that return a value do not start with void instead they have the datatype written in place of void. The datatype will obviously depend on the type of value the function is returning like int or float.



Syntax – functions without return type

void funname()


{


function body;


}



Syntax – functions with return value

datatype funname()


{


function body;


return value;


}

Note that the first declaration starts with the keyword “void” which means that function will not return a value.





Example 1 : Basic return value program

void main()


{


int show(int);


int a,b;


printf(“\nEnter a value “);


scanf(“%d”,&a);


b=show(a);


printf(“\nreturned value is %d”,b);


}


int show(int x)


{


x+=10;


return x;


}



Output

Enter a value 20


returned value is 30



Description

The first line declares a function using the statement “int show(int”). the “int” before show() denotes that the function will return an integer value while the “int” written as argument denotes that the function will take an integer as input. We can also say that the function show will take an integer as input and will return another integer as output. Since the function is declared with an integer value we must declare an extra variable that will handle the returned value. The statement “b=show (a)” denotes that the function will be passed the value of “a” as argument and the value returned by the function will be stored in the variable “b”. When the function is called the value of “a” is passed to “x”, show() increments the value by 10 and returns the incremented value to the calling function which is main(). The returned value (30) is stored in b and this value is then printed.

Example 2 : Program to print cube of the square of a number.




In the following program we want the user to enter a number and the program will calculate the cube of the square of the number. For example if the user enters 2 the value 64 will be printed since 64 is the cube of 4 which is the square of 2.

No. -->  Square --> Cube of square

2 --> 4 -->  64



void main()


{


int square(int);


void cube(int);


int num,sq;


printf(“\nEnter any numeric value “);


scanf(“%d”,&num);


sq=square(num);


cube(sq);


sq=square(4);


printf(“\nSquare of 4 is %d”,sq);


}


int square(int n)


{


int s=n*n;


return (s);


}


void square(int n)


{


int c=n*n*n;


printf(“\nSquare is %d”,c);


}



Output

Enter any numeric value 2


Square is 64


Square is 16



Description

The value entered by the user is passed as an argument to the function square. Square calculates the square of that number but instead of printing the result returns it to main. main() stores this value in the variable “sq”. Since this is not the final output it is sent to cube() to calculate cube. The second time when square is called we don’t want to print the cube of its square. Therefore when square returns the square of 4 it is again stored in sq and printed as it is.



Example 3 : returning a character value.



void main()


{


char showgrade(int);


char gr;


int salary;


printf(“\nEnter your basic salary “);


scanf(“%d”,&salary);


gr=showgrade(salary);


printf(“\nGrade according to the salary is %c”,gr);


}


char showgrade(int sal)


{


char g;


if(sal>=1 && sal<1000)


g=’D’;


else if(sal>=1000 && sal<5000)


g=’C’;


else if(sal>=5000 && sal<10000)


g=’B’;


else if (sal>=10000)


g=’A’;


return (g);


}



Output

Enter your basic salary 4500


Grade according to the salary is B

Description

The function prototype “char showgrade(int)” signifies that the function will return a char value while the argument is of integer type. When the salary is passed as argument the showgrade() will return the grade of the employee as per the salary entered. The returned value is copied to the variable “gr” in main where it is printed. You can also call the function by a statement like “showgrade(2300)” since 2300 is also an integer value.



Example 4: returning a float value

void main()


{


float bankloan(char);


char type;


float interest;


printf(“\nH. Home Loan”);


printf(“\nE. Education Loan”);


printf(“\nP. Personal Loan”);


printf(“\nSelect a loan type (H,E,P) : “);


scanf(“%c”,&type);


interest=bankloan(type);


if (interest==0)


printf(“\nInvalid loan type selected”);


else


printf(“\ninterest rate of this category is %f”,interest);


}


float bankloan(char ty)


{


float roi; // rate of interest


if (ty==’H’  || ty==’h’)


roi=8.5;


else if (ty==’E’  || ty==’e’)


roi=11.25;


else if (ty==’P’  || ty==’p’)


roi=12.75;


else


return 0;


return roi;


}

Output

H. Home Loan


E. Education Loan


P. Personal Loan


Select a loan type (H,E,P) : P


Interest rate of this category is 12.75



Description

The above function has a return value of float while the input value of char. The user selects a type of loan from the given menu (H for home, E for education and P for personal). The function checks the loan type and accordingly returns the interest applicable on it. The return value is sent to the variable “interest” and then printed. if the user enters an invalid choice the function returns 0 and “Invalid loan type selected” message is printed.

0 comments:

Post a Comment