Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Behind the scenes of returning value from function c++

Tags:

c++

function

What is the behind the scenes of returning value from function in c++?

In my understanding whenever func. is called the returning address and stack frame ( with local variables , reveresed order of func. arguments and registers ) is pushed onto call stack.

But what happens when executing encounters return statemenet? e.g

int a( int b ){
   int c = b * 2;
   return c;
}

After encountering return statement, is value of C stored in EAX register, local variables are destroyed and stack frame is removed from call stack, and after that the value in EAX register is moved into "returning address" memory?

Or did i misunderstood this concept?

All help is highly appreciated. Thanks.

like image 778
Darlyn Avatar asked Sep 29 '17 15:09

Darlyn


People also ask

How does return work in a function in C?

A return statement ends the execution of a function, and returns control to the calling function. Execution resumes in the calling function at the point immediately following the call. A return statement can return a value to the calling function.

How do you get the value returned from a function?

To return a value from a function, you must include a return statement, followed by the value to be returned, before the function's end statement. If you do not include a return statement or if you do not specify a value after the keyword return, the value returned by the function is unpredictable.

What is the meaning of function should return a value in C?

When a function returns a value, the value is returned via a return statement to the caller of the function, after being implicitly converted to the return type of the function in which it is defined.

Where does the return value go in C?

Where does the returned value for 'main' function go? It is returned to the calling process. On a POSIX compliant systems if the calling parent process were a C program it could at least retrieve the lowest 8bit of its child's return value by calling wait() or waitpid() after the child ended.


1 Answers

BTW, the assembly language is processor dependent. The ARM processor does not have an EAX register.

Compilers may have a standard for passing parameters and returning parameters. The method for returning values from functions is dependent on the implementation (compiler). There is no standard across all compilers.

Unoptimized code
Compilers are designed to take advantage of processor registers.

If the return value fits into a single register, then a register will be used to return the value. Depends on the processor.

For larger objects / values, the compiler has two options: return the object in multiple registers or return a pointer to the value. The pointer can be as simple as an index into the stack or an address to where the value is.

Optimized Code
The compiler may replace your function with a simple processor instruction or even drop the code. In this case, there is no return value.

The compiler may evaluate your function to a constant and place the constant into the executable code; thus requiring no function calls or function returns.

The compiler may decide to inline your function. In this case, there is no return value, similar to an assignment statement. A temporary variable may be used to contain the value or another register.

Further information
For more detailed information, research "compiler theory". There is a nice book with a dragon on it ...

like image 115
Thomas Matthews Avatar answered Sep 21 '22 02:09

Thomas Matthews