Sie sind auf Seite 1von 22

PIC 10A

Lecture 15 Functions parameters

return_type function_name(parameter1, parameter2,


, parametern) {
// code, pretty much independent of everything
else.
return return_type_variable;
}

Parts of a function
1.

return_type is something like double, int, char, bool. It can


also be void, which does not return a value (more on this
later). There can only be ONE return type.

2.

The function_name follows the same rules as variable


names. Make sure the function name is not a reserved
word, and it should be descriptive.

3.

The parameters can be any data type or combination of


data types, excluding void. If using void, it must be the
only parameter.

4.

The return_type_variable is the value to return, or output.


Only a single variable can be output.

Function Declarations
Recall

that you can define a function after main function, but


you need to declare it before main.

If

there are multiple functions, and functions calling other


functions, they should be listed in reverse order in which
theyre called.

This

can get tricky for complex programs. One way around


this is to declare your functions right after the namespace
line.

Function Declarations

Or

Function Declarations
In

declaration, it could

has

no variable name specified inside parentheses


has a variable name specified for each parameter inside
parentheses
has a variable name different to the one used in the
definition of functions

Function Name
The

function name should be descriptive.

If

your function is best described using multiple words I


suggest following the convention that the first word is
lowercase and all subsequent words are uppercase.

You

can also separate words by _ underscores, but this is


becoming less common.

Function overload
In

C++, two different functions can have the same name if


their parameters are different; either because they have a
different number of parameters, or because any of their
parameters are of a different type.

Function overload
Another

example

Function overload
Remember

cin.ignore() ?

cin.ignore(void)
cin.ignore(int)
cin.ignore(int,

char)

Function overload
Note:

cannot overload functions distinguished by return type


alone!

Modularization
A

function is like a completely separate program. In


other words, aside from the input parameters, a
function does not generally know or have access to
the variables defined in main and other functions.

This

is actually a good thing, because we dont have


to worry about using the name variable name
multiple times.

There

are of course exceptions, like global variables,


but we will see that good style is to make things are
compartmentalized as possible.

Separation of variables
In

general, the x in
cube is just a dummy
variable, which has
nothing to do with the
variable names in
main or in any other
function.

This

is part of the
reason why we
declare cube above
without a variable
name, just the data
types. It is because
the variable names
are immaterial, and

double cube(double);
int main() {
// These variable names in main dont care
about cube
double x=2.0;
cout<<cube(x);
}
/** Finds the cube
@param x is the number
@returns the cube of x, i.e., x*x*x or x^3
*/
double cube(double x) {
// The variable x is completely independent of
main
return x*x*x;
}

Separation of variables
Even

though I
changed the dummy
variable from x to y,
this is still the exact
same program.

double cube(double);
int main() {
// These variable names in main dont care
about cube
double x=2.0;
cout<<cube(x);
}
/** Finds the cube
@param x is the number
@returns the cube of x, i.e., x*x*x or x^3
*/
double cube(double y) {
// The variable x is completely independent of
main
return y*y*y;
}

Side Effects
It

is a general design principle that a function had best leave


no trace of its existence except for returning a value.

The

function should not have any visible side effect.

Things
This

like cout statements are generally left to main( ).

makes the function more re-useable.

Procedure
There

are also functions that


just run a set of code and
dont return a value. For
example, suppose I print a
welcome message
customized for a name

procedure is any function


that does NOT return a
value. It can have as many
parameters as desired, but
its return type is void, and
rather than returning a
variable with a value we
simply write return;n f

the

return; statement is

void Welcome(string);
int main { }
/** */
void Welcome(string name) {
cout<<Welcome
<<name<<!\n;
return;
}

Predicate
A

predicate is a function that returns a bool.

bool isPositive(double x);


bool validInput(int n);
bool isThatYourFinalAnswer(string answer);
Predicates

are very useful in algorithms, since they can


transform a statement like
if(sqrt(x*x+y*y)<=radius))

into

if(insideCircle(x,y))

All roads lead to return;


Another

reason to
include the return; in a
function is to terminate
it early.

/** Prints some statements. Terminates early


if n=10.
@param n is the input number
*/
void printStatements(int n) {

Also

cout<<Well hi there\n;
if(n==10) {
cout<<You are special\n;
return;
}
cout<<This is not going to print if 10 is
input\n;
cout<<I wish I was special :(;
cout<<So very special ... \n;
return;
}

note the
comments do not have
@return because there
is no value returned.

All roads lead to return


All

functions need to return.

/** */
int sign(double x) {
if(x>0) return 1;
if(x<0) return -1;
// Error. The case x == 0 is not
covered.
}

All roads lead to return


All

functions need to return.

/** */
int sign(double x) {
if(x>0) return 1;
if(x<0) return -1;
// The case x == 0 is now covered.
return 0;
}

Example: Factorization
We

want to write a program that decomposes a number into


its prime factors, sorted in order from smallest to biggest.
Notice we have to count repetitions.

15 = 3*5
24 = 2*2*2*3
It

would help if we had a function that returned the smallest


divisor (>1) of a number.

Example: Factorization

This

function computes the smallest divisor (>1) of a number.

Returns

the original number if no divisor found: the smallest


divisor of a prime number is itself.

Das könnte Ihnen auch gefallen