322 Programming and Data Structures
fUE.'/
f
c -
printf ( mn In fun OlBยป%d Cm%d*,b,c);
}
OUTPUT;
In main () B= 10 C=5
In fun 0 B = ll C=4
Again In main 0 B= 12 C=3
Explanation In the above program variables V and 'c' are defined outside the main ( ) , hence
they are global. They can be called by any other function. The same variables V and 'c! are used
throughout the program. In main (), values of V and 'c' are printed.
The function fun () is called, V is incremented and 'c' is decremented. The values of V and 'c'
are printed. The control then returns to the function main ().
Again 'b' is incremented and 'c' is decremented. The values of V and 'c' are printed. Here, both
the functions use the same variables.
g) Return value: It is the outcome of the function.The result obtained by the function is sent back
to the calling function through the return statement.The return statement returns one value per
call. The value returned is collected by the variable of the calling function.
10.4 THE RETURN STATEMENT
The user-defined function uses a return statement to return the value to the calling function. Exit
from the called function to the calling function is done by the use of a return statement. When a
return statement is executed it always returns 1.
10.5 Write a program to use a return statement in different ways.
# in clu d e<stdio.h>
# include cconio. h>
main()
{
in t pass (in t );
in t x ,y;
c lr s c r ();
printf("Enter value of x:"fc
scanf("%d",&x*
if(x==l I I x<0)
return 0;
y=pass(xfc
switch(y)
{
case 1
Functions 323
printf ("The value returned is %d",y)
break;
default
printf ('The cube of %d is: %d",x,y)
}
return NULL;
>
pass(a)
if (a==0)
return;
else
return (a*a*a)fc
I
OUTPUT:
Enter value of x : 5
The cube of 5 is :125
1) return 0: This statement returns zero to the operating system if the value entered by the user is
1 or negative.
2) return: The value of V is passed to function pa s s (). If the value is zero then the return statement
returns 1. The return statement is used to exit from the function pass ().
3) return (a*a*a): In a function pass (), when the if statement finds the value of 'x' as non-zero
then else block is executed and the return statement returns cube of the entered number.
4) return NULL: This statement returns NULL.
The return statement can be used in the following ways.
a) retum(expression);
Example
return (a+b+c);
If such a statement is executed, the expression within the parentheses is first solved and the result
obtained is returned.
b ) A function may use one or more return statements. It is used when we want to return a value
depending upon certain conditions.
Example
Table 10.2 Multiple return statements
The if statement
The switch() statement
Switch(X)
i f (a>b)
{
return (a); case 1:
else
return (1);
return (b);
break;
case 2
return (1*2);
break;
d e fau lt:
return (0);
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.164.141