280 likes | 526 Views
Chapter 14 Functions. Topics to Cover…. Functions C Functions Function Examples Function Notes main Function Activation Records Run-time Stack Function Calls. Functions. Functions. Smaller, simpler, subcomponent of program Provides abstraction hide low-level details
E N D
Topics to Cover… • Functions • C Functions • Function Examples • Function Notes • main Function • Activation Records • Run-time Stack • Function Calls 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, ... Variables and Operators
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 programis evident, even withoutknowing implementation. 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. 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. 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). 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 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 {...} 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. Variables and Operators
function prototype (declaration) arguments formal parameters function definition 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); } 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 */ 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} 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 Variables and Operators
...int xyz(int, int);... scope of xyz with prototype without prototype 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. int xyz(int x, int y){ ...}... /* end of file */ Variables and Operators
Don’t worry about what this means. These are OK 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. Variables and Operators
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. 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 Variables and Operators
Run-time Stack Run-time Stack Operation main calls A Program starts A calls B Memory Memory SP func B SP func A func A SP main main main Variables and Operators
Run-time Stack Runtime Stack Operation A returns to main B returns to A Memory Memory SP func A SP main main Variables and Operators
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. Variables and Operators
High Address Frame or Activation Record Stack Stack Pointer (SP) Activation Records Stack Frames / Parameter Passing • Each function call creates a new stack frame. Parameters (When more than 4) Return Address Saved Registers (if any) Local Variables Low Address Variables and Operators
Activation Records Activation Record (Frame) Memory int func(int a, int b) { int x, y, z; … return y; } Return Address Bookkeeping Info z y Symbol Table x Local variables b Type Offset Scope Name a SP 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 Variables and Operators
Run-time Stack Stack Frames / Parameter Passing main: 0x8762: 8221 SUB.W #4,SP 0x8764: 40B1 0032 0000 MOV.W #0x0032,0x0000(SP) 0x876a: 40B1 003C 0002 MOV.W #0x003c,0x0002(SP) 0x8770: 403C 000A MOV.W #0x000a,R12 0x8774: 403D 0014 MOV.W #0x0014,R13 0x8778: 403E 001E MOV.W #0x001e,R14 0x877c: 403F 0028 MOV.W #0x0028,R15 0x8780: 12B0 853C CALL #func 0x8784: 430C CLR.W R12 0x8786: 5221 ADD.W #4,SP 0x8788: 4130 RET func: 0x853c: 8031 000E SUB.W #0x000e,SP 0x8540: 4F81 0006 MOV.W R15,0x0006(SP) 0x8544: 4E81 0004 MOV.W R14,0x0004(SP) 0x8548: 4D81 0002 MOV.W R13,0x0002(SP) 0x854c: 4C81 0000 MOV.W R12,0x0000(SP) 0x8550: 4391 0008 MOV.W #1,0x0008(SP) 0x8554: 43A1 000A MOV.W #2,0x000a(SP) 0x8558: 40B1 0003 000C MOV.W #0x0003,0x000c(SP) 0x855e: 411C 0002 MOV.W 0x0002(SP),R12 0x8562: 512C ADD.W @SP,R12 0x8564: 511C 0004 ADD.W 0x0004(SP),R12 0x8568: 511C 0006 ADD.W 0x0006(SP),R12 0x856c: 511C 0010 ADD.W 0x0010(SP),R12 0x8570: 511C 0012 ADD.W 0x0012(SP),R12 0x8574: 511C 0008 ADD.W 0x0008(SP),R12 0x8578: 511C 000A ADD.W 0x000a(SP),R12 0x857c: 511C 000C ADD.W 0x000c(SP),R12 0x8580: 5031 000E ADD.W #0x000e,SP 0x8584: 4130 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; } SP 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. 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 = mov.w @sp+,pc) • Caller • Return value is in R12 Variables and Operators
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 a SP Variables and Operators