Function Review

advertisement
Function Review
CS1210
 What Are Functions? Functions are named, parameterized blocks of code.
 Function Syntax:
a. Prototype: returntype functionname (parameter types or parameter list);
i. Examples: float computeArea(float, float);
int getNumber(string prompt);
string reverse(string);
void write(string output);
void getPoint(float &, float &);
b. Definition: returntype functionname (parameter list) { statements }
c. Invocation (or calling the function): variable = functionname(parameters);
or functionname(parameters);
i. Examples: area = computeArea(len, width);
getNumber(“Please enter a number”);
reversedString = reverse(inputString);
write(outputString);
getPoint(x, y);
d. Rules: Either the function’s definition or prototype must precede the first
call to the function.
i. Prototypes may either be included or explicitly listed at the
beginning of your code.
 Formal and Actual Parameters:
a. Formal parameters: The parameters that appear in the definition of the
function are called formal parameters. Formal parameters must be
variables.
b. Actual parameters: The parameters that appear when the function is
invoked are called actual parameters. Actual parameters can be either
variables or literals.
c. Relationship: When a function is invoked the actual parameters are
associated with the formal parameters by position (i.e., the first actual
parameter is associated with the first formal parameter, the second actual
parameter is associated with the second formal parameter, etc.). What
happens with this association depends on whether the parameter is a value
parameter or a reference parameter (see below).
 Value and Reference Parameter Types:
a. Value Parameters: The default parameter type in C++ (except for arrays)
is the value parameter. With value parameters the actual parameter value
is copied into the formal parameter, so when (or if) the function changes
the formal parameter it does not change the actual parameter, because it is
using a copy of the actual parameter.
b. Reference Parameters: A reference parameter is designated a reference
parameter by preceding it with a ‘&’ character. With reference parameters
the formal parameter refers to the actual parameter, so when (or if) the
function changes the formal parameter it changes the actual parameter,
because with reference parameters the actual and formal parameters are
the same variable (just with different names).
 Default Parameters: Default parameters are a way to give standard or default
values to parameters.
a. Example: The function prototype int add(int x, int y = 1); gives a default
value of one to the parameter y whenever the function invocation does not
specify a value.
i. z = add(3); assigns 4 to z, because 3+1=4.
ii. z = add(4,2); assigns 6 to z, because 4+2=6 (in this case y gets
value 2, because y was specified).
 Constant Parameters: Constant parameters are a way to get the efficiency of
reference parameters (since they are not copied) with the safety of value
parameters (since they cannot be changed).
a. Example: The function prototype int length(const int a[]); passes the
integer array a efficiently (does not make a copy), but a cannot be changed
in the function, because it is constant.
 Name Overloading: When we use the same name for two (or more) different
functions, we are said to be overloading the name. The compiler determines
which function we want by looking at the number and type of the parameters.
a. Example: Given the function prototypes: void swap(int &x, int &y); to
swap two integers and void swap(float &x, float &y); to swap two floats.
i. swap(a, b); calls the integer version of swap, if a and b are ints.
ii. swap(a, b); calls the float version of swap, if a and b are floats.
iii. swap(a, b); generates an error, if a is an int and b is a float,
because the compiler doesn’t know which function to call.
Download