Download presentation
Presentation is loading. Please wait.
1
Chapter 14 Functions
2
Variables and Operators
Topics to Cover… Functions C Functions Function Examples Function Notes main Function Activation Records Run-time Stack Function Calls BYU CS/ECEn 124 Variables and Operators
3
Variables and Operators
Functions Functions Smaller, simpler, subcomponent of program Provides abstraction hide low-level details give high-level structure to program, easier to understand overall program flow enables separable, independent development Decomposition Functions break large computing tasks into smaller ones. Functions enlarge the set of elementary building blocks. In other languages, called procedures, methods, subroutines, ... BYU CS/ECEn 124 Variables and Operators
4
Example of High-Level Structure
Functions Example of High-Level Structure int main() { SetupBoard(); /* place pieces on board */ DetermineSides(); /* choose black/white */ /* Play game */ do { WhitesTurn(); BlacksTurn(); } while (NoOutcomeYet()); } Structure of program is evident, even without knowing implementation. BYU CS/ECEn 124 Variables and Operators
5
Variables and Operators
Functions The Power of Functions Decomposition Functions break large computing tasks into smaller ones. Functions enlarge the set of elementary building blocks. Abstraction Separate the “function” of a component from the details of how it is accomplished. The component can be used as a building block while hiding the details in the function. BYU CS/ECEn 124 Variables and Operators
6
Variables and Operators
Functions Functions in C Functions have been in all programming languages since the very early days of computing. Support for functions is provided directly in all instruction set architectures. C is heavily oriented around functions. A C program is organized as a collection of functions. Every C statement belongs to a function All C programs start and finish execution in the function main Functions may call other functions which, in turn, call more functions. BYU CS/ECEn 124 Variables and Operators
7
Variables and Operators
C Functions Parts of a C Function The Declaration: Informs the compiler about the function. The Definition: The function body contained inside braces {...}. The Return Value: Calculated by the function, and returned by a return statement in the body. The Call: Arguments (actual parameters) from the caller are transmitted to the function parameters (formal parameters). BYU CS/ECEn 124 Variables and Operators
8
Variables and Operators
C Functions Parts of a C Function The Declaration: type name(type, type, ... ); Informs the compiler about the function Only argument types needed Ends in a semi-colon The function prototype declares: The function name The type of the output value returned by the function (void = nothing returned) The types of input arguments that the function accepts as input BYU CS/ECEn 124 Variables and Operators
9
Variables and Operators
C Functions Parts of a C Function The Definition: type name(type param, type param,...) { body } The first line matches type in the declaration (but without the semicolon) The list of input arguments (formal parameters) by type and name The function body contained inside braces {...} BYU CS/ECEn 124 Variables and Operators
10
Variables and Operators
C Functions Parts of a C Function The Return Value: return expression; Returned by a return statement in the body Must match declaration return type The Call: name(expression, expression, ... ) Arguments (actual parameters) from the caller are transmitted to the function parameters (formal parameters) Function definitions and function calls must having matching prototypes. BYU CS/ECEn 124 Variables and Operators
11
Variables and Operators
Function Examples Example #1 #include <stdio.h> int addNumbers(int, int); int main() { printf("\nResult is: %d", addNumbers(4, 5)); } int addNumbers(int x, int y) return (x+y); function prototype (declaration) arguments formal parameters function definition BYU CS/ECEn 124 Variables and Operators
12
Variables and Operators
Function Examples Example #2 #include <stdio.h> void PrintBanner(); /* function declaration */ int main() { PrintBanner(); /* function call */ printf("\nA simple C program."); PrintBanner(); /* function call */ } void PrintBanner() /* function definition */ { printf("\n=================================="); } /* no return value needed */ BYU CS/ECEn 124 Variables and Operators
13
Variables and Operators
Function Examples Example #3 #include <stdio.h> int Factorial(int n); // function declaration int main() { int number, answer; answer = Factorial(number); // function call } int Factorial(int n) // function definition { int i; int result = 1; for(i=1; i<=n; i++) result = result * i; return result; // return value to caller } BYU CS/ECEn 124 Variables and Operators
14
Variables and Operators
Function Notes Function Notes Minimal do-nothing function void dummy( ) {} Default return type is integer three( ) { return 3; } int three( ) { return 3; } Function definitions cannot nest Function arguments are local to the function Function arguments are passed “by value” Temporary (private) variables rather than the originals Value could be a reference (such as with an array) Call by value is an asset, not a liability These are the same BYU CS/ECEn 124 Variables and Operators
15
Variables and Operators
Function Notes Function Notes The scope of a name (variable or function) is the part of the program within which the name can be used. A function prototype “extends” the scope of a function and helps the compiler check function call syntax. with prototype ... int xyz(int, int); ... scope of xyz without prototype int xyz(int x, int y) { } ... /* end of file */ BYU CS/ECEn 124 Variables and Operators
16
Variables and Operators
main Function The main Function Must be present in every program Is “called” by the operating system when program is run Returning from main exits the program Is pre-declared as: int main (int argc, char *argv[]); The definition doesn’t have to match. int main() { ... } main() { ... } The return statement can be omitted. Don’t worry about what this means. These are OK BYU CS/ECEn 124 Variables and Operators
17
Implementing Functions in C
Activation Records Implementing Functions in C Functions are the C equivalent of subroutine in assembly language. Function calls involve three basic steps Parameters from caller are passed to the callee. Control is passed to callee. Callee does the task Return value is passed back to caller. Control returns to the caller. Functions must be caller-independent, i.e. callable from any function. BYU CS/ECEn 124 Variables and Operators
18
Variables and Operators
Activation Records Activation Records When a function is called, it needs to be activated, that is, local variables must be given locations in memory. An activation record for a function is a template of the relative positions of its local variables in memory. A frame is a local data storage area allocated on the stack each time a function is called for the activation record. Frame variables are for temporary storage and are lost when the function returns. The stack pointer is used for the frame pointer. Data is stored and retrieved via indexed stack instructions. mov.w r12,0(sp) ; save parameter 1 mov.w 0(sp),r12 ; return value BYU CS/ECEn 124 Variables and Operators
19
Run-time Stack Operation
Program starts main calls A A calls B Memory Memory SP func B SP func A func A SP main main main BYU CS/ECEn 124 Variables and Operators
20
Runtime Stack Operation
B returns to A A returns to main Memory Memory SP func A SP main main BYU CS/ECEn 124 Variables and Operators
21
Stack Frames / Parameter Passing
Activation Records Stack Frames / Parameter Passing Each function call creates a new stack frame. The parameters of a called function are passed to a function in a right to left order. Up to four left most parameters are passed in registers unless they are defined as a struct or union type, in which case they are also passed on the stack. The remaining parameters are always passed on the stack. BYU CS/ECEn 124 Variables and Operators
22
Stack Frames / Parameter Passing
Activation Records Stack Frames / Parameter Passing Each function call creates a new stack frame. High Address Stack Parameters (When more than 4) Frame or Activation Record Return Address Saved Registers (if any) Local Variables Stack Pointer (SP) Low Address BYU CS/ECEn 124 Variables and Operators
23
Activation Record (Frame)
Activation Records Activation Record (Frame) int func(int a, int b) { int x, y, z; … return y; } Memory Return Address Bookkeeping Info z y Symbol Table x Local variables b Name Type Offset Scope SP a a int 0(SP) func b int 2(SP) func x int 4(SP) func y int 6(SP) func z int 8(SP) func BYU CS/ECEn 124 Variables and Operators
24
Stack Frames / Parameter Passing
Run-time Stack Stack Frames / Parameter Passing main: 0x8762: SUB.W #4,SP 0x8764: 40B MOV.W #0x0032,0x0000(SP) 0x876a: 40B1 003C MOV.W #0x003c,0x0002(SP) 0x8770: 403C 000A MOV.W #0x000a,R12 0x8774: 403D MOV.W #0x0014,R13 0x8778: 403E 001E MOV.W #0x001e,R14 0x877c: 403F MOV.W #0x0028,R15 0x8780: 12B0 853C CALL #func 0x8784: 430C CLR.W R12 0x8786: ADD.W #4,SP 0x8788: RET func: 0x853c: E SUB.W #0x000e,SP 0x8540: 4F MOV.W R15,0x0006(SP) 0x8544: 4E MOV.W R14,0x0004(SP) 0x8548: 4D MOV.W R13,0x0002(SP) 0x854c: 4C MOV.W R12,0x0000(SP) 0x8550: MOV.W #1,0x0008(SP) 0x8554: 43A1 000A MOV.W #2,0x000a(SP) 0x8558: 40B C MOV.W #0x0003,0x000c(SP) 0x855e: 411C MOV.W 0x0002(SP),R12 0x8562: 512C ADD.W @SP,R12 0x8564: 511C ADD.W 0x0004(SP),R12 0x8568: 511C ADD.W 0x0006(SP),R12 0x856c: 511C ADD.W 0x0010(SP),R12 0x8570: 511C ADD.W 0x0012(SP),R12 0x8574: 511C ADD.W 0x0008(SP),R12 0x8578: 511C 000A ADD.W 0x000a(SP),R12 0x857c: 511C 000C ADD.W 0x000c(SP),R12 0x8580: E ADD.W #0x000e,SP 0x8584: RET Example: int func(a, b, c, d, e, f) { int x, y, z; x = 1; y = 2; z = 3; return a+b+c+d+e+f+x+y+z; } int main() func(10, 20, 30, 40, 50, 60); return 0; f 60 0x0012(SP) e 50 0x0010(SP) Return adr z 3 0x000c(SP) y 2 0x000a(SP) x 1 0x0008(SP) d 40 0x0006(SP) c 30 0x0004(SP) b 20 0x0002(SP) a 10 0x0000(SP) SP BYU CS/ECEn 124 Variables and Operators
25
Variables and Operators
Function Calls Function Calls Caller If more than 4 arguments, push function arguments on stack (right-to-left) Put 1st argument in R12, 2nd argument in R13,… Transfer control to function with call instruction Callee Create activation record on the stack Save arguments in activation record Save any callee-saved registers that are altered in activation record. BYU CS/ECEn 124 Variables and Operators
26
Variables and Operators
Function Calls The Return Callee Put return value in R12 Restore any saved registers Pop activation record Return control to caller (RET = Caller Return value is in R12 BYU CS/ECEn 124 Variables and Operators
27
Activation Record (Frame)
Function Calls Activation Record (Frame) Memory int main() { int n; int m; … m = func(n, 10); } int func(int a, int b) int x, y, z; return y; Return Address Activation Record for main m Local variables n Return Address z Activation Record for func y Local variables x b SP a BYU CS/ECEn 124 Variables and Operators
28
Variables and Operators
BYU CS/ECEn 124 Variables and Operators
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.