28
Chapter 14 Functions

Chapter 14 Functions. BYU CS/ECEn 124Variables and Operators2 Topics to Cover… Functions C Functions Function Examples Function Notes main Function Activation

Embed Size (px)

Citation preview

Page 1: Chapter 14 Functions. BYU CS/ECEn 124Variables and Operators2 Topics to Cover… Functions C Functions Function Examples Function Notes main Function Activation

Chapter 14Functions

Page 2: Chapter 14 Functions. BYU CS/ECEn 124Variables and Operators2 Topics to Cover… Functions C Functions Function Examples Function Notes main Function Activation

BYU CS/ECEn 124 Variables and Operators 2

Topics to Cover…

Functions C Functions Function Examples Function Notes main Function Activation Records Run-time Stack Function Calls

Page 3: Chapter 14 Functions. BYU CS/ECEn 124Variables and Operators2 Topics to Cover… Functions C Functions Function Examples Function Notes main Function Activation

BYU CS/ECEn 124 Variables and Operators 3

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, ...

Functions

Page 4: Chapter 14 Functions. BYU CS/ECEn 124Variables and Operators2 Topics to Cover… Functions C Functions Function Examples Function Notes main Function Activation

BYU CS/ECEn 124 Variables and Operators 4

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.

Functions

Page 5: Chapter 14 Functions. BYU CS/ECEn 124Variables and Operators2 Topics to Cover… Functions C Functions Function Examples Function Notes main Function Activation

BYU CS/ECEn 124 Variables and Operators 5

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.

Functions

Page 6: Chapter 14 Functions. BYU CS/ECEn 124Variables and Operators2 Topics to Cover… Functions C Functions Function Examples Function Notes main Function Activation

BYU CS/ECEn 124 Variables and Operators 6

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.

Functions

Page 7: Chapter 14 Functions. BYU CS/ECEn 124Variables and Operators2 Topics to Cover… Functions C Functions Function Examples Function Notes main Function Activation

BYU CS/ECEn 124 Variables and Operators 7

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).

C Functions

Page 8: Chapter 14 Functions. BYU CS/ECEn 124Variables and Operators2 Topics to Cover… Functions C Functions Function Examples Function Notes main Function Activation

BYU CS/ECEn 124 Variables and Operators 8

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

C Functions

Page 9: Chapter 14 Functions. BYU CS/ECEn 124Variables and Operators2 Topics to Cover… Functions C Functions Function Examples Function Notes main Function Activation

BYU CS/ECEn 124 Variables and Operators 9

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 {...}

C Functions

Page 10: Chapter 14 Functions. BYU CS/ECEn 124Variables and Operators2 Topics to Cover… Functions C Functions Function Examples Function Notes main Function Activation

BYU CS/ECEn 124 Variables and Operators 10

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.

C Functions

Page 11: Chapter 14 Functions. BYU CS/ECEn 124Variables and Operators2 Topics to Cover… Functions C Functions Function Examples Function Notes main Function Activation

BYU CS/ECEn 124 Variables and Operators 11

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)

formal parameters

arguments

function definition

Function Examples

Page 12: Chapter 14 Functions. BYU CS/ECEn 124Variables and Operators2 Topics to Cover… Functions C Functions Function Examples Function Notes main Function Activation

BYU CS/ECEn 124 Variables and Operators 12

#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 */

Example #2Function Examples

Page 13: Chapter 14 Functions. BYU CS/ECEn 124Variables and Operators2 Topics to Cover… Functions C Functions Function Examples Function Notes main Function Activation

BYU CS/ECEn 124 Variables and Operators 13

Example #3

#include <stdio.h>int Factorial(int n); // function declarationint 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}

Function Examples

Page 14: Chapter 14 Functions. BYU CS/ECEn 124Variables and Operators2 Topics to Cover… Functions C Functions Function Examples Function Notes main Function Activation

BYU CS/ECEn 124 Variables and Operators 14

Function Notes

Minimal do-nothing functionvoid dummy( ) {}

Default return type is integerthree( ) { 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 arethe same

Function Notes

Page 15: Chapter 14 Functions. BYU CS/ECEn 124Variables and Operators2 Topics to Cover… Functions C Functions Function Examples Function Notes main Function Activation

BYU CS/ECEn 124 Variables and Operators 15

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 */

scope of xyz

without prototypewith prototype

...int xyz(int, int);...

Function Notes

Page 16: Chapter 14 Functions. BYU CS/ECEn 124Variables and Operators2 Topics to Cover… Functions C Functions Function Examples Function Notes main Function Activation

BYU CS/ECEn 124 Variables and Operators 16

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

main Function

Page 17: Chapter 14 Functions. BYU CS/ECEn 124Variables and Operators2 Topics to Cover… Functions C Functions Function Examples Function Notes main Function Activation

BYU CS/ECEn 124 Variables and Operators 17

Implementing Functions in C

Functions are the C equivalent of subroutine in assembly language.

Function calls involve three basic steps1) Parameters from caller are passed to the callee.

Control is passed to callee.2) Callee does the task3) Return value is passed back to caller.

Control returns to the caller. Functions must be caller-independent, i.e.

