Lecture #16: Functions
November
8, 1999
Bob
Pitha
whats a function?
A
group of declarations and statements that is assigned a name
effectively,
a named statement block
A
sub-program
we
write our program by writing a function named main
inside which
we can use other functions
which can
themselves use other functions, and so on
why use functions?
They
can break your problem down into smaller sub-tasks
easier to
solve complex problems
They
make a program much easier to read
abstraction
like printf,
we just have to know what a function does without worry about the details of
how it works
Generalize
a repeated set of instructions
we dont
have to keep writing the same thing over and over
characteristics of functions
Name
Arguments
Body
Return
value
function name
Is
an identifier
This
is how you refer to the function elsewhere in the program
function arguments
These
are variables whose values you specify every time you invoke the function
so they can
have different values each time
function body
These
are the declarations and statements that actually do the work
Specified
in the function definition
return value
A
function can optionally return a value.
This
value becomes the value of the function call expression
If a function doesnt return a value, the return type
should be void
function declaration
Just
as with variables, functions can be declared
The
declaration contains:
the function
name
the data
types of the arguments
the data
type of the return value
function declaration vs. definition
The
definition is the actual function
it has the
function name
it has the
argument declarations
it specifies
the return type
it has the
statements that comprise the function body
The
declaration is just a reference to the function
it has the
function name
it has the
argument data types
it specifies
the return type
it has no
code
no
statements or declarations
what does the function declaration do?
it
enables you to call a function without having to know the definition
remember
when we used printf?
we didnt
know how the function worked
but the
compiler needed to know that printf was actually a function
the file
stdio.h contains a declaration for printf
function definition is a declaration
You
need to declare a function before you can call it
at least you
do in order to adhere completely to the ANSI standard
You
can declare the function by:
including a function prototype
including
the function definition
function prototype
This
is a fancy name for a function declaration that isnt the function definition
It
must include:
the return
type
the function
name
the data
types of the arguments in parentheses
optionally
with names, but theyre ignored
and a
semicolon after the right parenthesis
an analogy
When
you buy a TV, you get a users manual
that manual
tells you how to change channels
in case
youve been living in a cave for the past 50 years
once youve
read the manual, I can tell you, change to channel 7 and you know what to do
or I can
say, change to channel 4
In
this analogy:
change the
channel is the function
the channel
number is the argument
another analogy
You
know (or can learn) how to calculate the geometric area of an ellipse
given the
major and minor axes
Once
you know, all I have to do is give you the two values, and you give me back a
number
In
this analogy:
calculate
the area of an ellipse is the function
the lengths
of the 2 axes are the arguments
the area is
the return value
function example

what does this function do?

It
takes a single argument
an int
It
returns the value of that argument multiplied by two
also an int
how might we call this function?

note this
Variable
names
inside
MultByTwo, the argument is named x
when we call
it, we pass a variable named i
there is no
correspondence between the name of what we pass in and what we call it inside
the function
function call expression
We
now have a new expression: the function call
the
expression is evaluated by executing the statements in the function
the value of
the function call expression is determined by the first return statement
encountered in the function
function call expression example
So
given the code:
i
= MultByTwo (37) + 4;
the
statement is an assignment expression statement
operator
is =
operands
are i
and the expression MultByTwo(37)
+ 4
the
sub-expression is an arithmetic expression
operator
is +
operands
are the expression MultByTwo(37)
and 4
MultByTwo(37)
is an expression
it
is evaluated by calling the function MultByTwo, with the argument 37
function call expression example continued
When
the computer evaluates the expression
MultByTwo (37)
it invokes
the function:

function call

The
local variable x is set to the value of the argument
which in
this case was a constant, 37
retval is
set to 74
the value 74
is returned, which means the value of the expression MultByTwo(37) becomes 74
expression evaluation
i
= MultByTwo (37) + 4;
So
the value of the expression
MultByTwo (37) + 4
becomes 78
And
this is what i gets set to
return statement
This
is the statement that causes the function to return a value
it must
return a value that can be converted into the data type specified as the
function return value
A
function may have any number of return statements, but the first one
encountered will end the execution of the function and return the value
return statement example

function arguments
Each
argument declares a local variable that is only accessible from inside the
function
its value is
set using the value that is specified in the call
it ceases to
exist after the return statement is executed
usually
Important
concept here:
the local
variable inside the function is totally distinct from the variable whose value
was used to call the function
so if the
function changes the value of one of its arguments, it doesnt affect the
variable used to call the function
pass-by-value
This
is the term that means that function arguments are passed into the function by
copying their values
rather than
giving the function direct access to the actual variables

so if I call
this function like this:
a = AddOne (b);
it does not
change the value of b