return Statement

Computer Education, Training & Tutorial Resources - ComputerEducationWorld.com
Home » Free Educations » Free Programming Training » C Programming » C For Beginners »

The return statement immediately ends the execution of a subprogram and returns control to the caller.

For a function of return type void, a return statement is not strictly necessary. If the end of such a function is reached without encountering a return statement, control is passed to the caller as if a return statement without an expression were encountered. In other words, an implicit return takes place upon completion of the final statement, and control automatically returns to the calling function.

In functions, a RETURN statement must contain an expression, which is evaluated when the RETURN statement is executed. The resulting value is assigned to the function identifier, which acts like a variable of the type specified in the RETURN clause.

Example :

#include < stdio.h>

void exchange(int a, int b);

void main()
{ /* WRONG CODE */
    int a, b;

    a = 5;
    b = 7;
    printf(”From main: a = %d, b = %d\n”, a, b);

    exchange(a, b);
    printf(”Back in main: “);
    printf(”a = %d, b = %d\n”, a, b);
}

void exchange(int a, int b)
{
    int temp;

    temp = a;
    a = b;
    b = temp;
    printf(” From function exchange: “);
    printf(”a = %d, b = %d\n”, a, b);
}


• • •
 



captcha PHP Script Free PHP captcha script free php
Website Design by WebWalas.com