say there is a function to calculate factorial(n)
Does factorial(7) creates 7 function object for each of n from 1 to 7
and use those values when ever necessary (for factorial(8) as like factorial(7)*8)
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It depends on the language and the language implementation.
In many functional languages (e.g. Haskell), a function is guaranteed to change nothing; only to return a value. This lack of side effects allows the language to remember/cache, or ‘memoize’, the results of function calls.
In a less sophisticated language, 7 distinct function call frames might be placed on the stack and popped off.
A properly written factorial function in many functional languages would also be tail recursive; in that case the language might choose to simply jump from the bottom of the function to the top to avoid creating another function call. In this case the language is turning the recursive function into a loop ‘for free’.