creturninline

inlining when function body has no return statement


I read that compiler may not perform inlining when "return" statement does not exist in function body and also in the case where return type is other than void. If it's like inlining cannot happen with functions that return anything other than void, why it needed a "return" statement for making the function inline. Assume simple code as below:

Here the function declared as inline does not have a "return" statement in its body. Does inlining happen here? Is there any way through which we can know if the inline request has been accepted and executed by compiler?

#include<stdio.h>
inline void call()
{
    printf("*****In call*****\n");
}

main()
{
    call();
}

Solution

  • I read that compiler may not perform inlining when "return" statement does not exist in function body

    This is not at all true. Compiler can certainly inline void functions, but whether it does inline any function is upto it even if you specify inline keyword.

    Just see here

    This is the generated assembly:

    .LC0:
        .string "*****In call*****"
    main:
        subq    $8, %rsp
        movl    $.LC0, %edi
        call    puts
        movl    $0, %eax
        addq    $8, %rsp
        ret
    

    GCC does inline your code when compiled with -O. It also replaced the printf call with a simple puts.