filmov
tv
C++ Programming Value-returning functions defined in Visual Studio Libraries, pow, squr, rand,srand

Показать описание
Function Definition
A function is a block of code that performs a task
Every C++ program contains at least one function (main), Most contain many functions
Some functions are built-in functions (part of C++): defined in language libraries
Others, called program-defined functions, are written by programmers; defined in a program
Functions allow for blocks of code to be used many times in a program without having to duplicate code
Functions also allow large, complex programs to be broken down into small, manageable sub-tasks
All value-returning functions perform a task and then return precisely one value
Typically, a statement that calls a function assigns the return value to a variable
–However, a return value could also be used in a comparison or calculation or could be printed to the screen
Built-in Value returning functions
POW function - The pow() function computes a base number raised to the power of exponent number.
A random number generator produces a sequence of numbers that meet certain statistical requirements for randomness. With the random number generator, the numbers have equal probability to be chosen from a finite set of number
rand() is a random number generator which returns an integer between 0 to 32,767 (inclusive)
Doesn’t require any actual arguments, but parentheses are still required
int num = rand(); will returns an integer between 0 to 32,767 ( Max Integer) . This number may not useful in programming.
Narrow down the range between lowerBound and UpperBound
The formula to produce random integers within a specific range is:
lowerBound + rand() % (upperBound – lowerBound + 1)
For example: to roll a die ( 1 to 6) lowerBound = 1, upperBound = 6
int number = lowerBound + rand() % (upperBound – lowerBound + 1);
int number = 1 + rand() % (6 – l1+ 1);
Function definition in Mathematics
A function is a relation for which each value from the set the first components of the ordered pairs is associated with exactly one value from the set of second components of the ordered pair.
We can use Doman and Range relation to separate a function and not a function
A function is a block of code that performs a task
Every C++ program contains at least one function (main), Most contain many functions
Some functions are built-in functions (part of C++): defined in language libraries
Others, called program-defined functions, are written by programmers; defined in a program
Functions allow for blocks of code to be used many times in a program without having to duplicate code
Functions also allow large, complex programs to be broken down into small, manageable sub-tasks
All value-returning functions perform a task and then return precisely one value
Typically, a statement that calls a function assigns the return value to a variable
–However, a return value could also be used in a comparison or calculation or could be printed to the screen
Built-in Value returning functions
POW function - The pow() function computes a base number raised to the power of exponent number.
A random number generator produces a sequence of numbers that meet certain statistical requirements for randomness. With the random number generator, the numbers have equal probability to be chosen from a finite set of number
rand() is a random number generator which returns an integer between 0 to 32,767 (inclusive)
Doesn’t require any actual arguments, but parentheses are still required
int num = rand(); will returns an integer between 0 to 32,767 ( Max Integer) . This number may not useful in programming.
Narrow down the range between lowerBound and UpperBound
The formula to produce random integers within a specific range is:
lowerBound + rand() % (upperBound – lowerBound + 1)
For example: to roll a die ( 1 to 6) lowerBound = 1, upperBound = 6
int number = lowerBound + rand() % (upperBound – lowerBound + 1);
int number = 1 + rand() % (6 – l1+ 1);
Function definition in Mathematics
A function is a relation for which each value from the set the first components of the ordered pairs is associated with exactly one value from the set of second components of the ordered pair.
We can use Doman and Range relation to separate a function and not a function
Комментарии