functions-summer

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

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

Functions in C++ Top Down Design with Functions

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

View Full Document
Top-down Design Big picture first broken down into smaller pieces
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; }

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

View Full Document
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
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?

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

View Full Document
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?
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?

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

View Full Document
Function prototype example: float myfun (float one, float two); // assumes cmath has been included
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!

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

View Full Document
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
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!

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

View Full Document
Value-returning Function Semantics A function is a control structure so how does it change the order of execution?
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
Ask a homework question - tutors are online