I was reading about inline functions in C++ , and mostly what I understood was that the compiler will copy paste the function code which is inlined. If an inline function contains a return statement and the function is used in some other function will it cause the caller function to terminate and return?
As an example consider
inline int foo(void) {
return 1;
}
int bar(void) {
//Some statements
foo()
//Some more statements
return 2;
}
Will the foo()
in bar(
) return before bar reaches return 2;
line because the code is copy-pasted by compiler? Else how is the return statement handled in inline functions? I do understand that this will not break the code flow now, but how is the return statement handled if the code is copy-pasted or inlined?
Inline functions are more than a simple copy-paste procedure (in contrast to, say, preprocessor macros). They behave like normal functions, so any return value would be reflected to the caller just like a normal function.
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.
The return statement returns the flow of the execution to the function from where it is called. This statement does not mandatorily need any conditional statements. As soon as the statement is executed, the flow of the program stops immediately and returns the control from where it was called.
In lieu of a data type, void functions use the keyword "void." A void function performs a task, and then control returns back to the caller--but, it does not return a value. You may or may not use the return statement, as there is no return value.
return statement terminates a function and transfer program control to its caller function. Optionally it may return a value to the caller. You can use the return statement anywhere inside a function.
Inline Functions in C++ 1 If a function contains a loop. (for, while, do-while) 2 If a function contains static variables. 3 If a function is recursive. 4 If a function return type is other than void, and the return statement doesn’t exist in function body. 5 If a function contains switch or goto statement.
The return statement under the else if block passes the control back to calling function i.e main (). If the return statement would not have been there in the else if block, the control would have been moved ahead to execute the statement following if-else statement.
When the inline function is called whole code of the inline function gets inserted or substituted at the point of inline function call. This substitution is performed by the C++ compiler at compile time.
Inline functions are more than a simple copy-paste procedure (in contrast to, say, preprocessor macros). They behave like normal functions, so any return value would be reflected to the caller just like a normal function.
Optimizations should generally not alter the observable behavior of the code. This is true in C++ except for a few cases (floating point operation reordering, copy elision). Inlining a function will not change the code flow of the program.
Think of functions as if they are rules for computing the values, not returning something. It's just that a return
keyword says that if control reaches the keyword then the value computed by the current function is whatever is next to the keyword and the current function should exit immediately (unconditional jump onto the closing {
of the current function).
In your example some function foo()
is programmed to return 1
. If it is not inlined then the compiler emits code that indeed puts 1
somewhere where the caller expects to find it and there's an instruction for return. If it isn't inlined - okay, the compiler just sees that the function result is 1
at all times and compiles the code which calls this function appropriately - whereever that function result is used it simply "inserts" 1
. In your example foo()
result is not used, so the compiler simply ignores the result and the function call is compiled into no-op.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With