Presentation is loading. Please wait.

Presentation is loading. Please wait.

CMPT 201 Functions.

Similar presentations


Presentation on theme: "CMPT 201 Functions."— Presentation transcript:

1 CMPT 201 Functions

2 Top Down Design (Divide and Conquer)
Break the algorithm into subtasks Subtasks, or functions in C++, make programs Easier to understand Easier to change Easier to write Easier to test Easier to debug Easier for teams to develop

3 Predefined Functions C++ comes with libraries of predefined functions
Example: sqrt function #include <cmath> root = sqrt(9.0); The number, 9, is called the argument returns, or computes, the square root of a number

4 pow() #include <cmath>
cout<<“2.5 to the power 3.0 is “<< pow(2.5, 3.0); The sequence of the two arguments matters!

5

6 Random Number Generation
Really pseudo-random numbers Ri = (Ri-1 * 7) % 11 1. Seed the random number generator only once #include <cstdlib> #include <ctime> srand(time(0)); 2. The rand() function returns a random integer that is greater than or equal to 0 and less than RAND_MAX (32767);

7 Random Numbers Use % and + to scale to the number range you want
Generate a random number from 0-99 int num = rand() % 100; Generate a random number from 1-6 int die = (rand() % 6) + 1; Generating a random number x where 10<=x<=20?

8 Revisit: Guess a number
Generate a random lucky number between 1 and 10

9 User-Defined Functions
Function declaration Type Function_Name(Type Parameter); int product(int x, int y);

10 User-Defined Functions
Function definition Type Function_Name(Type Parameter) { //code } int product(int x, int y) // function header { int result = x * y; return result; }

11 The Function Call int main() { int number = product (5, 6); ... }
int product(int x, int y) int result = x * y; return result;

12 Note When a return statement is executed, the function call ends.

13 Exercise (Multiple returns with if-else)
Build our own my_abs() function.

14 Functions that Return a Boolean Value
bool isPrime (int number); //returns true if number is a prime number, false otherwise Nested loops: Consider using function calls (for inner loop) to make the program more readable

15 Note You can place the whole function definition before the main.
Procedural Abstraction If a function is well designed, the programmer can use the function as if it were a black box. char grade (int score) // return a student’s grade based on his/her score

16 Local Variables vs. Global Variables
Are declared in a function Cannot be used from outside the function Have the function as their scope Global variables Available to all functions (including main) Declared outside any function body e.g., Global named constant

17

18 Call-by-value int square (int n) { n = n * n; return n; } int main () int number = 10; int result = square(number); cout<<result<<“ “<<number;

19 Overloaded Functions C++ allows more than one definition for the same function name different numbers of arguments different types of arguments double avg(double n1, double n2) { return ((n1 + n2) / 2); } double avg(double n1, double n2, double n3) { return (( n1 + n2 + n3) / 3); }

20 Overloaded Functions cout << avg( 10, 20, 30);
double avg(double n1, double n2) { return ((n1 + n2) / 2); } double avg(double n1, double n2, double n3) { return (( n1 + n2 + n3) / 3); } Which one are we calling? cout << avg( 10, 20); cout << avg( 10, 20, 30);

21 To summarize.. Overloaded functions
Must have different numbers of formal parameters AND / OR Must have at least one different type of parameter


Download ppt "CMPT 201 Functions."

Similar presentations


Ads by Google