Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will return statements in an inline function in C++ actually return and terminate the code flow?

Tags:

c++

inline

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?

like image 278
kartik Avatar asked Sep 22 '14 10:09

kartik


People also ask

Can inline function return value in C?

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.

Does return statement terminate 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.

How does the return function work in C?

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.

Which function must not use a return statement?

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.

What is the use of return statement in C?

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.

How to check if a function is inline in C++?

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.

What does the return statement under the else if block do?

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 is inserted?

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.


Video Answer


3 Answers

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.

like image 115
Eitan T Avatar answered Sep 25 '22 07:09

Eitan T


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.

like image 37
pmr Avatar answered Sep 21 '22 07:09

pmr


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.

like image 31
sharptooth Avatar answered Sep 24 '22 07:09

sharptooth