Functions and Headers Flashcards
arguments, return values, and function prototypes are all parts of a blank
function
multiple source files, the C preprocessor, include guards all require blank
headers
blank are named units of code that can be called upon to perform a task
functions
examples of functions include
getline( ) swap( ) strlen( ) pow( ) replace ( ) sort ( ) toupper( )
sometimes functions are called from an object such as .size( ) from a vector or string
these are member functions also called
methods some other methods are .begin( ) .end( ) .open( ) .close( ) .second( ) .first( )
to call a function, we supply it with the information that it needs, called blank
arguments
for example, pow( ) needs two arguments: base and exponent
blank supplied by listing them in order, separated by commas, in the parentheses after the function’s name
arguments
functions have the following general form
return_type function_name(argument_type 1 argument_name 1, ...) { //code body; }
the blank is any valid data type such as int or string
return type
the function’s blank follows the same rules as variable’s blank
name(s)
the blank is a comma-separated list of type and variable names
arguments list
a blank is a value handed back from a function to be used where the function was called
return value
a function call is effectively replaced by its blank
return value
cout «_space;repeat_string(“Hello! “,5);
What does this mean?
cout «_space;“Hello! Hello! Hello! Hello! Hello! “;
functions make code blank
reusable
more organized
more readable
what happens if you put the other functions in your program below main instead of above main?
the program won’t compile because main( ) can’t see the other functions
blank has to be declared above where we use it
a variable or a function
to have the ability to place functions below main( ) and still have main( ) recognize them, use blank above the main( ) functions
function prototypes
vector common_letters(string a, string b):
What is common_letters
’s return type and what does it accept as arguments?
return type: vector of char
arguments: two strings
having multiple source files helps keep your programs blank
clean and easy to read
it is important to prototype functions in blank source files, because functions might call each other
all
blank uses the .hpp extension instead of the .cpp extension
header files