Use of return statement in C programming

The return is a jump statement. When the return statement is executed in a function, then it terminates the execution of the function and returns control to its caller function.

Syntax:

return;
return expression;

How does the return statement work?

When a function call to another function, then execution of the calling function is paused. The control is transfer to the called function from the calling function. The return statement returns the control to the calling function (terminates the execution of the called function) and now execution of the calling function is resumed.

Let see an example,

In the below program, when the main function calls addNumbers, then “main” is paused and addNumbers starts.

The addNumbers calculate the sum of the number from 1 to 100 and when control reaches to the statement return sum; then terminate the execution of addNumbers and control is transferred back to the main function.

#include <stdio.h>

//Add 1 to 100
int addNumbers(void)
{
    int sum = 0;
    int mumber = 0;

    for(mumber = 1; mumber <= 100; ++mumber)
    {
        sum += mumber;
    }
    return sum;
}

//Main function
int main(int argc, char *argv[])
{
    int sum =0;

    //calling addNumbers
    sum = addNumbers();

    printf("%d\n", sum);

    return 0;
}

Output: 5050

 

Some important point related to the return statement in C

 

1.  A function may have any number of return statements.

You can see below the 1D dynamic array in which I am using return two times.

#include <stdio.h>
#include <stdlib.h>


int main(int argc, char *argv[])
{

    int *piBuffer = NULL; //pointer to integer

    int nBlock = 0; //Variable store number of block

    int iLoop = 0; //Variable for looping



    printf("\nEnter the number of block = ");

    scanf("%d",&nBlock); //Get input for number of block


    piBuffer = (int *)malloc(nBlock * sizeof(int));

    //Check memory validity
    if(piBuffer == NULL)
    {
        return 1;
    }

    //copy iLoop to each block of 1D Array
    for (iLoop =0 ; iLoop < nBlock ; iLoop++)
    {
        piBuffer[iLoop] = iLoop;
    }

    //Print the copy data
    for (iLoop =0 ; iLoop < nBlock ; iLoop++)
    {
        printf("\npcBuffer[%d] = %d\n", iLoop,piBuffer[iLoop]);
    }

    free(piBuffer); // free allocated memory

    return 0;
}

 

 

2. We must not use the return statement with an expression in a function whose return type is void.

#include <stdio.h>

void main(int argc, char *argv[])
{

    printf("Hello Aticleworld !\n\n");

    return 0;
}

 

Output: [Warning] ‘return’ with a value, in function returning void

 

3. We must use the return statement without an expression in a function whose return type is void.

#include <stdio.h>

void main(int argc, char *argv[])
{

    printf("Hello Aticleworld !\n\n");

    return;
}

 

4. According to C standard, If the expression has a type different from the return type of the function in which it appears, the value is converted as if by assignment to an object having the return type of the function. See the below example,

#include <stdio.h>

//Sample function has return type int
int Sample()
{

    float b = 4.5;

    return b;
}


int main(int argc, char *argv[])
{

    printf("Value of Sample = %d\n\n",Sample());

    return 0;
}

 

Output: Value of Sample  = 4

 

Recommended Articles for you: