Chapter 14 Functions. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 14-2 Function Smaller, simpler, subcomponent.

Slides:



Advertisements
Similar presentations
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 6 Repetition Statements.
Advertisements

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 5 Selection Statements Animated Version.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 5 Selection Statements.
Chapter 13 Control Structures. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Control Structures Conditional.
Chapter 15 Debugging. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Debugging with High Level Languages.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four Trusses.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
1 Introduction to “C” Control Loops and Functions Patt and Patel Ch. 13 & 14.
CS 201 Functions Debzani Deb.
Copyright © The McGraw-Hill Companies, Inc
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 2 Image Slides.
Chapter 8 Traffic-Analysis Techniques. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 8-1.
Chapter 14 Functions. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Function Smaller, simpler, subcomponent.
CMPE13Cyrus Bazeghi Chapter 14 Functions in C. CMPE13 2 Functions Smaller, simpler, subcomponents of programs Provide abstraction –hide low-level details.
Chapter 14 Functions.
Sudeshna Sarkar, IIT Kharagpur 1 Functions Lecture
Functions. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 9-2 JSR Instruction Jumps to a location (like.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Three Statics of Structures Reactions.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7.
Chapter 13 Transportation Demand Analysis. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display
Chapter 14 Functions.
Introduction to Algorithms Second Edition by
Introduction to Algorithms Second Edition by
Chapter 10 Image Slides Title
PowerPoint Presentation Materials Transportation Engineering
PowerPoint Presentations
Chapter 14 Functions.
Copyright © The McGraw-Hill Companies, Inc
Chapter 14 Functions.
Chapter 3 Image Slides Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Introduction to Algorithms Second Edition by
Chapter 3 Image Slides Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter 8 Image Slides Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Discrete Mathematics and Its Applications Chapter 7 Images
Chapter R A Review of Basic Concepts and Skills
Introduction to Algorithms Second Edition by
Copyright ©2014 The McGraw-Hill Companies, Inc
Chapter 7 Image Slides Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter 14 Functions.
Introduction to Algorithms Second Edition by
Copyright © 2004 The McGraw-Hill Companies, Inc. All rights reserved.
Assignment Pages: 10 – 12 (Day 1) Questions over Assignment? # 1 – 4
Introduction to Algorithms Second Edition by
Chapter R.2 A Review of Basic Concepts and Skills
Chapter 12 Image Slides Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Introduction to Algorithms Second Edition by
Introduction to Algorithms Second Edition by
Chapter 5 Foundations in Microbiology Fourth Edition
Copyright © 2004 The McGraw-Hill Companies, Inc. All rights reserved.
Introduction to Algorithms Second Edition by
Introduction to Algorithms Second Edition by
Title Chapter 22 Image Slides
Chapter 3 Foundations in Microbiology Fourth Edition
Copyright © The McGraw-Hill Companies, Inc
Copyright © 2004 The McGraw-Hill Companies, Inc. All rights reserved.
Introduction to Algorithms Second Edition by
CHAPTER 6 SKELETAL SYSTEM
Copyright © 2004 The McGraw-Hill Companies, Inc. All rights reserved.
Introduction to Algorithms Second Edition by
Journey to the Cosmic Frontier
Journey to the Cosmic Frontier
Discrete Mathematics and Its Applications Chapter 7 Images
Journey to the Cosmic Frontier
Copyright © 2004 The McGraw-Hill Companies, Inc. All rights reserved.
Chapter 14 Functions.
Chapter 24 Image Slides* *See PowerPoint Lecture Outline for a complete ready-made presentation integrating art and lecture notes Copyright © The.
Copyright © 2004 The McGraw-Hill Companies, Inc. All rights reserved.
Chapter 3 Introduction to Physical Design of Transportation Facilities.
Introduction to Algorithms Second Edition by
Chapter 14 Functions.
Presentation transcript:

Chapter 14 Functions

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Function 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 C functions zero or multiple arguments passed in single result returned (optional) return value is always a particular type In other languages, called procedures, subroutines,...

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Example of High-Level Structure 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.

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Functions in C Declaration (also called prototype) int Factorial(int n); Function call -- used in expression a = x + Factorial(f + g); type of return value name of function types of all arguments 1. evaluate arguments 2, execute function 3. use return value in expression

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Function Definition State type, name, types of arguments must match function declaration give name to each argument (doesn't have to match declaration) int Factorial(int n) { int i; int result = 1; for (i = 1; i <= n; i++) result *= i; return result; } gives control back to calling function and returns value

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Why Declaration? Since function definition also includes return and argument types, why is declaration needed? Use might be seen before definition. Compiler needs to know return and arg types and number of arguments. Definition might be in a different file, written by a different programmer. include a "header" file with function declarations only compile separately, link together to make executable

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Example double ValueInDollars(double amount, double rate); main() {... dollars = ValueInDollars(francs, DOLLARS_PER_FRANC); printf("%f francs equals %f dollars.\n", francs, dollars);... } double ValueInDollars(double amount, double rate) { return amount * rate; } declaration function call (invocation) definition

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Implementing Functions: Overview Activation record information about each function, including arguments and local variables stored on run-time stack Calling function push new activation record copy values into arguments call function get result from stack Called function execute code put result in activation record pop activation record from stack return

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Run-Time Stack Recall that local variables are stored on the run-time stack in an activation record Frame pointer (R5) points to the beginning of a region of activation record that stores local variables for the current function When a new function is called, its activation record is pushed on the stack; when it returns, its activation record is popped off of the stack.

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Run-Time Stack main Memory R6 Watt Memory R6 main Memory main Before callDuring callAfter call R5 R6 R5

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Activation Record int NoName(int a, int b) { int w, x, y;... return y; } NameTypeOffsetScope abwxyabwxy int NoName y x w dynamic link return address return value a b bookkeeping locals args R5

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Activation Record Bookkeeping Return value space for value returned by function allocated even if function does not return a value Return address save pointer to next instruction in calling function convenient location to store R7 in case another function (JSR) is called Dynamic link callers frame pointer used to pop this activation record from stack

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Example Function Call int Volta(int q, int r) { int k; int m;... return k; } int Watt(int a) { int w;... w = Volta(w,10);... return w; }

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Calling the Function w = Volta(w, 10); ; push second arg AND R0, R0, #0 ADD R0, R0, #10 ADD R6, R6, #-1 STR R0, R6, #0 ; push first argument LDR R0, R5, #0 ADD R6, R6, #-1 STR R0, R6, #0 ; call subroutine JSR Volta q r w dyn link ret addr ret val a xFD00 new R6 Note: Caller needs to know number and type of arguments, doesn't know about local variables. R5 R6

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Starting the Callee Function ; leave space for return value ADD R6, R6, #-1 ; push return address ADD R6, R6, #-1 STR R7, R6, #0 ; push dyn link (callers frame ptr) ADD R6, R6, #-1 STR R5, R6, #0 ; set new frame pointer ADD R5, R6, #-1 ; allocate space for locals ADD R6, R6, #-2 m k dyn link ret addr ret val q r w dyn link ret addr ret val a xFCFB x xFD00 new R6 new R5 R6 R5

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Ending the Callee Function return k; ; copy k into return value LDR R0, R5, #0 STR R0, R5, #3 ; pop local variables ADD R6, R5, #1 ; pop dynamic link (into R5) LDR R5, R6, #0 ADD R6, R6, #1 ; pop return addr (into R7) LDR R7, R6, #0 ADD R6, R6, #1 ; return control to caller RET m k dyn link ret addr ret val q r w dyn link ret addr ret val a xFCFB x xFD00 R6 R5 new R6 new R5

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Resuming the Caller Function w = Volta(w,10); JSR Volta ; load return value (top of stack) LDR R0, R6, #0 ; perform assignment STR R0, R5, #0 ; pop return value ADD R6, R6, #1 ; pop arguments ADD R6, R6, #2 ret val q r w dyn link ret addr ret val a xFD00 R6 R5 new R6

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Summary of LC-3 Function Call Implementation 1.Caller pushes arguments (last to first). 2.Caller invokes subroutine (JSR). 3.Callee allocates return value, pushes R7 and R5. 4.Callee allocates space for local variables. 5.Callee executes function code. 6.Callee stores result into return value slot. 7.Callee pops local vars, pops R5, pops R7. 8.Callee returns (JMP R7). 9.Caller loads return value and pops arguments. 10.Caller resumes computation…