|
|
|
|
|
|
|
FIGURE 5.6
Moving the stack pointer. |
|
|
|
|
|
|
|
|
When your program calls a function, a stack frame is established. A stack frame is an area of the stack set aside to manage that function. This is very complex and different on different computers, but here are the essential steps: |
|
|
|
|
|
|
|
|
1. The return address of the function is put on the stack. When your function returns, it will resume executing at this address. |
|
|
|
|
|
|
|
|
2. Room is made on the stack for the return type you've declared. |
|
|
|
|
|
|
|
|
3. All the arguments to the function are placed on the stack. |
|
|
|
|
|
|
|
|
4. The program branches to your function. |
|
|
|
|
|
|
|
|
5. Local variables are pushed onto the stack as they are defined. |
|
|
|
|
|
|
|
|
This chapter introduced functions. A function is, in effect, a subprogram into which you can pass parameters and from which you can return a value. Every C++ program starts in the main() function, and main() in turn can call other functions. |
|
|
|
|
|
|
|
|
A function is declared with a function prototype, which describes the return value, the function name, and its parameter types. A function can optionally be declared inline. A function prototype can also declare default variables for one or more of the parameters. |
|
|
|
|
|
|
|
|
The function definition must match the function prototype in return type, name, and parameter list. Function names can be overloaded by changing the number or type of parameters; the compiler finds the right function based on the argument list. |
|
|
|
|
|