Return statement
Itis used to return value to calling function.
It is used for exit from called function to calling function.
When it is executed it always returns 1.
Absence of return statement indicates no value is returned.Such functions are called void.
Next statement following return statement willnot be executed.
The return statement can be used in different ways-
return;
It returns always 1
return(expression);
example-
return(a+b+C)
if such a statement is executed then expression within parenthesis is first solved and result obtained is returned.
Multiple return statement within a function-
example-
if(a>b)
return a;
else
return b;
return(&p)-It returns address of the variable.
return(*p)-It returns value of variable through pointer.
return NULL-It return NULL value.
Write a program to show return statement in different ways.
#include<stdio.h>
#include<stdio.h>
main()
int pass(int);
int x,y;
clrscr();
printf("Enter value of x:");
scanf("%d",&x);
y=pass(x);
switch(y)
{
case 1:
printf("Returned value is %d",y);
break;
default:
printf("Cube of x is %d",y);
}
return NULL;
}
pass(a)
{
if(a==0)
return;
else
return(a*a*a)
}
Output
Enter value of x:5
Cube of x is 125
Itis used to return value to calling function.
It is used for exit from called function to calling function.
When it is executed it always returns 1.
Absence of return statement indicates no value is returned.Such functions are called void.
Next statement following return statement willnot be executed.
The return statement can be used in different ways-
return;
It returns always 1
return(expression);
example-
return(a+b+C)
if such a statement is executed then expression within parenthesis is first solved and result obtained is returned.
Multiple return statement within a function-
example-
if(a>b)
return a;
else
return b;
return(&p)-It returns address of the variable.
return(*p)-It returns value of variable through pointer.
return NULL-It return NULL value.
Write a program to show return statement in different ways.
#include<stdio.h>
#include<stdio.h>
main()
int pass(int);
int x,y;
clrscr();
printf("Enter value of x:");
scanf("%d",&x);
y=pass(x);
switch(y)
{
case 1:
printf("Returned value is %d",y);
break;
default:
printf("Cube of x is %d",y);
}
return NULL;
}
pass(a)
{
if(a==0)
return;
else
return(a*a*a)
}
Output
Enter value of x:5
Cube of x is 125
No comments:
Post a Comment