9 + 10. Functions Flashcards
function declaration
- declare functions before use
- incl all info needed to use function
- provide comment w/ each function declaration
info needed to use function
function data type function name parameters with their data types
function definition
- consists of declaration + function body
- body: computes function value + returns a value unless it is a void function
what functions can return
doubles, ints, chars, strings, booleans, etc
boolean function
useful for abstracting complex abstractions
parameters
in the declaration, the info inside () are the formal parameters of the function
arguments
when calling a function, the values put into the () are the arguments passsed to the function
zero parameters
eg. int f(); double g();
when to use a function
- when task is easy to identify but hard to implement code
- whenever you have to write same code or v. similar code a second time (when code can be repeated)
pass by reference (call-by-reference)
use of &, changes the argument
pass by value (call-by-value)
no use of &, does not change the argument
void functions
functions that dont return anything
void functions syntax
void function_name (parameters);
need for call-by-reference parameters
for when we want a function to:
- read in a bunch of data eg. read payroll details
- return multiple values eg. day, month, year
- change 2 values in tandem eg. simplify fraction
call-by-reference parameters
- can update storage places
- instead of passing value into function, pass a reference to a place where value is stored
- use ampersand (&)