Week 9 - 10 Functions
Week 9 - 10 Functions
Presented By:
Dr. Abdalla Moustafa
[Week 9-10]
1
Students Learning Objectives (Weeks Nine & Ten)
Introduction to functions
Function declaration and definition
Function parameters (pass by value)
Function calling
Function overloading
C++ storage class
C++ Recursion
2
C++ Functions
3/27
User-defined Function
4/27
C++ Function Declaration
Here,
•the name of the function is greet()
•the return type of the function is void
•the empty parentheses mean it doesn't have any parameters
•the function body is written inside {}
5/27
Calling a Function
6/27
Example 1: Display a Text
7/27
Function Parameters
8/27
Example 2: Function with Parameters
9/27
Function with Parameters
Note: The type of the arguments passed while calling the function must match with
the corresponding parameters defined in the function declaration.
10/27
Return Statement
In the above programs, we have used void in the function declaration. For example,
11/27
Return Statement
• Here, we have the data type int instead of void. This means that the function returns
an int value.
• The code return (a + b); returns the sum of the two parameters as the function value.
• The return statement denotes that the function has ended. Any code
after return inside the function is not executed.
12/27
Example 3: Add Two Numbers
13/27
Example 3: Add Two Numbers
Notice that sum is a variable of int type. This is because the return value of add() is
of int type.
14/27
Function Prototype
• In C++, the code of function declaration should be before the function call.
• However, if we want to define a function after the function call, we need to use the function
prototype. For example,
In this code, the function prototype is:
16/27
Benefits of Using User-Defined Functions
• Functions make the code reusable. We can declare them once and use them multiple
times.
• Functions make the program easier as each small task is divided into a function.
• Functions increase readability.
17/27
C++ Library Functions
18/27
C++ User-defined Function Types
For better understanding of arguments and return in functions, user-defined functions can
be categorized as:
•Function with no argument and no return value
•Function with no argument but return value
•Function with argument but no return value
•Function with argument and return value
Consider a situation in which you have to check prime number. This problem is solved
below by making user-defined function in 4 different ways as mentioned above.
19/27
Example 1: No arguments passed and no return value
• In the above program, prime() is
called from the main() with no
arguments.
• prime() takes the positive number
from the user and checks whether the
number is a prime number or not.
• Since, return type of prime() is void,
no value is returned from the
function.
20/27
Example 2: No arguments passed but a return value
• In the above
program, prime() function is called
from the main() with no arguments.
• prime() takes a positive integer from
the user. Since, return type of the
function is an int, it returns the
inputted number from the user back
to the calling main() function.
• Then, whether the number is prime
or not is checked in the main() itself
and printed onto the screen.
21/27
Example 3: Arguments passed but no return value
22/27
Example 4: Arguments passed and a return value.
23/27
Which method is better?
• All four programs above gives the same output and all are technically correct program.
• There is no hard and fast rule on which method should be chosen.
• The particular method is chosen depending upon the situation and how you want to
solve a problem.
24/27
C++ Function Overloading
• In C++, two functions can have the same name if the number and/or type of arguments passed is
different.
• These functions having the same name but different arguments are known as overloaded functions.
For example:
• Here, both functions have the same name, the same type, and the same number of arguments.
Hence, the compiler will throw an error.
25/27
Example 1: Overloading Using Different Types of Parameter
26/27
Example 1: Overloading Using Different Types of Parameter
27/27
Example 2: Overloading Using Different Number of Parameters
28/27
Example 2: Overloading Using Different Number of Parameters
Note:
• In C++, many standard
library functions are
overloaded. For
example,
the sqrt() function can
take double, float, int, etc. as
parameters.
• This is possible
because
the sqrt() function is
overloaded in C++.
C++ Programming Default Arguments (Parameters)
30/27
C++ Programming Default Arguments (Parameters)
32/27
Example: Default Argument
We can also define the default parameters in the function definition itself. The program below is equivalent
to the pervious one.
33/27
Things to Remember
1. Once we provide a default value for a parameter, all subsequent parameters must also have default
values. For example,
2. If we are defining the default arguments in the function definition instead of the function prototype, then
the function must be defined before the function call.
34/27
C++ Storage Class
• Every variable in C++ has two features: type and storage class.
• Type specifies the type of data that can be stored in a variable. For
example: int, float, char etc.
• And, storage class controls two different properties of a variable: lifetime (determines how
long a variable can exist) and scope (determines which part of the program can access it).
Depending upon the storage class of a variable, it can be divided into 3 major types:
•Local variable
•Global variable
•Static local variable
35/27
Local Variable
• A variable defined inside a function (defined inside function body between braces) is called a local
variable or automatic variable.
• Its scope is only limited to the function where it is defined. In simple terms, local variable exists and
can be accessed only inside a function.
• The life of a local variable ends (It is destroyed) when the function exits.
36/27
Example 1: Local variable
37/27
Global Variable
38/27
Example 2: Global variable
39/27
Static Local variable
Keyword static is used for specifying a static variable. For example:
• A static local variable exists only inside a function where it is declared (similar to a local
variable) but its lifetime starts when the function is called and ends only when the program
ends.
• The main difference between local variable and static variable is that, the value of static
variable persists the end of the program.
40/27
Example 3: Static local variable
• As we can see,
the factorial() function is
calling itself. However, during
each call, we have decreased
the value of n by 1.
• When n is less than 1,
the factorial() function
ultimately returns the output.
Example 3: Static local variable
47