Jan 17

Till now,we come across that we cannot give multiple return statements and functions can return only one value at a time. This means if we do so in programming,then compiler must flash an error message?

But program will be compiled successfully.

We’ll understand this with the help of examples.

Example 1.

main()
{
int i=10,j=20;
fun(i,j);
printf(”i= %d j= “%d”,i,j);
}
fun(int p,int q)
{
p= 2*q;
q=2*p;
return(p);
return(q);
}
Output
i=10,j=20
Explanation
A call to fun() from main() sends 10 and 20 to variables p and q. In fun(), p and q are calculated and return(p) is executed,which sends the control back to main() along with the value of p. But this value is not collected in any variable in main() it just gets ignored. As a result i and j stands unchanged at 10 and 20 respectively. The statement return(q) never gets execued,since the previous return statement will not allow the control to reach there.

Example 2.

main()
{
int i=10,j=20,k;
k=fun(i,j);
printf(”k= %d,k);
}
fun(int p,int q)
{
int a.b;
a=p-q;
b=p+q;
return(a,b);
}

Output
k=30
Explanation
Whenever we are attempt to return more than one value through the return statement,the last value gets returned. Thus in this case the value of b,i.e. 30 gets returned. Had the return statement been return(b,a),the value of a would have been returned.

written by Shweta

Jan 17

If a function has to return a value to the calling function,it is done through the return statement. It may be possible that a function does not return any value; only the control is transferred to the calling function.

Syntax

return (expression);

  • you can pass any number of arguments to a function but can return only one value at a time.

Example

return(5);  ,   return (x*y);     valid

return(2,3);   return(x,y);        invalid

  • If a function does not return anything,void specifier is used in the function declaration.
  • All the function’s type is by default is int i.e. a function return an integer value,if no type specifier is used in the function declaration.

Example

square(int n);

int square (int n);

Both above will return an integer value.

void square(int n);

it will not return anything.

  • A function can have many return statements. This thing happens when some condition based returns are required.

Example

if((i%2)==0)

return(0);

else

return(1);

  • With the execution of return statement,the control is transferred to the calling function with the value associated with it.

written by Shweta

Jan 16

Declaration of a function or function prototype

Every function has its declaration and function definition. When we talk of declaration only,it means only the function name,its arguments and return type are specified and the function body or definition is not attached to it.

int square(int num)

{

int result;

result = num * num;

return(result);

}

void main()

{

int n,sq;

printf(”Enter a number to calculate square value \n”);

scanf(”%d”,&n);

sq= square(n);

}

In above program for calculating square of a given number,we have declared function square() before main() function,this means before coming to main() ,compiler knows about square(),as the compilation process starts with the first statement of any program. Now,suppose we reverse the sequence of functions in this program i.e. writing the main() function and later on writing the square() function ,what happens?

The C compiler will give an error.

Function prototype require that every function which is to be accessed should be declared in the calling function.

Program to calculate square of number using function prototype

#include<stdio.h>

void main()

{

int n,sq;

int square(int); /*function prototype*/

printf(”Enter a number\n”);

scanf(”%d”,&n);

sq=square(n);

printf(”Square of a number is %d “,sq);

}

/*square function*/

int square (int num)

{

int result;

result = num* num;

return(result);

}

Output

Enter a number

5

Square of the number is 25

Note

  • Function prototype requires that the function declaration must include the return type of function as well as number of arguments or parameter passed.
  • The variable name of arguments need not be declared in prototype.

written by Shweta