answersLogoWhite

0


Best Answer

It depends on the language. Some use one stack for everything within a program while others use multiple stacks for different purposes (return addresses, local variables, function arguments, exception handling and so on). For the purpose of this answer we shall assume a single stack.

The basic purpose of the stack is to allow functions to be invoked from multiple call sites and for those functions to be able to return control to the site that invoked them. If a function only has one call site then there is no need to make a function call at all; the code can simply be inline expanded by the compiler. The same applies to trivial functions that do very little work, such as a function that computes a simple value to be returned to the caller. Thus the programmer gains the benefit of having a function call that makes his code self-documenting and therefore easier to read, without the inconvenience of making an expensive function call.

However, when a function has multiple call sites and the function is too large to be inline expanded (because large code typically reduces performance, negating the benefit of inline expansion), the function needs to know from where it was called so that it can return control to the callee when it has completed its task. This is achieved by the call site pushing a return address onto the call stack. When the called function is finished, the return address can be popped from the stack and control returned to the caller. This process is generally known as winding and unwinding the stack, because it allows functions to call functions (to call functions) without losing track of where the calls came from. This also allows functions to recursively call themselves.

The stack is also used to store local variables. That is, variables that are scoped to a code block. When the block comes into scope, the variable is pushed onto the stack and when the block falls from scope, the variable is popped from the stack. Thus local variables are only visible from within the scope in which they are declared. Code blocks may also be nested, such that variables within the scope of the outer block are visible to code within the inner block, but variables within the scope of the inner block are not visible to code within the outer block. If an inner variable has the same name as an outer variable, the outer variable is hidden from the inner scope. Such trickery is best avoided as it makes code harder to read. However, a function is itself a code block, thus a function's local variables will make use of the stack.

Most functions also accept arguments (or parameters). In order to pass these arguments from the caller to the callee, the arguments are pushed onto the stack.

The stack is typically divided up into frames, such that each frame is only of relevance to a particular function. The frame at the top of the stack relates to the current function, and the one below relates to the function that called the current function. The current function's local variables are placed at the top of its frame, followed by the return address of the call site, followed by the arguments that were passed to the function from the call site.

If a function returns a value, the caller can store that value by assigning the function to a suitable variable. A function's return value is typically pushed onto the stack immediately after popping the function's stack frame, but if the function is not assigned to a variable, that value is lost forever. This is desirable when a return value is provided by a function but is not actually required by the caller. The value still exists at the top of the stack, just beyond the stack pointer, but short of inline assembly there's no way to retrieve it before the next function call, which will overwrite it with its stack frame.

Other information that may be contained in a function's frame include its exception handlers, if any. When an exception occurs and no exception handler is available, the current function immediately terminates, unwinding the stack. The stack will continue to unwind, prematurely terminating each function in turn, until a frame containing a suitable handler is found. It is vital that exception handlers make use of RAII (resource acquisition is initialisation) to ensure any resources acquired between the exception handler and the code that caused the exception are released back to the system in an orderly manner. Raw pointers to allocated memory must be avoided at all costs; use resource handles or smart pointers instead. If the stack completely unwinds then no handler was found and the program terminates with an unhandled exception error. Ugh! These can be extremely difficult to trace, therefore it is vital that you place a catch-all handler in the program's main function to capture as much information about unhandled exceptions as possible, and handle it as gracefully as possible. You can then use this information to (hopefully) provide a suitable handler within the appropriate function.

User Avatar

Wiki User

9y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

For storing intermediate values

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the role of stack in function call?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Which function call Does not consume stack space?

Calling an in-line function, which is not actually a function-call.


What is the maximum depth of recursive calls a function may make?

Recursive function call depend your primary memory space because the recursive call store in stack and stack based on memory.


How are arguments passed to a function?

