Kay
Kay

Reputation: 21

Return statements, values - how are they used?

I'm a little confused about return statements in C. I am in a college intro class for C, and we've learnt functions, however I still don't really understand what return values are used for. What can you do with them? Where does the value go? For example,

#include <stdio.h>

int add(int, int);

int main()
{
    int x=0, y =0, z;
    // insert code here...
    printf("Enter a number: ");
    scanf("%d", &x);

    printf("Enter another number: ");
    scanf("%d", &y);

    z = add(x,y);
    printf("%d\n", z);

    return z;
}

int add(int x, int y)
{
    int sum;
    sum = x + y;
    return sum;
}

The sum only displays because of the printf function. What happens to the return z? Would you be able to somehow pass return values to other functions?

Upvotes: 2

Views: 424

Answers (6)

kevintodisco
kevintodisco

Reputation: 5181

Typically a return value is 'captured' by a variable in the calling function. In your case, main is the calling function, and add is the 'callee,' or the function being called. int z is what is capturing the return value.

Of course you can pass return values to other functions! Something like:

z = add(add(5,7), add(1,2));

will return 15! What's happening behind the scenes, without getting too complicated, is that the inner two add functions are evaluated first, and the return values are pushed on the stack for the outer add function to consume. The stack is a section of memory that a program uses to store temporary values.

In the case of main, it's standard that a return value of 0 means the program executed correctly, and a non-zero return value indicates an error. main is called by the operating system when the program starts.

Upvotes: 5

M.C.
M.C.

Reputation: 1745

The return value in the main() method is not important when trying to understand how functions work. You usually return 0 or -1 to indicating the state of your program. In the add function , you are returning an int to any other function calling the add method. In your case the main method is calling the add function and the add function returns an int back. You can call the add function from any other methods you declare and so you can re use functions. And thats one of the most important benefits of a function,that you can write the code once and use it multiple times. so you can do something like this

int addNumbers(int x,int y,int z)
{

int sum = 0;

sum = sum + add(y,x);
sum = sum + add(z,0); // or sum = add(z,sum) 

return sum;
}

And now you have two functions and one of them(addNumbers) is calling the other one(add). and you can call both of them from your main function.

Upvotes: 0

Ian R. O&#39;Brien
Ian R. O&#39;Brien

Reputation: 6920

The return value is what is passed back to the calling function. For example:

int theSum = add(2, 3); // theSum will equal 5 because the method add(int x, int y) will return 5.

Upvotes: 0

Jack
Jack

Reputation: 16724

In which case of main() function, it's return code to caller. Usually, 0-value meaning success and non-zero value on error.

It may be very useful in bash scripts e.g:

foo && baa

baa program just runs if foo program have exited with success.

Tipycall example is compiling libraries on UNIX:

./configure && make && make install

Upvotes: 0

MANG KANOR
MANG KANOR

Reputation: 44

return will return the output value. like for example when you use the add function, it will return the value of x+y thus the sum variable

Upvotes: 0

Inisheer
Inisheer

Reputation: 20794

Your "add' function returns an integer value. In your "main()" function, you are setting "z" to the result of that function... ie... the return value.

Upvotes: 0

Related Questions