Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functions October 23, 2017.

Similar presentations


Presentation on theme: "Functions October 23, 2017."— Presentation transcript:

1 Functions October 23, 2017

2 Top-Down Design Top-down design refers to designing a program by breaking down the end result into a series of smaller subtasks, then breaking those down further, and so on. The final subtasks should be trivial to implement (e.g., incrementing a variable). In C++, these subparts are known as functions. Other languages may refer to them as procedures or methods.

3 Predefined Functions C++ includes a number of predefined functions. The following functions are found in the cmath library: sqrt(x) computes the square root of x (which can be an int or double) pow(x, y) raises x to the power of y abs(x) computes the absolute value of x ceil(x) computes the first whole number greater than or equal to x ceil(2.1) == 3 floor(x) computes the first whole number less than or equal to x floor(2.9) == 2

4 Predefined Functions Consider the following code:
root = sqrt(9); The value in parentheses is called the argument of the function. The value the function computes (in this case, 3) is called the value returned. The expression sqrt(9) is called a function call. Not all predefined functions take arguments. The rand() function (which computes a random number between 0 and 231 – 1) is one such function.

5 Random Number Generation
N.B. On the AP exam, the pseudocode RANDOM(x, y) generates a random number between x and y. For instance, RANDOM(0, 5) generates a random number between 0 and 5.


Download ppt "Functions October 23, 2017."

Similar presentations


Ads by Google