ENGG1810/9810
ENGG1810/9810
Introduction to Engineering Computing
Week 5: Functions I
Dr. Imdad Ullah
Faculty of Engineering
ENGG1810/9810
COMMONWEALTH OF AUSTRALIA
Copyright Regulations 1969
WARNING
This material has been reproduced and communicated to you by or on behalf of
the University of Sydney pursuant to Part VB of the Copyright Act 1968 (the Act).
The material in this communication may be subject to copyright under the Act.
Any further reproduction or communication of this material by you may be the
subject of copyright protection under the Act.
Do not remove this notice.
INTRODUCTION
ENGG1810/9810
What will you learn in this course?
Week 1: Introduction to Python
Week 2: Storing Data and Making Decisions Programming
Week 3: Repeating Actions I Basics
Week 4: Repeating Actions II
Week 5: Functions I
Week 6: Functions II Functions
Week 7: Libraries and Modules I
and Packages
Week 8: Libraries and Modules II
Week 9: Application I
Week 10: Application II Advanced
Week 11: Case Study I
Topics
Week 12: Case Study II
Week 13: Revision and Exam Guide
ENGG1810/9810
LabTest 1 Guide
Assessment Details
Sample Questions
Function
Definition and Scope
Global and Local Variables
Multiple Values
ENGG1810/9810
LabTest 1 Guideline
Lab Test 1 Guideline and sample Qs can be found in:
https://edstem.org/au/courses/11229/lessons/34010/slides/237979
ENGG1810/9810
LabTest 1 Guide
Assessment Details
Sample Questions
Functions
Definition and Structure
Return
Global and Local Variables
ENGG1810/9810
Remember? Built-in Functions
The Python interpreter has a number of functions and types built into it that are always available
Wait, what is a function?
Function is a block of re-usable
codes that executes when it is
requested.
https://docs.python.org/3/library/functions.html
ENGG1810/9810
Remember? Built-in Functions
The Python interpreter has a number of functions and types built into it that are always available
Wait, what is a function?
Function is a block of re-usable
codes that executes when it is
requested. The len() function returns the number of items in an object:
https://docs.python.org/3/library/functions.
ENGG1810/9810
Remember? Built-in Functions
The Python interpreter has a number of functions and types built into it that are always available
Wait, what is a function?
Function is a block of re-usable
codes that executes when it is
requested. The len() function returns the number of items in an object:
len() function?
https://docs.python.org/3/library/functions.
ENGG1810/9810
Python Functions
Times Table for n in range 1,11
Print 32
format
in in
ENGG1810/9810
Python Functions
Times Table
I am sick and tired of writing
same code again and again
..
.
..
.
Of course, we can make with a for loop!
ENGG1810/9810
Python Functions
Times Table
With for loop, we can have the output in 3 lines.. but still..
I am sick and tired of writing
same code again and again
Save this code and re-use it when we need
Remember?
Function is a block of re-usable codes
that executes when it is requested.
ENGG1810/9810
Python Functions
Times Table our own function
Create a function!
No need to repeat
this anymore
Function calls! like we use built-in functions in Python.
ENGG1810/9810
Python Functions
our own function
Syntax check in detail!
def <function_name> (<argument-list>):
statement 1
statement 2
Function calls! like we use built-in functions in Python.
def is used to create (define) a function
<function_name> is the name you want to call the function.
be lowercase with words separated by underscores
Reflect what the function does e.g. calc_sum, times_table
<argument-list> is the information needed to execute function.
Function call must match function signature
ENGG1810/9810
Python Functions
our own function
Syntax check in detail! Parameter
def <function_name> (<argument-list>):
statement 1
statement 2
argument
Function calls! like we use built-in functions in Python.
def is used to create (define) a function
<function_name> is the name you want to call the function.
be lowercase with words separated by underscores
Reflect what the function does e.g. calc_sum, times_table
<argument-list> is the information needed to execute function.
Function call must match function signature
Parameters or Arguments?
The terms parameter and argument can be used for the same thing:
information that are passed into a function.
From a function's perspective:
A parameter is the variable listed inside the parentheses in the
function definition.
An argument is the value that is sent to the function when it is called.
ENGG1810/9810
Python Functions
our own function
Syntax check in detail!
def <function_name> (<argument-list>):
statement 1
statement 2
Function calls! like we use built-in functions in Python.
Function call: To execute the function, you must call the function
name and pass the required arguments (pieces of information)
Number of Arguments
By default, a function must be called with the correct number of
arguments.
Meaning that if your function expects 1 argument, you have to call the
function with 1 argument, not more, and not less.
ENGG1810/9810
Python Functions
our own function
Syntax check in detail!
def <function_name> (<argument-list>):
statement 1
statement 2
Function calls! like we use built-in functions in Python.
Function call: To execute the function, you must call the function
name and pass the required arguments (pieces of information)
Number of Arguments
By default, a function must be called with the correct number of
arguments.
Meaning that if your function expects 1 argument, you have to call the
function with 1 argument, not more, and not less.
What if we do not have required argument?
ENGG1810/9810
Python Functions
Times Table We can have more arguments!
Can we set another argument a?
ENGG1810/9810
Python Functions
Interesting Examples Keyword Arguments
You can also send arguments with the key = value syntax.
This way the order of the arguments does not matter.
ENGG1810/9810
Python Functions
Interesting Examples Default Parameter Value
It shows how to use a default parameter value. If we call
the function without argument, it uses the default value.
ENGG1810/9810
Python Functions
Interesting Examples Default Parameter Value
Of course, if we call with the argument, it will use the one
you put in the function call
ENGG1810/9810
LabTest 1 Guide
Assessment Details
Sample Questions SubplotsI
Functions
fig ax pit
Definition and Structure
Return
Global and Local Variables
ENGG1810/9810
Python Return
def <function_name> (<argument-list>):
statement 1
statement 2
return [expression]
The return statement immediately terminates a function execution
and sends the return value back to the caller code.
Cannot be used outside of function
ENGG1810/9810
Python Printing vs Returning
Function with print() Function with return
needto callit
I can see the values are printed Where are the values?
The return statement immediately terminates a function execution
and sends the return value back to the caller code
It sends where? How can I use this?
ENGG1810/9810
Python Printing vs Returning
Function with print() Function with return
I can see the values are printed Now I can see the values
The return statement immediately terminates a function execution
and sends the return value back to the caller code
We can use this in multiple way!
ENGG1810/9810
Python Printing vs Returning
Function call
What if we try with the print(), instead of return?
ENGG1810/9810
Python Printing vs Returning
Function call
ENGG1810/9810
Python Returning
Function call
ENGG1810/9810
Python Returning
Function call Function call
ENGG1810/9810
Python Returning
Function call Function call
Function call
ENGG1810/9810
LabTest 1 Guide
Assessment Details
Sample Questions
Functions
Definition and Structure
Return
Global and Local Variables
ENGG1810/9810
Python Global and Local Variable
Who is CEO of Google?
Sundar Pichai
ENGG1810/9810
Python Global and Local Variable
Who is CEO of Google in AU?
ENGG1810/9810
Python Global and Local Variable
Who is CEO of Google in AU?
ENGG1810/9810
Python Global and Local Variable
Who is CEO of Google in AU?
Function to print
someone in Australia
Function call to austarlia
ENGG1810/9810
Python Global and Local Variable
Who is CEO of Google in AU?
Global CEO AU CEO
Sundar Pichai Melanie Silva
ENGG1810/9810
Python Global and Local Variable
Who is CEO of Google in AU?
Variables that are created outside of a function (so far we learned
in the course) are known as global variables. Global variables can Global Variable
be used by everyone, both inside of functions and outside.
Local Variable
Variables declared inside the function (local variables) can only be
used within the function, it is not accessible outside the function
ENGG1810/9810
Python Global and Local Variable
Interesting examples for global and local variable (1)
Global Variable
Variables that are created outside of a function (so far we learned
in the course) are known as global variables. Global variables can
be used by everyone, both inside of functions and outside. Local Variable
Variables declared inside the function (local variables) can only be
used within the function, it is not accessible outside the function
ENGG1810/9810
Python Global and Local Variable
Interesting examples for global and local variable (2)
Global Variable
Variables that are created outside of a function (so far we learned Local Variable
in the course) are known as global variables. Global variables can
be used by everyone, both inside of functions and outside.
Variables declared inside the function (local variables) can only be
used within the function, it is not accessible outside the function
ENGG1810/9810
Python Global and Local Variable
Interesting examples for global and local variable (2)
Global Variable
Variables that are created outside of a function (so far we learned Local Variable
in the course) are known as global variables. Global variables can
be used by everyone, both inside of functions and outside.
Variables declared inside the function (local variables) can only be
used within the function, it is not accessible outside the function
ENGG1810/9810
Python Global and Local Variable
Interesting examples for global and local variable (3)
Global Variable
Variables that are created outside of a function (so far we learned
in the course) are known as global variables. Global variables can
be used by everyone, both inside of functions and outside.
Local Variable
Variables declared inside the function (local variables) can only be
used within the function, it is not accessible outside the function
ENGG1810/9810
Python Global and Local Variable
Interesting examples for global and local variable (4)
Variables that are created outside of a function (so far we learned Local Variable
in the course) are known as global variables. Global variables can
be used by everyone, both inside of functions and outside.
Variables declared inside the function (local variables) can only be
used within the function, it is not accessible outside the function
ENGG1810/9810
Python Global and Local Variable
Interesting examples for global and local variable (4)
Variables that are created outside of a function (so far we learned Local Variable
in the course) are known as global variables. Global variables can
be used by everyone, both inside of functions and outside.
Variables declared inside the function (local variables) can only be
used within the function, it is not accessible outside the function
Is there any way that we can make
global variable inside a function?
ENGG1810/9810
Python Global and Local Variable
Yes! We can! Use the global keyword
Global Variable
Variables that are created outside of a function (so far we learned
in the course) are known as global variables. Global variables can
be used by everyone, both inside of functions and outside.
Variables declared inside the function (local variables) can only be
used within the function, it is not accessible outside the function
To create a global variable inside a function,
you can use the global keyword.
ENGG1810/9810
THANKS FOR
WATCHING
Good luck in studying