Presentation is loading. Please wait.

Presentation is loading. Please wait.

Single-Result Functions & Modularity

Similar presentations


Presentation on theme: "Single-Result Functions & Modularity"— Presentation transcript:

1 Single-Result Functions & Modularity
Section 10/28/13

2 Reading Read pages of Chapter 5

3 Functions For Modularity 5.1
Complex problems can be broken down into sub tasks, each of which is easy to implement in C++. Advantages to using programmer defined functions and types, as opposed to writing the entire solution in main(): Multiple programmers Testing/Debugging/Maintaining Reduce duplication of code 3

4 Functions Have discussed math library functions
Example in ch5/pendulum.cpp sqrt( )is a black box – user not aware of details Can create your own functions

5 Example ch5/circle.cpp Function call Function definition
Has a function to find the area of a circle. Function call name and arguments Function definition Describes how function computes the value it returns Has a heading and a body

6 Single-Result Function Definition Syntax
// Comment w/ purpose and preconditions resultType functionName( parameter list) // header { // function body Declarations of local variables Statements to find a result Return statement to send back result }

7 #include <iostream>
Using namespace std; //maximum finds the maximum of //three integers, x, y, z //Pre: x, y, and z must be set int maximum(int x, int y, int z)//HEADER { //BODY int max = x; if (y > max)‏ max = y; if (z > max)‏ max = z; return max; }

8 //main program with function call
int main(){ int a,b,c; cout << "Enter three numbers>"; cin >> a >> b >> c; cout << "Max is " << maximum(a,b,c)//call with <<endl; //3 arguments return 0; }

9 Arguments and Formal Parameters
Arguments are values sent to function Parameters (formal parameters) are dummy variables that receive the information

10 Arguments and Parameters
Must match in type and number a,b,c & x,y,z three ints Paired in the order that they appear

11 Local variables double circleArea(double r) { double a;
a = PI * pow(r,2.0); return a; } Variable declared in a function. a is a local variable invisible to main program

12 return statement double circleArea(double r) { double a;
a = PI * pow(r,2.0); return a; } return a Sends value back to call

13 What is the purpose of a function?
What is the difference between an argument and a parameter? What is a global variable?

14 LetterGrade( ) Write an algorithm for a function that takes an exam score and returns a letter grade. Write the function definition Write a main program to test the function. Called a driver

15 Function Write a function that takes a date of birth (mo, da, yr) and today's date(mo, da, yr) and returns a person's age in years. Disregard extra months and days. birthday.cpp has program. We need to add the function. Now let's put some error checking in the main function.

16 Boolean Function Returns a bool Can be used in an if statement example
ch5/factor.cpp

17 Boolean Function Returns a bool Can be used in an if statement example
ch5/factor.cpp

18 Write a Boolean Function
Write a bool function isVowel() that takes a character as an argument. It should return true if it is a vowel, false if it isn't. vowel.cpp

19 What is the output? #include <iostream> using namespace std;
int mystery(int x){ int y = x*x; return y; } int main(){ int a = 5; cout << mystery(a) << endl; return 0;


Download ppt "Single-Result Functions & Modularity"

Similar presentations


Ads by Google