functions-summer

functions-summer - Functions in C+ Top Down Design with...

Info iconThis preview shows pages 1–13. Sign up to view the full content.

View Full Document Right Arrow Icon
Functions in C++ Top Down Design with Functions
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Top-down Design Big picture first broken down into smaller pieces
Background image of page 2
Can you tell what this is doing? int main () { rad = get_a_number(); display_input (rad); pie_area = find_area (rad); pie_cost = calccost (pie_area); draw_pie (rad, pie_cost); return 0; }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Why use functions? easier for programmers to work together put details off while looking at big picture easier to reuse code easier testing and debugging
Background image of page 4
Suppose you had some code that looked like this: side2 = pow(a, 2) + pow (b, 2); side4 = pow(c, 2) + pow (side2, 2); side7 = pow(b, 2) + pow (side4, 2); side3 = pow(5.3, 2) + pow (side2, 2); cout << pow (19.2, 2) + pow(angle3, 2) + angle9; why not abstract that expression and only have to write it once?
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Function definition example //definition float myfun (float one, float two) // assumes //cmath has been included { return pow(one, 2) + pow (two, 2); } could you name the parameters?
Background image of page 6
Function call examples: // the code before becomes side2 = myfun (a, b); side4 = myfun (c, side2); side7 = myfun (b, side4); side3 = myfun (5.3, side2); cout << myfun (19.2, angle3) + angle9; could you name the arguments?
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Function prototype example: float myfun (float one, float two); // assumes cmath has been included
Background image of page 8
Value-returning Function Syntax The Definition has header "return type" "name" ( parameter list ) has body - must be between braces body must have return x; where x is a constant or variable or expression location - anywhere in file but NOT nested in another function's body!
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Value-returning Function Syntax The function call is an expression using " name ( argument list) " since the call is an expression, it must be part of a larger statement: output assignment statement (RHS) if statement while statement location of a call - wherever needed in code
Background image of page 10
Value-returning Function Syntax The Prototype just like header except ends with semicolon "float funA (int a, float b);" location near top of file prototypes go OUTSIDE of any function definitions! (not inside a function) parameter names are optional but a good idea! "int myfun (int, int, int);" is mysterious!
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Value-returning Function Semantics A function is a control structure so how does it change the order of execution?
Background image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/13/2011 for the course CS 115 taught by Professor Staff during the Fall '08 term at Kentucky.

Page1 / 34

functions-summer - Functions in C+ Top Down Design with...

This preview shows document pages 1 - 13. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online