Friday, July 29, 2016

C Basics - The return Statement in C

The return statement terminates the execution of a function and returns control to the calling function. A return statement can also return a value to the calling function.

The syntax for a return statement is,

return expression;

The value of expression, if present, is returned to the calling function. If expression is omitted, the return value of the function is undefined. The expression, if present, is evaluated and then converted to the type returned by the function.

If no return statement appears in a function definition, control automatically returns to the calling function after the last statement of the called function is executed. In this case, the return value of the called function is undefined.

#include <stdio.h>
 
int num1=0,num2=0;
void receive(void){
 printf("\nEnter a number:");
 scanf("%d",&num1);
 printf("\nEnter another number:");
 scanf("%d",&num2);
}
long square(int x, int y){
 return (x*y);
} 
long sum(int x, int y){
 return x+y;
} 
void diff(int x,int y){
 int z = 0;
 if(num1 >num2)
  z = num1-num2;
 else
  z=num2-num1;
 printf("\nDifference between numbers:%d",z);
 return;
}
int main( ) {
 long z = 0;
 receive();
 z = sum(num1,num2);
 printf("\nSum of two numbers: %ld",z);
 diff(num1,num2);
 z = square(num1,num2);
 printf("\nSquare of two numbers: %ld",z);
 
 
 return 0;
}

Output of above program,

Enter another number:5

Sum of two numbers: 9
Difference between numbers:1
Square of two numbers: 20



Related topics:
Loops in C   |   The for Loop in C   |   The while Loop in C   |   The do-while Loop in C   |   Nested Loop in C   |   Infinite Loop in C   |   Loop Control Statements in C   |   The break Statement in C   |   The continue Statement in C   |   The goto Statement in C

List of topics: C Programming

No comments:

Post a Comment