Arguments are passed to functions via the thread's function call stack. Every thread has its own call stack, which is a small region of contiguous, fixed-size memory that extends downwards into lower addresses. The stack is allocated when the thread is instantiated and deallocated when the thread terminates thus there is minimal cost in using the stack. Data is pushed and popped from the stack while a stack pointer keeps track of the top of the stack (the lowest unused address).


What is role of stack in calling a subroutine and returning from the routine?

The stack has many purposes, but is chiefly used to enable procedure calls. When you invoke a function, the return address (the next instruction after the function call) is pushed onto the stack. Control is then passed to the function. When the called function returns, it pops the return address off the stack and control is returned to that address. In this way functions can call functions without losing track of where the calls came from because return addresses are popped in the reverse order they were pushed. In other words, it provides an automatic "breadcrumb trail" without which it would be impossible to call and return from functions. The stack is also used to pass actual arguments to functions by pushing values onto the stack immediately after the return address. The called function can then pop those values off the stack and assign them to its formal arguments. Finally, the stack is used to store the function's local variables prior to calling a function. These are pushed onto the stack before the return address is pushed. In this way, a function can call itself recursively without losing track of its own local variables. Since invoking and returning from functions is a time-consuming operation, compilers will try to expand functions inline and thus eliminate the overhead of the function call. However, this needs to be weighed against the increased code size which can affect overall performance. By keeping functions small (one or two very simple statements), programs not only become much easier to read and maintain, there's also a much greater opportunity for inline expansion. Complex functions are never good examples for inline expansion.


How are functions invoked in cpp?

If the function is inline expanded then it is not invoked at all -- there is no function call. However, if the function is not or cannot be inline expanded, a procedure call is invoked. This pushes the calling function's local values onto the stack, followed by the return address, followed by the callee's argument values in reverse order. Control is then passed to the address of the function. The function then pops the arguments off the stack and assigns them to its local parameters (parameters that are passed by value will automatically invoke the copy constructors of those parameters). The function then executes. When a return statement is encountered, the return address is popped from the stack, the return value (if any) is pushed onto the stack, and control is passed to the return address. When a function returns, the return value (if any) and the local values are popped from the stack, and execution continues from where it left off.

Related questions

Which function call Does not consume stack space?

Calling an in-line function, which is not actually a function-call.


Basic concept and application of stack?

A stack is a last in first out (LIFO) data structure. Stacks are typically used in backtracking algorithms where values are popped off the stack in the reverse order they were pushed onto the stack. Procedural programming languages make use of a call stack so that functions can easily return control to their callers -- which is vital when a function may have more than one call site. The calling function pushes the return address onto the call stack and then passes control to the callee function. When the callee has finished its task, the return address is popped off the stack and control returned to that address. In this way, functions can call other functions (including themselves) without losing track of where those calls came from. The call stack is also used to store a function's local variables as well as any arguments that were passed to the function by its caller. The function's exception handlers can also be placed on the stack. When an unhandled exception occurs, the call stack unwinds until a stack frame with a suitable exception handler is located. If the call stack unwinds all the way to the program's entry point (the main function) and there is still no handler, the program terminates with an unhandled exception error.


What is the maximum depth of recursive calls a function may make?

Recursive function call depend your primary memory space because the recursive call store in stack and stack based on memory.


In C plus plus How does a function pass control back to the calling function?

The calling code pushes the return address onto the call stack. When the function returns, it pops the return address off the call stack and returns control to that address. The call stack (or simply the stack), is also used to pass parameters to functions and to receive return values from functions, as well as for local storage, evaluation, and the this pointer when calling class member functions. Functions that call other functions, or that recursively call themselves, will increase consumption of the call stack accordingly. This is known as winding, because the return addresses will remain on the stack until the function is ready to return (even if the function calls itself or another function), thus allowing functions to automatically unwind. In other words, it's a bit like leaving a trail of breadcrumbs (as per the Hansel & Gretel fairytale) allowing functions to retrace their steps back to the original call site and, ultimately, back to the main function.