callable from any function.

Activation Records

Page 18: Chapter 14 Functions. BYU CS/ECEn 124Variables and Operators2 Topics to Cover… Functions C Functions Function Examples Function Notes main Function Activation

BYU CS/ECEn 124 Variables and Operators 18

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 1mov.w 0(sp),r12 ; return value

Activation Records

Page 19: Chapter 14 Functions. BYU CS/ECEn 124Variables and Operators2 Topics to Cover… Functions C Functions Function Examples Function Notes main Function Activation

BYU CS/ECEn 124 Variables and Operators 19

Run-time Stack Operation

main

Memory

SP

Program starts

main

func A

SP

Memory

main calls A

main

SP

func A

func B

A calls B

Run-time Stack

Page 20: Chapter 14 Functions. BYU CS/ECEn 124Variables and Operators2 Topics to Cover… Functions C Functions Function Examples Function Notes main Function Activation

BYU CS/ECEn 124 Variables and Operators 20

Runtime Stack Operation

main

Memory

SP

A returns to main

main

func A

SP

Memory

B returns to A

Run-time Stack

Page 21: Chapter 14 Functions. BYU CS/ECEn 124Variables and Operators2 Topics to Cover… Functions C Functions Function Examples Function Notes main Function Activation

BYU CS/ECEn 124 Variables and Operators 21

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.

Activation Records

Page 22: Chapter 14 Functions. BYU CS/ECEn 124Variables and Operators2 Topics to Cover… Functions C Functions Function Examples Function Notes main Function Activation

BYU CS/ECEn 124 Variables and Operators 22

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

High Address

Sta

ck

Stack Pointer (SP)

Frame orActivation Record

Activation Records

Page 23: Chapter 14 Functions. BYU CS/ECEn 124Variables and Operators2 Topics to Cover… Functions C Functions Function Examples Function Notes main Function Activation

BYU CS/ECEn 124 Variables and Operators 23

Activation Record (Frame)

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

Name Type Offset Scope

Symbol Table

int func(int a, int b){ int x, y, z; … return y;}

y

z

x

aSP

Return Address Bookkeeping Info

b

Local variables

Memory

Activation Records

Page 24: Chapter 14 Functions. BYU CS/ECEn 124Variables and Operators2 Topics to Cover… Functions C Functions Function Examples Function Notes main Function Activation

BYU CS/ECEn 124 Variables and Operators 24

Stack Frames / Parameter Passing

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;}

main:0x8762: 8221 SUB.W #4,SP0x8764: 40B1 0032 0000 MOV.W #0x0032,0x0000(SP)0x876a: 40B1 003C 0002 MOV.W #0x003c,0x0002(SP)0x8770: 403C 000A MOV.W #0x000a,R120x8774: 403D 0014 MOV.W #0x0014,R130x8778: 403E 001E MOV.W #0x001e,R140x877c: 403F 0028 MOV.W #0x0028,R150x8780: 12B0 853C CALL #func0x8784: 430C CLR.W R120x8786: 5221 ADD.W #4,SP0x8788: 4130 RET func:0x853c: 8031 000E SUB.W #0x000e,SP0x8540: 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),R120x8562: 512C ADD.W @SP,R120x8564: 511C 0004 ADD.W 0x0004(SP),R120x8568: 511C 0006 ADD.W 0x0006(SP),R120x856c: 511C 0010 ADD.W 0x0010(SP),R120x8570: 511C 0012 ADD.W 0x0012(SP),R120x8574: 511C 0008 ADD.W 0x0008(SP),R120x8578: 511C 000A ADD.W 0x000a(SP),R120x857c: 511C 000C ADD.W 0x000c(SP),R120x8580: 5031 000E ADD.W #0x000e,SP0x8584: 4130 RET

f 60 0x0012(SP)e 50 0x0010(SP)

Return adrz 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

Run-time Stack

Page 25: Chapter 14 Functions. BYU CS/ECEn 124Variables and Operators2 Topics to Cover… Functions C Functions Function Examples Function Notes main Function Activation

BYU CS/ECEn 124 Variables and Operators 25

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.

Function Calls

Page 26: Chapter 14 Functions. BYU CS/ECEn 124Variables and Operators2 Topics to Cover… Functions C Functions Function Examples Function Notes main Function Activation

BYU CS/ECEn 124 Variables and Operators 26

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

Function Calls

Page 27: Chapter 14 Functions. BYU CS/ECEn 124Variables and Operators2 Topics to Cover… Functions C Functions Function Examples Function Notes main Function Activation

BYU CS/ECEn 124 Variables and Operators 27

Activation Record (Frame)

int main(){ int n; int m; … m = func(n, 10); …}

int func(int a, int b){ int x, y, z; … return y;}

SP

Memory

Function Calls

y

z

x

a

Return Address

b

Localvariables

ActivationRecord forfunc

n

m

Return Address

Localvariables

ActivationRecord formain

Page 28: Chapter 14 Functions. BYU CS/ECEn 124Variables and Operators2 Topics to Cover… Functions C Functions Function Examples Function Notes main Function Activation

BYU CS/ECEn 124 Variables and Operators 28