PREDEFINED AND USER-DEFINED FUNCTIONS Flashcards
these are like building blocks
functions
they allow complicated programs to be divided into manageable piecesq
functions
4 advantages of functions
- a programmer can focus on just that part of the program and construct it, debug it, and perfect it
- different people can work on different functions simultaneously
- can be reused (even in different programs)
- enhance program readability
functions are also called as these
modules
functions are like _________________
miniature programs
these can be put together to form a larger program
functions
in algebra, a function is defined as a rule or correspondence between values, called the function’s __________
arguments
predefined functions are organized into separate ______________
libraries
i/o functions are in the _________ header, whereas math functions are in the __________ header
iostream, cmath
these functions have a return type
value-returning functions
they return a value of a specific data type using the return statement
value-returning functions
value-returning functions return a value of a specific data type using the _______ statement
return
these functions do not have a return type; they do not use a return statement to return a value
void functions
to use value-returning functions, you must ________________________________
include the appropriate header file in your program using the include statement
to use value -returning functions, you must know the following items:
- name of the function
- number of parameters, if any
- data type of each parameter
- data type of the value returned (called the type of the function)
TRUE OR FALSE: the value returned by a value-returning function is unique
true
a value-returning function is used in an _________ or in an __________
assignment, output statement
the first 4 properties of a value-returning function
heading
what type of function is this:
int abs(int number);
variable declared in the heading of a value-returning function
formal parameter
identify the formal parameter in this example:
int abs(int num)
num
variable or expression listed in a call to a value-returning function
actual parameter
in value-returning functions, the function type can also be called as these
data type or return type
syntax for a value-returning function
functionType functionName (formal parameters)
{
statements
}
syntax for a formal parameter list
(dataType identifier, dataType identifier, …)
syntax for calling a value-returning function
functionName(actual parameters);
syntax for actual parameter list
(expression or variable, expression or variable, …);
TRUE OR FALSE: a formal parameter list can be empty (no passing parameter)
true
once a value-returning function computes the value, the function returns the value via the ______ statement
return
return statement syntax
return expression;
TRUE OR FALSE: in c++, return is not a reserved word
false
when a return statement executes…
- function immediately terminates
- control goes back to the caller
what happens when a return statement executes in the function main
the program terminates
function heading without the body of the function
function prototype
syntax of a function prototype
functionType functionName (parameter list);
TRUE OR FALSE: in a function prototype, it is necessary to specify the variable name in the parameter list
false
TRUE OR FALSE: in a function prototype, the data type of each parameter must be specified
true
execution always begins at……
the first statement in the function main
when are other functions outside main executed
only when they are called
these appear before any function definition because the compiler translates these first
function prototypes
this results in transfer of control to the first statement in the body of the called function
a function call
after the last statement of a function is executed, control is….
passed back to the point immediately following the function call
after executing the function, the returned value…..
replaces the function call statement
TRUE OR FALSE: user-defined void functions can only be placed before the function main
false
these have similar structures with value-returning functions; both have a heading part and a statement part
void functions
if a user-defined void function is placed after main, …
the function prototype must be placed before the function main
this does not have a return type
void function
in void functions, this is typically used to exit the function early
return statement without any value
in void functions, these are optional
formal parameters
a call to a void function is a __________
stand-alone statement
void function definition syntax
void functionName(formal parameters)
{
statements
}
void function call syntax
functionName(actual parameters);
in void functions, it is a formal parameter that receives a copy of the content of the corresponding actual parameter
value parameter
in void functions, it is a formal parameter that receives the location (memory address) of the corresponding actual parameter
reference parameter
in void functions, this happens when a formal parameter is a value parameter
the value of the corresponding actual parameter is copied into it
during program execution, this manipulates the data stored in its own memory space
value parameter
in the case of a reference parameter, …
the address of the actual parameter passes to the formal parameter
this is the content of a formal parameter
address
TRUE OR FALSE: during execution, changes made by the formal parameter permanently change the value of the actual parameter
true
stream variables (such as ifstream) should be _______________ to a function
passed by reference
TRUE OR FALSE: you cannot use reference parameters in a value-returning function
false
by definition, a value-returning function returns _________ value
a single
if a function needs to return more than one value, you should change it to a ________________ and use the appropriate reference parameters to return the values
void function
this refers to where in the program an identifier is accessible
scope of an identifier
these are identifiers declared within a function (or block)
local identifier
these are identifiers declared outside of every function definition
global identifier
TRUE OR FALSE: c++ allows nested functions
false
in a c++ program, several functions can have the same name, this is called ____________________ or ____________________
function overloading, overloading a function name
2 functions are said to have ______________________ if both functions have:
- a different number of formal parameters, or
- if the number of formal parameters is the same, then the data type of the formal parameters, in the order you list them, must differ in at least one position
different formal parameter list
creating several functions with the same name
function overloading
this consists of the function name and its formal parameter list
the signature of a function
two functions have different signatures if …?
either different names or different formal parameter lists