How are arguments passed to a function?

Arguments are passed to functions via the thread's function call stack. Every thread has its own call stack, which is a small region of contiguous, fixed-size memory that extends downwards into lower addresses. The stack is allocated when the thread is instantiated and deallocated when the thread terminates thus there is minimal cost in using the stack. Data is pushed and popped from the stack while a stack pointer keeps track of the top of the stack (the lowest unused address).


Can anybody explain stack level functioning of recursion in c language exclusively?

Yes, any body can explain stack level functioning of recursion in C language exclusively. ;-) Whenever we invoke a function, the return address is pushed onto the call stack. That return address remains on the call stack until the function returns at which point the address is popped from the stack and control passed to that address. In this way, functions can always find their way back to their callers, even if those functions invoke other functions, including themselves (recursive functions). As well as the return address, the formal arguments of the function and the local variables of the function are also pushed onto the stack. Formal arguments are initialised by the actual arguments passed by the caller, assigning the values of the actual arguments to the formal arguments. If the formal argument is a pointer variable or reference, the address of the actual argument is passed instead. In addition, the call stack is used by the exception handling mechanism. When an exception is thrown by a function, the call stack "unwinds" (popping each function's stack frame) until a suitable handler is found.


What does 'stack overflow at line 1776' mean?

This probably means that the stack ran out of memory when the program reached line 1776. This is in many cases caused by a function that calls itself too many times, since each function call takes up memory on the stack and that memory is not returned until the function exits.


How can you call a function written in FORTRAN from a C program?

Is the FORTRAN function part of a library. If it is it will be no different from call a c function in a library. There could be an issue the order that the functions attribures are pusshed onto the stack like there is with pascal.


What is role of stack in calling a subroutine and returning from the routine?

The stack has many purposes, but is chiefly used to enable procedure calls. When you invoke a function, the return address (the next instruction after the function call) is pushed onto the stack. Control is then passed to the function. When the called function returns, it pops the return address off the stack and control is returned to that address. In this way functions can call functions without losing track of where the calls came from because return addresses are popped in the reverse order they were pushed. In other words, it provides an automatic "breadcrumb trail" without which it would be impossible to call and return from functions. The stack is also used to pass actual arguments to functions by pushing values onto the stack immediately after the return address. The called function can then pop those values off the stack and assign them to its formal arguments. Finally, the stack is used to store the function's local variables prior to calling a function. These are pushed onto the stack before the return address is pushed. In this way, a function can call itself recursively without losing track of its own local variables. Since invoking and returning from functions is a time-consuming operation, compilers will try to expand functions inline and thus eliminate the overhead of the function call. However, this needs to be weighed against the increased code size which can affect overall performance. By keeping functions small (one or two very simple statements), programs not only become much easier to read and maintain, there's also a much greater opportunity for inline expansion. Complex functions are never good examples for inline expansion.


How are functions invoked in cpp?

If the function is inline expanded then it is not invoked at all -- there is no function call. However, if the function is not or cannot be inline expanded, a procedure call is invoked. This pushes the calling function's local values onto the stack, followed by the return address, followed by the callee's argument values in reverse order. Control is then passed to the address of the function. The function then pops the arguments off the stack and assigns them to its local parameters (parameters that are passed by value will automatically invoke the copy constructors of those parameters). The function then executes. When a return statement is encountered, the return address is popped from the stack, the return value (if any) is pushed onto the stack, and control is passed to the return address. When a function returns, the return value (if any) and the local values are popped from the stack, and execution continues from where it left off.


How is the old stack pointer value recovered on a function return?

If your stack grows bottom-up, it's decremented when you leave a function; if the stack grows top-down, the stack pointer is incremented.


Explain the allocation of memory cells when a function is called?

When you call a function, the stack pointer is adjusted to cater for the function's arguments (if any), the caller's return address (mandatory), the function's local variables (if any) and the function's exception handlers (if any).