This blog is under construction

Saturday, 12 May 2012

return Statement

return statement is used to return program control from calling function to called function. The outcome from the called function will be sent to the calling function through the return statement.  Only one value can be returned using return statement. Value returned from the called function can be collected using a variable in calling function.  Below is the general form of return statement.

return return_value;

In case of void function, we can use return statement without return value as shown below.

return;

Example C program to illustrate return Statement:
 
  #include <stdio.h>
  int multiply(int, int);

  int main() {
        int a, b, res;
        printf("Enter two number to multiply:\n");
        scanf("%d%d", &a, &b);
        // return value of multiply() function is stored in variable res
        res = multiply(a, b);
        printf("Output: %d\n", res);
        return 0;
  }

  int multiply(int a, int b) {
        return (a * b);
  }

  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter two number to multiply:
  10 20
  Output: 200


No comments:

Post a Comment