779 views
0 votes
0 votes
Programming languages which permit a function to return a function as its result cannot be implemented with a stack-based storage allocation scheme for activation records.

Why?

1 Answer

1 votes
1 votes

In stack-based storage allocation scheme for activation records, a function generally returns a value or an address (pointer). When a function (say the name is B) is to be returned by a function (say the name is A), the function B would have been defined within this function A itself.

When you return the function B you must be returning the address of the function B (will depend on the implementation of the language which is not our priority here), which resides in the activation record of A. As soon as A finishes, it will return the address of B and code of A will get popped out of stack. Now the function returned (B) is now referring to a something which is no more available as A has already been popped and with it, the code for B has also been removed. So, there is no way in which it could be implemented in this way. Hope you understood.

Please refer https://gateoverflow.in/477/gate2008-54 for detailed discussion.

Related questions

1 votes
1 votes
1 answer
2
jugnu1337 asked May 10, 2022
282 views
SOURCE NAME::: UNDERSTANDING POINTER IN C (BOOK) BY YASHWANT KANETKARmy answer is C am i right?
1 votes
1 votes
0 answers
3
aakash pandey asked Apr 30, 2022
289 views
We use character array to declare string in C. So, if I declare an array likecharacter ch[ ] = {‘a’,’k’,’a’,’/o’};and try printing ch[3] like :printf(“%...
0 votes
0 votes
0 answers
4
Anirudh Kaushal asked Apr 6, 2022
198 views
#include<stdio.h struct marks { int p:3; int c:3; int m:2; }; void main() { struct marks s = {2, -6, 5}; printf("%d %d %d", s.p, s.c, s.m); }What does p:3 means here ?