Introduction to Functions
Lecture objectives
At the end of this lecture you should be able to:
• Definitions the following terms:
– A function, function prototype, function definition, function call.
• Explain a-tleast three the advantages of using functions.
• Give the syntax ( general form) of a function
declaration(prototype),function definition and function call in C.
• Explain the Scope rules of using variables in functions.
• Passing data to functions(arguments).
• Calling functions using( call by value or call by reference)
• Explain recursion and implement.
• Return arrays to functions.
Monday, June 9, 2025 By Mr C . ZANO
Functions
Can you define the term function. what is it?
A function is a self-contained block of statements that performs
a specific coherent task. Eg addition, sorting etc
Using a function is something like hiring a person to do a
specific job for you.
The person will report back the results of the task.
As well a function should return results of its operation to the
main program.
– You’ve used predefined functions already:
• E.g. main(),printf(), scanf(),
The art of writing programs with functions is known as modular
programming.
Monday, June 9, 2025 By Mr C . ZANO
Advantages of Functions
Simplification of programs
• A large single list of instructions (Monolethic program ) is
difficult to understand.
• functions are used for better understanding of programs
since a function has a clearly defined objective (purpose)
and a clearly defined interface with other functions in the
program.
Reduction in program size
• The functions code is stored in only one place in memory,
even though it may be executed as many times as a user
needs.
Monday, June 9, 2025 By Mr C . ZANO
Advantages of Functions
Reusability of code
• The same function can be executed many times.
• There are a number of predefined functions eg clrscr(). The programmer
needs to be concerned only with what the function does but not how it
does it.
• In complex software systems, this principle of separating what from the
how is an important aspect of managing the complexity of programs.
Division of labor
• Several programmers work together in teams on the same project.
Maintainable code
• We can change the modules easily because they are relatively small and
stand alone. Thus modular programs improve program portability.
Monday, June 9, 2025 By Mr C . ZANO
Declaring a Functions
• A Function declaration is also called a function prototype.
• It indicates the name of the function, its return type and its
parameters.
• The syntax or general form of a function prototype is
• return-type function-name (argument-list);
• Nb the declaration statement is terminated by a semi-
colony.
• Each declaration should clearly specifies the number, order
and type of arguments each function is going to receive
during a call as well as the type of the value it would
return.
Monday, June 9, 2025 By Mr C . ZANO
Declaring a Functions
• The compiler uses the prototype to ensure that the types
of actual arguments passed in a function call corresponds
to the formal arguments in the function definition.
• A prototype describes the function interface to the
compiler by giving details of the number and type of
arguments and return type of values.
• No function can be called unless its prototype is available
or has been defined first.
• Example int squareprimeter (int side);
• float CircleArea (int Radius);
Monday, June 9, 2025 By Mr C . ZANO
Defining a Functions
• A Function definition gives the body or code of the function.
• The general form of a function definition
• return-type function-name (argument-list)
{
statements;
…..
}
• Nb there is no semi-colony after the parameter list.
• Argument list is a comma separated list of variables of a function through
which the function may receive data or send data when called from other
function.
• All C/C++ programs must have at least one function, the main() function.
Monday, June 9, 2025 By Mr C . ZANO
Passing Arguments
• The mechanism used to convey information to
the function is the argument.
• We pass arguments to functions while calling
them.
• Arguments are sometimes called parameters.
• The parameters passed to the function during a
call are called actual parameters and those
given in a function definition are called formal
parameters.
Monday, June 9, 2025 By Mr C . ZANO
Calling a Functions
• A Function call consists of a function name followed by a pair of
parenthesis.
• If the function takes parameters, they are passed within the pair of
parenthesis in the same sequence mentioned in the prototype of
the function.
• The syntax or general form of a function call is
• function-name (argument-list);
• Nb there is no return type but is terminated by a semi-colony.
• When a compiler encounters a function call, control is passed to the
function definition for its execution.
• functions with arguments can be invoked a call by value or reference
Monday, June 9, 2025 By Mr C . ZANO
Call by value
• In this method the values of the actual parameters
(appearing in the function call) are copied into the
formal parameters (appearing in the function
definition),
• i.e., the function creates its own copy of argument
values and operates on them.
• The creation of a separate set of variables
consumes more memory space.
• The function does not have access to the actual
variables.
Monday, June 9, 2025 By Mr C . ZANO
Call by reference
• A reference provides an alias – an alternate name – for the variable
• i.e., the same variable’s value can be used by two different names : the
original name and the alias name.
• In call by reference method, a reference to the actual arguments(s) in the
calling program is passed (only variables).
• So the called function does not create its own copy of original value(s) but
works with the original value(s) with different name.
• Any change in the original data in the called function gets reflected back to
the calling function.
• It is useful when you want to change the original variables in the calling
function by the called function.
• If original variables should not change, constant augments should be used.
Declare parameters preceded by the key word const.
• E.g float Area(const float Pi);
Monday, June 9, 2025 By Mr C . ZANO
Passing Arrays to functions
• To pass an array to a function, specify the name of the array
without any brackets.
• E.g. int hourlytemperatures[24];
• Declares an interger array hourlytemperatures.
• To pass this to a function called modifyarray, we have the
function call statement as follows
modifyarray(hourlytemperatures, 24);
• C automatically passes arrays to functions using simulated call
by reference. The name of the array is actually the address of
the first element of the array.
• However individual array elements are passed using call by
value just like simple variables.
Monday, June 9, 2025 By Mr C . ZANO