what does it mean to "return a value to its caller"?
I got that return(0) means your program has no errors, and that returning any other integer means there is an error, but what confuses me is returning variables. Can someone please explain to me the return statement so I can understand what returning variables does?
>>9050907
blah blah call stack blah blah blah
to /g/ with you.
>>9050907
Suppose you have function
int sum(int a,int b) {
return a+b;
}
Whenever you call it, your program executes a subprogram that does the computation and all your program does while that is going on is wait for the subprogram to communicate back with something.
For example if you run:
sum(3,5);
Your computer will execute that subprogram, which will return 8 to your main program, signaling to it that it can now continue.
But if you run
int c = sum(3,5);
Then your program executes the subprogram sum, this does the computation and returns 8 but now your program is designed to store that 8, perhaps to use it somewhere else. Again, this return signals your main program that is may continue now.
The return 0 from the main happens because your program is a sub-program of the operating system. Your entire program is technically just a function that your operating system executed and naturally whenever the operating system executes something it must wait for a response. The return 0 thing is pretty much arbitrary and if you put return -1 you will most likely see no error message. That said, -1 is a common error code programmers use so obviously this is bad design in an actual project. But this is all arbitrary, it is just that it was chosen decades ago. For all you want you can make your error code be "TITS"
>>9050907
When you create a value returning function like main or any other user-defined function, you promise to return a value. This is because, by definition, a value-returning function must return a value via the return statement. You don't actually have to return 0 for main, you could return 100 or 3848373, but some compilers take issue with this. The main purpose of the return statement is to tell the compiler the program is finished, but with value-returning user defined functions, it also allows us to break up a program into smaller pieces. By using return x; in a function, we can have the value returned be specifically determined at the time that function executes and be based on its parameters, etc.
Read more
then who was telephone?
You tell your friend "add 2+2". He does it, then throws away the paper without telling you the result.
The next day, you tell him "add 2+2 and tell me the answer", and he gives you the result.
>>9050907
You'd need to understand how the stack works. Returning the variable means copying it down to the location allocated for it, before moving the stack pointer down and thus "popping" the function and its temporary stuff you just called, off the top of the stack.
This is also the wrong way to think of it"
>I got that return(0) means your program has no errors
It can mean that in the context of an overall system, or it can not. It can mean whatever. It can be the X dimension of a jpeg image, it can be the number of files processed, whatever. It doesn't have a meaning, just checking for non-zero returns is a convenient convention for writing logic that deals with external program execution.
The call stack is just a range of memory addresses. Every address is a slot. It's just copying the value to the lower slot where it won't be overwritten by the next thing pushed onto the stack.
Read this site
http://www.learncpp.com/cpp-tutorial/14-a-first-look-at-functions/
This page specifically
does returning a variable return it to the beginning of the entire program, or just to the beginning of the subprogram its in?
>they live in 2017 but don't know how to code
This is figuratively like not being able to read in earlier ages.
Stay brainlet
>>9050907
Just pretend the function is like a variable. Whatever you return is what the function is equal to when you call it. (Of course it's not exactly like that but it'll help you start out.)
>>9051322
It just copies it to somewhere lower on the stack. Which is why you don't have dynamic storage on the stack, and have to request it to be allocated on the heap. Arbitrary storage is not deterministic, and therefore the compiler cannot know where to put things on the stack.
The "stack" is just a range of memory addresses.
Return statement is equivalent to "pop eip". There is literally nothing else going on.
c++ is a clusterfuck
Since c++14, return x and return (x) are two different things.
https://stackoverflow.com/a/25615981
You will never understand this language fully.
>>9051322
it returns to the exact place where it was called, this is accomplished by the stack pointer being saved first, so it becomes the last thing to get popped from a returning function call so you always end up where the function call happened and can resume the rest of the program.
>>9051670
you have callee saved registers which is where the compiler puts things
Anybody who wants to know about this, it's all explained here https://www.cs.cmu.edu/~213/schedule.html click on old video, and get the book.
>>9050907
usually you return 0 for no error so that you can do this
if(a = functioncall(b)){
// handle error here
}