9.4 – Returning a value from the function

by subbu on November 15, 2013

Returning a value from the function

In previous session, we have discoursed how we can send arguments to a function?  Now we will see how to get back a value from the function. Generally the control comes back to the calling function, once the function’s execution is completed.

return is the keyword used to interrupt the function execution and send the control back to the calling function.

#include<stdio.h>
int main()                  /* calling function */
{
  display();
}
display()
{
  printf("One");
  printf("\nTwo");
  return;                  /* termination point */
  printf("\nThree");
  printf("\nFour");
}

Output:
One
Two

Here the return statement sent the control back to the calling function after the execution of first two statements and before execution of last two statements in the function display().

The same return keyword is also used to return a value from the function. The return statement could only send single value while sending the control back to the calling function. A constant, variable or an expression can be written with the return statement, the result of which will be returned.

return 5;            /* returns 5 */
return x;            /* returns the value of x */
return 2*(x+y);  /* returns the result of expression */

L-value to the function call

The function calling statement is assigned to a variable (l-value) to assign a value that would be returned by the function. Here the l-value can never be a constant and expression but, only a variable.

sending arguments to a function

Constant with return statement

Example:

#include<stdio.h>
int main()
{
  int x;
  x=december();                      /* returned value assigns to x */
  printf("Last month %d",x);
  return 0;
}
december()
{
  return 12;                         /* returns 12 */
}

Output:
Last month 12
Example explained:
constant with return statement

Here the main() function is calling the sub function display(), which returns 12 to the calling function and assigning to the variable x (l-type).

Variable with return statement

Example:

#include<stdio.h>
int main()
{
  int x,y,z;
  x=20;
  y=30;
  z=adding(x,y);           /* sending 20, 30 as arguments */
  printf("Sum:%d",z);
  return 0;
}
adding(int p,int q)
{
  int r;
  r=p+q;
  return r;                 /* returning 50 */
}

Output:
Sum:50

Example explained:
variable with return statement

The values of x, y (actual arguments) are assigned to p, q (formal arguments). Sum of two numbers (r) is returned to the calling function and assigned to “z”. The value of “z” (50) is printed as sum.

Expression with return statement

Example:

#include<stdio.h>
int main()
{
  int x=20,y=30,z;
  z=adding(x,y);
  printf("Sum of two numbers %d",z);
  return 0;
}
adding(int p,int q)
{
  return p+q;
}

Output:
Sum of two numbers 50

Example explained:
expression with return statement

Unlike in the previous example, here the expression p+q is written with the return statement. The result of expression 50 is returned and assigned to the variable z.

Shadowing return statement:

Example:

#include<stdio.h>
int main()
{
  int x=10,y=20,z;
  z=manip(x,y);
  printf("%d",z);
  return 0;
}
manip(int x,int y)
{
  return x+y;   /* returns sum */
  return x-y;    /* will have not effect */
}

Output:
30

Example explained:

Function manip() has two return statements. The first return statement shadows the second return statement. The second return statement has no effect in the function.

It is optional to collect returned value:

Example:

#include<stdio.h>
int main()
{
  int x=10,y=20,z;
  addition(x,y);           /* no l-value */
  printf("Sum %d",z);
  return 0;
}
addition(int x,int y)
{
  return x+y;              /* returns sum */
}

Output:
Sum 5705472               //Garbage value

Example explained:

Here the sub function addition() is returning addition of two numbers 30 (x+y) but, not collecting at the calling statement because calling statement is not being assigned to l-value (variable).

There would be no error as we expect. The garbage value would be the output because z is not assigned with any value.

Previous post:

Next post: