7- predefined functions (library functions) Flashcards
Function
named group of statements carrying out a particular task that accepts values and computes the result
Function name
identifier distinguishing the function from others
example – square root function: sqrt
argument
the value accepted by the function to use in computation
return value
value computed by the function
function call (function invocation)
executing function code from elsewhere in program
syntax: expression consisting of function name followed by arguments in parentheses
result = sqrt(9.0);
semantics: argument is evaluated, function is executed, return value replaces function invocation
invocation forms
in expression – return value replaces invocation in expression evaluation
result = sqrt(9.0) – myVar5;
standalone – return value is ignored
srand(55);
argument variants
function may have more than one argument
result = pow(myVar, 55);
an argument is an (arbitrary) expression
result = sqrt(myVar2 + 9.0);
nested function call: use of one function call as argument to another
result = sqrt(abs(myVar));
casting
explicit type conversion
coercion
implicit type conversion
Random number generation
(pseudo) random number generation pre-defined functions are used to create events unpredictable by user (e.g. events in games)
need to include <cstdlib>
generates a series of numbers, numbers within single series are (pseudo) random</cstdlib>
srand(seed)
initializes random number generator, needs to be invoked once in the program, no return value
seed
integer, selects the (pseudo) random series
rand()
returns a new integer random number in the series, can be used multiple times in program, no argument
time(nullptr)
returns number of seconds since 01/01/1970, good for initializing unique series, needs <ctime>
nullptr is there for historical reasons</ctime>
srand(1);
repeats series every time you run your program – good for debugging
srand(time(nullptr));
selects unpredictable series every time you run your program – good for final compilation