Tishk International University
Cybersecurity Department
Course Code: CBS 114
Advanced Programming
Lecture 2
Functions
Spring 2025
Hemin Ibrahim, PhD
[email protected]
Outline
✓ Introduction to functions
✓ Define function
✓ Call function
✓ Function with parameters
✓ Function Prototype
✓ Pass by Value
✓ The return Statement
Objectives
• Understand the concept of functions in programming
• Understand how control returns to the calling function after the execution of the
called function.
• Learn how to define functions with parameters to accept input data.
• Understand how function prototypes declare the function's interface.
• Learn how to return values from functions using the return statement.
• Understand the syntax for declaring and defining functions that operate on arrays.
• Understand the concept of recursive functions.
Introduction to Functions
■ Modular programming: breaking a program up into smaller units
■ They promote modularity, making code more organized, readable, reusable and
maintainable.
■ Function: is a set of statements that work together to perform a specific task
■ Advantages for modular programming
– Simplifies the process of writing programs
– Improves debugging of programs
– reducing redundancy
Introduction to Functions
■ Functions consist of a name, parameters (if any), a return type (if any), and a body
containing the executable code.
■ name: name of the function. Function names follow the same rules as variable names
parameter list: variables that hold the values passed to the function
body: statements that perform the function’s task
return type: data type of the value the function returns to the part of the program that
called it
Simple Sum Function
Function Type
Function parameters
Call function
Introduction to Functions
Function Concept
Define and Call Function in C++
Calling a Function
■ main is automatically called when the program starts
■ main can call any number of functions
■ Functions can call other functions
Function Procedure
Function Procedure
Output
Function called inside loop
Output
Function called inside loop
Output
Pass by Value
■ Pass by value: when argument is passed to a function, a copy of its value is placed in
the parameter
■ Function cannot access the original argument
Example #1 Example #2
Void Function
Functions that don't return a value use void as the return type.
Returning a Value From a Function
■ Return statement can be used to return a value from the function to the
module that made the function call
■ Prototype and definition must indicate data type of return value (not void)
Returning a Value From a Function
■Calling function should use return value, e.g.,
– assign it to a variable
– send it to cout
– use it in an arithmetic computation
– use it in a relational expression
Returning a Value From a Function
■Calling function should use return value, e.g.,
– assign it to a variable
– send it to cout
– use it in an arithmetic computation
– use it in a relational expression
Returning a Value From a Function
■Calling function should use return value, e.g.,
– assign it to a variable
– send it to cout
– use it in an arithmetic computation
– use it in a relational expression
Returning a Value From a Function
■Calling function should use return value, e.g.,
– assign it to a variable
– send it to cout
– use it in an arithmetic computation
– use it in a relational expression
A function with Boolean return
It can be written like this too
A function with Boolean return
All the same
Example: Cube Function, using Parameters
Parameter
Example: Sum Function
Example: Sum Function
Example: Factorial Calculation
■ The factorial of a non-negative integer n is
denoted as n! and is defined as the product
of all positive integers less than or equal to n.
■ For example, 5! (read as "5 factorial") is
calculated as 5 × 4 × 3 × 2 × 1 = 120.
Function Prototype
■ Program must include either prototype or full function definition before any call to
the function, otherwise a compiler error occurs
■ When using prototypes, function definitions can be placed in any order in the source
file.
■ Function prototype is similar to the heading of the function except in the parameter
list in the prototype only includes the data types of the parameters, followed by a
semicolon.
■ By having a function prototype, the compiler can ensure that the function is called
correctly, with the appropriate parameters and types, and that the function returns
a value of the correct type.
■ In this course we will define the functions before calling them, so we do not need
prototype.
Function Prototype
Function without Prototype
■The function (add) is coming after main function and it hasn’t defined
as a function prototype
Function without Prototype
■The function (add) is coming before main function and it is correct
and doesn’t need a function prototype
Parameter values
■Changes to the parameter in the function do not affect the value of
the argument in the calling function
Output
0 1.5
1.5 0
0 10
0 1.5
A function with Boolean return
Same result
Arrays with Functions
Functions can accept arrays as parameters and perform operations on array elements.
void Function type int Function type
Recursion
■ Recursion is when a function calls itself.
■ It's useful for solving problems that can be divided into smaller, similar sub-
problems.
■ Having the right initial conditions is vital to avoid never-ending loops in your code.
■ Recursion must have a base case, a condition that determines when the recursion
should stop. Without a base case, recursion would lead to infinite calls.
Recursion - Factorial Calculation Example
■ If n is 0, the factorial is 1 (0! = 1). This is the simplest case.
■ If n is greater than 0, the factorial of n can be calculated as n times the factorial of (n
- 1).
■ Recursion is used in various algorithms and data structures, such as tree traversal,
searching, and sorting.
■ it simplifies the solution for problems that have
a self-similar structure.
Comparison
Using Recursion Normal Function
Recursion - Sum Function Example
Recursion - Sum Function Example