02-Functions
02-Functions
Topic 2
CSIT122 Intermediate Programming
Mr. Jay Vince Serato
• By the end of this week, you will be creating your very own
function!
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.
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.
• Thus, any changes made to the local variable will not affect
the value of the variable in the calling function.
BASE CASE
RECURSIVE CASE