You are on page 1of 12

Modelling 2A

Lecture 5
Chapter 7

Philip Robinson
2017
Functions

• Functions are the building blocks of C programs.


• A function is simply a chunk of C code (statements) that you have
grouped together and given a name.
• The value of doing this is that you can use that "chunk" of code repeatedly
simply by writing its name.
• It is a subroutine which may be called from within other functions.
• A function can return a value, depending on how it is defined.
• Functions may be passed parameters.
• Functions should be declared before they are used, and must be defined
before the linking process links all the modules which constitute the
program.
• Functions can contain variables visible only to statements inside the body
of the specific function. This is called the SCOPE of the variable.
• A typical function definition has this form:
return_type name (argument declarations list)
2
{ function body }
Function Structure

• Two primary areas can be distinguished in the structure of a


function when its source code is inspected.

3
Functions

• The return type and argument declaration are optional, i.e for
no return type or arguments use type void.
• Variables other than arguments are declared within the body,
which is bounded by braces.
• The argument declaration list is a comma separated list of
variable declarations.
• Example of function structure
int diff (int x, int y)/* function header, name and
argument list */
{ //start of function
int z; //declare local variables
z = x – y;
return z;
}//end function body
4
Communication Values

• Arguments or Parameters are used to convey values from


the calling code to the function.
• If variables a and b have values 5 and 2, then the call
c = diff(a, b);
transmits 5 and 2 to the variables x and y.
• The values 5 and 2 are called actual arguments and the
variables x and y declared in diff() are called the formal
arguments.
– Formal Arguments are the variables declared as parameters in a
function header.
– Actual Arguments are the values that are passed to the function when
called by other code.
• The keyword return communicates one value from the
function back to the calling program.
5
• Functions must have the same type as the value they return.
Function Declarations

• Functions have to be declared before they can be used in


ANSI conforming compilers.
– As such most functions are declared above int main() {}

• If a function is used but not declared, the linker will give an


error message saying that an "unresolved external" was
found.

• A declaration makes the name and type of an identifier known


to the compiler and in the case of ANSI function prototypes,
also the number of parameters and the types of the
parameters.

6
Global, Local and Static Variables
Variable Scope

• Local variables are declared inside a function.


– These variables are created every time the function is called and are only
accessible form inside the scope of that function.
– These variables are destroyed when the function completes its execution.
– When a function is called multiple times a new local variable is created
EVERY time.
• Global variables are declared outside of a function and are
accessible throughout the entire program inside all the functions.
– If a local variable is declared with the same name as a global variable any
reference to that variable in the local scope will refer to the local variable and
NOT the global variable
• Static variables are local variables whose value will be persist
between separate calls of the function that contains them.
– These variables are still only accessible from the scope in which they are
declared but they will not be destroyed at the end of the function’s execution.
7
Function Parameters
Passing By Value vs By Reference

• By default parameters are passed by value to functions. This


means the value is copied to a new memory location created
for the parameter.
• This means that any modification of the parameter variables
inside the function do not affect the Actual Arguments
passed from the code calling the function.
• To obtain a value from a function, we use the function’s return
value. However this only allows for a single value to be
returned to the calling code.
• If you need to modify the value of one or more variables via
the parameters of a function you have to pass the address of
where the variable is stored in memory to the function.
– This is called passing by Reference as you are not just passing a value to
the function but an address of where a value is stored (a pointer).
8
• Functions cannot return arrays, only pointers to arrays.
Function Parameters
Passing By Value (default behaviour)

When Passing By Value the


values of the Actual
Arguments are copied into
the Formal Arguments.

Any operations that are


performed on the Formal
Arguments then use these
value but do not affect the
original Actual Arguments
in the calling code.

9
Function Parameters
Passing By Value vs By Reference

• If want to be able to modify the original variables passed as Actual


Arguments to a function we need to pass them by reference.
• Instead of making a copy of the values in the Arguments we will pass a
pointer to the argument, that is to say the address of where the original
variables are stored in memory.
• To do this the Formal Arguments are declared as pointers to indicate
that they hold an address to a value as opposed to an actual value. This
is done using the * operator.
• The Actual Arguments must then be the address of the variables being
passed and not the values in those variables. This is done using the &
operator which returns the address of a given variable.
• Finally to be able to work with the values stored at the locations pointed
to by the pointers we need to use the deferencing operator * in front of
the pointer variable
– Remember the deferencing operator will resolve a pointer which means to either fetch
the value stored at the location the pointer points to or to assign a value to the location 10
that a pointer points to.
Function Parameters
Passing By Reference (using pointers)

When passing By Reference the


Formal Arguments are declared
as pointers, meaning they hold the
ADDRESS where a value is
located in memory.
The Actual Arguments that are
passed are the addresses of the
values which is found using the &
operator.
Thus in the function the
Dereferencing operator is used to
turn the pointer address into the
value stored at that address.
Any modifications to the values
stored at the address will reflect in
the variables in the calling function
11
as they weren’t copied. x value has been modified!
exit() function

• The exit() function performs the same task as return in the


main() function
• The exit() function ends the program with the exit code specified
as an integer parameter
– exit(0);
• The main difference between the return and exit function is that
the exit function will end the program even if it is called from a
function that is not main() but a return statement in another
function will just return control back to the calling function.

12

You might also like