|
|
|
|
|
|
|
This compiles into code that is the same as if you had written the following: |
|
|
|
|
|
|
|
|
By the time your program executes, the instructions are already in place, compiled into the OBJ file. This saves a jump in the execution of the code, at the cost of a larger program. |
|
|
|
|
|
|
|
|
Inline is a hint to the compiler that you would like the function to be inlined. The compiler is free to ignore the hint and make a real function call. |
|
|
|
|
|
|
|
|
|
How Functions WorkA Look Under the Hood |
|
|
|
|
|
|
|
|
When you call a function, the code branches to the called function, parameters are passed in, and the body of the function is executed. When the function completes, a value is returned (unless the function returns void), and control returns to the calling function. |
|
|
|
|
|
|
|
|
How is this task accomplished? How does the code know where to branch? Where are the variables kept when they are passed in? What happens to variables that are declared in the body of the function? How is the return value passed back out? How does the code know where to resume? |
|
|
|
|
|
|
|
|
New Term: When you begin your program the compiler creates a stack. The stack is a special area of memory allocated for your program to hold the data required by each of the functions in your program. It is called a stack because it is a last-in first-out queue, much like a stack of dishes at a cafeteria, as shown in Figure 5.4. |
|
|
|
|
|
|
|
|
Last-in first-out means that whatever is added to the stack last will be the first thing taken off. Most queues are like a line at a theater: the first one on line is the first one off. A stack is more like a stack of coins: if you stack 10 pennies on a tabletop and then take some back, the last three you put on will be the first three you take off. |
|
|
|
|
|
|
|
|
When data is pushed onto the stack, the stack grows; as data is popped off the stack, the stack shrinks. It isn't possible to pop a dish off of the stack without first popping off all the dishes placed on after that dish. |
|
|
|
|
|