Foundations of Engineering II- PVUHow to solve a big problem?3The Top-down design approach is based on dividingthe main probleminto smaller tasks which may be dividedinto simpler tasks,
Trang 1TALENTS FOR DEVELOPMENT - Petrovietnam University
Chapter 7: Functions
Lecturer: Trần Ngọc Hiệp, MSc.
Email: hieptn@pvu.edu.vn Mobile: 0901 25 2468
Contents Functions
Advantages of functions Declaration and Defining a Function Function calling
Function Program Example
1
Trang 2TALENTS FOR DEVELOPMENT - Petrovietnam University
Trần Ngọc Hiệp, MSc Foundations of Engineering II- PVU
How to solve a big problem?
3
The Top-down design approach is based on dividing
the main problem into smaller tasks which may be
divided into simpler tasks , then implementing each
simple task by a subprogram or a function
This technique is called divide and conquer
TALENTS FOR DEVELOPMENT - Petrovietnam University
Trần Ngọc Hiệp, MSc Foundations of Engineering II- PVU
7.1 Functions
Function: A function is a group of statement that
together perform a specific task.
Why use function: Function are used for divide a
large code into module due to this we can easily
debug an maintain the code.
4
3
4
Trang 3TALENTS FOR DEVELOPMENT - Petrovietnam University
Trần Ngọc Hiệp, MSc Foundations of Engineering II- PVU
7.2 Advantages of Functions
Functions separate the concept (what is done) from the implementation (how it is done).
Function make program easier to understand
Function can be called several times in the same program, allowing the code to be reused.
Avoid repeating of code
5
7.2 Advantages of Functions
Divide a complex problem into many simpler problems
Reduce chances of error
Makes modifying a program becomes easier
5
Trang 4TALENTS FOR DEVELOPMENT - Petrovietnam University
Trần Ngọc Hiệp, MSc Foundations of Engineering II- PVU
Types of Functions
Functions
Library Function of predefined Function
User defined Function
7
TALENTS FOR DEVELOPMENT - Petrovietnam University
Trần Ngọc Hiệp, MSc Foundations of Engineering II- PVU
Types of Functions
8
Library functions: are those which are predefined.
Limitation of Library function:
Contained limited task only
Programmer do not have any controls on predefined
function.
User Defined function: are created by programmer
according to their requirements.
7
8
Trang 5TALENTS FOR DEVELOPMENT - Petrovietnam University
Trần Ngọc Hiệp, MSc Foundations of Engineering II- PVU
Components of Function
Function prototype
Function definition Function call
9
7.3 Declaration and Defining a Function
1 Declaration and Defining:
Syntax:
9
Trang 6TALENTS FOR DEVELOPMENT - Petrovietnam University
Trần Ngọc Hiệp, MSc Foundations of Engineering II- PVU
7.3 Declaration and Defining a Function
Return data type: A function may return a value
The return-data-type is the data type of the
values the function returns
Function name: is the name of function.
Parameters: This is a value which is pass in
function at the time of calling of function It is
optional.
Function body: is the collection of statements.
11
TALENTS FOR DEVELOPMENT - Petrovietnam University
Trần Ngọc Hiệp, MSc Foundations of Engineering II- PVU
Structure of a Function - Example
12
11
12
Trang 7TALENTS FOR DEVELOPMENT - Petrovietnam University
Trần Ngọc Hiệp, MSc Foundations of Engineering II- PVU
7.4 Function Calling
13
In Calling program write the name of the function and provide
its arguments without data types.
In calling a function, return value type and data types of the
arguments are not written.
7.4 Function Calling
The function can be called using either of the following methods
Call by
Value
Call by Reference
13
Trang 8TALENTS FOR DEVELOPMENT - Petrovietnam University
Trần Ngọc Hiệp, MSc Foundations of Engineering II- PVU
7.4 Function Calling - Call by Value
In call by value mechanism, the called function
creates a new set of variables in stack and copies the
values of the arguments into them.
In call by value method, the called function creates its
own copies of original values sent to it Any changes,
that are made, occur on the function’s copy of values
and are not reflected back to the calling function
Arguments must be immutable objects
15
TALENTS FOR DEVELOPMENT - Petrovietnam University
Trần Ngọc Hiệp, MSc Foundations of Engineering II- PVU
Trang 9TALENTS FOR DEVELOPMENT - Petrovietnam University
Trần Ngọc Hiệp, MSc Foundations of Engineering II- PVU
7.4 Function Calling - Call by Reference
In call by reference mechanism, instead of passing
values to the function being called,
reference/pointers to the original variables are
passed
In call by reference method, the called function
access and works with the original values using their
references Any changes , that occur, take place on the
original values are reflected back to the calling code.
Argument must be mutable object such as List, Tuple,
Set, Dictionary
17
7.4 Function Calling - Call by Reference
We also can pass the value by reference
In this case we pass the address of the value rather
than value
We use & operator for this purpose
To call by reference we can’t pass value , we have pass
memory address of variable
Note: C++ passes arrays to functions by reference.
17
Trang 10TALENTS FOR DEVELOPMENT - Petrovietnam University
Trần Ngọc Hiệp, MSc Foundations of Engineering II- PVU
7.4 Function Calling
19
Call by Reference Example
TALENTS FOR DEVELOPMENT - Petrovietnam University
Trần Ngọc Hiệp, MSc Foundations of Engineering II- PVU
Trang 11TALENTS FOR DEVELOPMENT - Petrovietnam University
Trần Ngọc Hiệp, MSc Foundations of Engineering II- PVU
Pass by Value Vs Pass by Reference
Pass by Reference Pass by Value
Passes address of actual parameter
Makes copy of actual
parameter
Accesses original variable’s content
Passes copy of contents
Original variable’s content
During program execution,
value parameter manipulates
data stored in own memory
overloading
Signature may be vary on the basis of number of argument or data type of argument or order of argument.
21
Trang 12TALENTS FOR DEVELOPMENT - Petrovietnam University
Trần Ngọc Hiệp, MSc Foundations of Engineering II- PVU
Function Overloading
23
Syntax of Function Overloading:
TALENTS FOR DEVELOPMENT - Petrovietnam University
Trần Ngọc Hiệp, MSc Foundations of Engineering II- PVU
Trang 13TALENTS FOR DEVELOPMENT - Petrovietnam University
Trần Ngọc Hiệp, MSc Foundations of Engineering II- PVU
Function Overloading
25
Example: Overloading function by changing data type.
Default Arguments
The default value for a parameter while declaring the
function is calling default argument.
If we make a call to the function without passing any
value for that parameter, the function will take the
default value specified
Example:
25
Trang 14TALENTS FOR DEVELOPMENT - Petrovietnam University
Trần Ngọc Hiệp, MSc Foundations of Engineering II- PVU
Default Arguments
Rules for using default arguments:
Only the last argument must be given default value
If we default an argument, then you will have to default all
the subsequent arguments after that
We can give any value a default value to argument
compatible with its datatype.
27
TALENTS FOR DEVELOPMENT - Petrovietnam University
Trần Ngọc Hiệp, MSc Foundations of Engineering II- PVU
Nested Member Function of a Class
Nested member function
A member function can be called inside another member function of the same class is known as nesting
of member function.
28
27
28
Trang 15TALENTS FOR DEVELOPMENT - Petrovietnam University
Trần Ngọc Hiệp, MSc Foundations of Engineering II- PVU
7.5 Function Program Example
29
Problems Problem 1
• Write a program using function which accept two integers as an argument and return its sum.
Problem 2
• Write a function which calculates the raise to power of an input base number up to given power number.
Problem 3
• Write a program to show Prime numbers between two intervals by making used-defined Function.
29
Trang 16TALENTS FOR DEVELOPMENT - Petrovietnam University
Trần Ngọc Hiệp, MSc Foundations of Engineering II- PVU
Solutions
31
Problem 1:
TALENTS FOR DEVELOPMENT - Petrovietnam University
Trần Ngọc Hiệp, MSc Foundations of Engineering II- PVU
Trang 17TALENTS FOR DEVELOPMENT - Petrovietnam University
Trần Ngọc Hiệp, MSc Foundations of Engineering II- PVU
Solutions
33
Problem 3:
Your Task
Write a program that lets the user perform arithmetic operations on two
number Your program must be menu driven, allowing the user to select the
operation (+,-,*, or /) and input the numbers Furthermore, your program
must consist of following functions:
• Function showChoice:
• Function add
• Function subtract
33
Trang 18TALENTS FOR DEVELOPMENT - Petrovietnam University
Trần Ngọc Hiệp, MSc Foundations of Engineering II- PVU
7.6 Variable Scope
Variable Scope is a region of the program, where
variables can be declared:
Inside a function: Local variables
In the definition of function parameters: Formal
parameters
Outside of functions: Global variables
35
Local variables
Formal parameters
Global variables
TALENTS FOR DEVELOPMENT - Petrovietnam University
Trần Ngọc Hiệp, MSc Foundations of Engineering II- PVU
Trang 19TALENTS FOR DEVELOPMENT - Petrovietnam University
Trần Ngọc Hiệp, MSc Foundations of Engineering II- PVU
Trang 20TALENTS FOR DEVELOPMENT - Petrovietnam University
Trần Ngọc Hiệp, MSc Foundations of Engineering II- PVU
7.6 Variable Scope
39
Example of Global Variables
TALENTS FOR DEVELOPMENT - Petrovietnam University
Trần Ngọc Hiệp, MSc Foundations of Engineering II- PVU
Trang 21TALENTS FOR DEVELOPMENT - Petrovietnam University
Trần Ngọc Hiệp, MSc Foundations of Engineering II- PVU
Python Standard Library
41
Python Math
Python has a set of built-in math functions,
including an extensive math module, that allows
you to perform mathematical tasks on numbers.
Python Math
41
Trang 22TALENTS FOR DEVELOPMENT - Petrovietnam University
Trần Ngọc Hiệp, MSc Foundations of Engineering II- PVU
Python Math
43
TALENTS FOR DEVELOPMENT - Petrovietnam University
Trần Ngọc Hiệp, MSc Foundations of Engineering II- PVU
Python Math
44
43
44
Trang 23TALENTS FOR DEVELOPMENT - Petrovietnam University
Trần Ngọc Hiệp, MSc Foundations of Engineering II- PVU
Python Math
45
Python Standard Library
Python Dates
A date in Python is not a data type of its own, but
we can import a module named datetime to work
with dates as date objects.
45
Trang 24TALENTS FOR DEVELOPMENT - Petrovietnam University
Trần Ngọc Hiệp, MSc Foundations of Engineering II- PVU
Python Datetime
47
To create a date.
TALENTS FOR DEVELOPMENT - Petrovietnam University
Trần Ngọc Hiệp, MSc Foundations of Engineering II- PVU
Python Datetime
48
The datetime object has a method for formatting
date objects into readable strings.
47
48
Trang 25TALENTS FOR DEVELOPMENT - Petrovietnam University
Trần Ngọc Hiệp, MSc Foundations of Engineering II- PVU
Python Datetime
49
Python Datetime
49
Trang 26TALENTS FOR DEVELOPMENT - Petrovietnam University
Trần Ngọc Hiệp, MSc Foundations of Engineering II- PVU
Python Datetime
51
TALENTS FOR DEVELOPMENT - Petrovietnam University
Trần Ngọc Hiệp, MSc Foundations of Engineering II- PVU
Summary
52
1 • Function
2 • How to declared and defined a function
3 • Calling function by value
4 • Calling function by reference
5 • Variable scope
51
52
Trang 27TALENTS FOR DEVELOPMENT - Petrovietnam University
Trần Ngọc Hiệp, MSc Foundations of Engineering II- PVU
Quiz
53
Discussion
53
Trang 28TALENTS FOR DEVELOPMENT - Petrovietnam University
Trần Ngọc Hiệp, MSc Foundations of Engineering II- PVU 55
55