JOB Interview question! | Sololearn: Learn to code for FREE!

+5

JOB Interview question!

Hello, found this question into a interview review: "Can a recursive function be declared inline? Argument as much as you can. " It can be done? Advantages? Disadvantages? Thank you!

2/2/2019 9:18:02 PM

Cristian-Alexandru Stefan

4 Answers

New Answer

+10

Yes, a recursive function can be declared inline. However, the recursive function may not be inlined by some compilers. Otherwise, if the function is tail recursive, then the function can be inlined by the compiler. The compiler will create a call graph of a detected cycle to determine the number of inline functions to create. When a limit cannot be determined from a call graph, some compilers will inline recursive functions with a #pragma specifying a max depth level. When that limit is reached, the function will be called to continue the cycle. There could be a performance benefit by eliminating the overhead of new stacks for each function call. However, it could grow the size of the executable.

+5

Interesting. :) Excited to hear what's the solution. My guess would be no, since the compiler can not know how often the function will be calling itself (how often it has to be 'copypasted' in there), except maybe in case of constexpr where it can be calculated at compile time.

+5

Cristian-Alexandru Stefan Yes a recursive function can be defined with an "inline" qualifier. But remember "inline" is a hint to a compiler to replace the code in-place. Depending on the complexity of the function the compiler may or may not expand the function call inline. To increase the chances of getting your recursive function inline, use tail recursion in your recursive function.

0

Thank you for sharing great conman question regarding Job, Job interview questions mean HR round interview Questions or their. you should brief describe regarding your questions. i found conman Job interview Questions with Answers https://www.andlearning.org/common-job-interview-questions/ if you have any questions you can ask.