FUNCTIONS
Topic 2
CSIT122 Intermediate Programming
Mr. Jay Vince Serato
Prepared by: Jay Vince D. Serato
Functions
Functions break large computing tasks into smaller ones, and
enable people to build on what others have done instead of
starting over from scratch.
Appropriate functions hide details of operation from parts of
the program that don’t need to know them, thus clarifying the
whole, and easing the pain of making changes.
B.W. Kernighan and D.M. Ritchie - The C Programming
Language
Prepared by: Jay Vince D. Serato
Functions
Functions
• Did you know that you have been using functions?
• Look at printf, or scanf, or even your main function. Ever
wonder how they perform?
• By the end of this week, you will be creating your very own
function!
Prepared by: Jay Vince D. Serato
Functions
Functions
• A large C program is divided into basic building blocks
called C function.
• C function contains a set of instructions enclosed by “{ }”
which performs specific operation in a C program.
• Actually, collection of these functions creates a C program.
Prepared by: Jay Vince D. Serato
Functions
Functions: Uses and Benefits
• C functions are used to avoid rewriting same logic/code
again and again in a program.
• There is no limit in calling C functions to make use of same
functionality wherever required.
• We can call functions any number of times in a program and
from any place in a program.
Prepared by: Jay Vince D. Serato
Functions
Functions: Uses and Benefits
• A large C program can easily be tracked when it is divided
into functions.
• The core concept of C functions are re-usability, dividing a
big task into small pieces to achieve the functionality and to
improve understandability of very large C programs.
Prepared by: Jay Vince D. Serato
Functions
Functions: Uses and Benefits
• Functions are fundamental to writing modular code.
• They provide the basic mechanism for enclosing low-level
source code, hiding algorithmic details, and presenting
instead an interface that describes more intuitively what the
code actually does.
• Functions present a higher level of abstraction and facilitate
a divide-and-conquer strategy for program decomposition.
Prepared by: Jay Vince D. Serato
Functions
Functions: Parts
Every function must have the following:
• Function Name
• List of Arguments
• Return Type
• Definition
Prepared by: Jay Vince D. Serato
Functions
Functions: Parts
Function Name
• The function name is located after the return type and before
the list of arguments (in the code above: sample).
• It serves as an ID so your other functions may call it.
• This being said, you cannot create other functions bearing the
same name.
Prepared by: Jay Vince D. Serato
Functions
Functions: Parts
List of Arguments
• The list of arguments is located inside the pair of parentheses
after the function name (in the code above: char ch).
• These arguments will serve as inputs needed for the
operations in the function.
• Each argument shall have its data type and variable name.
• If a function needs no argument, supply void as argument.
Prepared by: Jay Vince D. Serato
Functions
Functions: Parts
Return Type
• The return type is located before the function name (in the
code above: int).
• A function may have to give back, or output, a value to the
function that called it.
• The function must specify what type of data it will return.
• If it is a function that does not return a value, supply void to
the return type.
Prepared by: Jay Vince D. Serato
Functions
Functions: Parts
Definition
• The function definition is the block of code enclosed in “{ }”
after the list of arguments.
• This serves as the implementation for the function where you
can initialize variables, perform statements and operations, etc.
• The function definition must end with a return statement, for
non-void return-type functions.
Prepared by: Jay Vince D. Serato
Functions
Functions: Aspects
There are 3 aspects in each C function. They are
• Function Declaration (or Prototype)
• This informs compiler about the function name, function
parameters and return value’s data type.
• Function Definition
• This contains all the statements to be executed.
• Function Call
• This calls the actual function.
Prepared by: Jay Vince D. Serato
Functions
Functions: Prototypes
• A function must be declared before it is used.
• This means that either the function declaration or definition
must exist in the source file above the place where it is first
called by some other function (especially the main function).
Prepared by: Jay Vince D. Serato
Functions
Functions: Prototypes
• A function declaration, or prototype, is an interface
specification.
• It states the function name, the number and type of input
arguments, and the return value type.
• It enables the compiler to perform type-checking — to ensure
the argument types being passed to the function match the
interface definition — which catches many coding errors at
compile time.
Prepared by: Jay Vince D. Serato
Functions
Functions: Prototypes
• Some example prototypes are as follows:
• Notice that the variable names are optional in the
declaration, only the types matter.
• However, variable names can help clarify how a function
should be used.
Prepared by: Jay Vince D. Serato
Functions
Functions: Definition
• A function definition contains the actual workings of the
function — the declarations and statements of the function
algorithm.
• The function is passed a number of input parameters (or
arguments) and may return a value, as specified by its
interface definition.
Prepared by: Jay Vince D. Serato
Functions
Functions: Definition
• Function arguments are passed by a transaction termed
“pass-by-value”.
• This means that the function receives a local copy of each
input variable, not the variable itself.
• Thus, any changes made to the local variable will not affect
the value of the variable in the calling function.
Prepared by: Jay Vince D. Serato
Functions
Functions: Definition
Prepared by: Jay Vince D. Serato
Functions
Functions: Definition
• In the case of the code in the previous slide, the values
passed to myfunc() are x=1 and y=2, respectively, and these
are changed to x=3 and y=3 in the subsequent statements.
• However, the values of a and b are unaffected and d = 1+2 =
3.
Prepared by: Jay Vince D. Serato
Functions
Functions: Tips
• While a function can only have one return value, it may
possess several return statements.
• These define multiple exit points from the function, from
which program-control returns to the next statement of the
calling function.
Prepared by: Jay Vince D. Serato
Functions
Functions: Tips
• If a function is to return a value of a certain type, all return
statements must return a value of that type.
• But if a function does not return a value (i.e., a void), then an
empty return; suffices, and this may be omitted altogether
for a no-value return occurring at the end of the function
block.
Prepared by: Jay Vince D. Serato
Functions
Functions: Tips
Prepared by: Jay Vince D. Serato
Functions
Recursion
Prepared by: Jay Vince D. Serato
Functions
Recursive Functions
• Functions in C can be recursive, which means that they can
call themselves.
• Recursion tends to be less efficient than iterative code (i.e.,
code that uses loop-constructs), but in some cases may
facilitate more elegant, easier to read code.
Prepared by: Jay Vince D. Serato
Functions
Recursive Functions
Example: Factorials
• A factorial of a number n (or expressed as n!) is a product of
all the numbers from 1 to n.
• For example,
• 5! = 1×2×3×4×5 = 120
• or 5! = 5×4×3×2×1 = 5 × 4!
• Therefore, in general, n! = n × (n-1)!
Prepared by: Jay Vince D. Serato
Functions
Recursive Functions
Prepared by: Jay Vince D. Serato
Functions
Recursive Functions: Parts
• Base Case/s
• Will handle the “stopping point” of a recursive function
• Recursive Case/s
• Will handle the function call to itself with updated argument
• Updated argument is expected to reach base case
BASE CASE
RECURSIVE CASE
Prepared by: Jay Vince D. Serato
Functions
Recursive Functions
Example: Fibonacci Sequence
• The nth term in the Fibonacci Sequence is the sum of its
previous two terms: the n-1th term and the n-2th term.
• The first few numbers of the Fibonacci Sequence are
0, 1, 1, 2, 3, 5, 8, 13, 21, 34
Prepared by: Jay Vince D. Serato
Functions
Recursive Functions
Prepared by: Jay Vince D. Serato
Functions