You are on page 1of 27

1

Preliminary Computer science

COMP0001

School of Science, Computing and Artificial Intelligence

Lecturer: Mr Ilenius Ildephonce

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 9 November 16, 2021 1 / 27
2

Lecture 9

Functions

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 9 November 16, 2021 2 / 27
Contents
3

1 Introduction

2 Defining Functions

3 Modularizing Code

4 Scope of Variables

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 9 November 16, 2021 3 / 27
Content
4

1 Introduction

2 Defining Functions

3 Modularizing Code

4 Scope of Variables

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 9 November 16, 2021 4 / 27
Opening problem
5

Find the sum of integers from 1 to 10, from 20 to 37, and from 35 to
49, respectively.

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 9 November 16, 2021 5 / 27
Solution
6

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 9 November 16, 2021 6 / 27
Content
7

1 Introduction

2 Defining Functions

3 Modularizing Code

4 Scope of Variables

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 9 November 16, 2021 7 / 27
Defining Functions
8

A function is a collection of statements that are grouped together to


perform an operation.

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 9 November 16, 2021 8 / 27
Calling Functions
9

Calling Functions
Testing the max function
This program demonstrates calling a function max to return the largest
of the int values

TestMax.py

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 9 November 16, 2021 9 / 27
Calling Functions
10

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 9 November 16, 2021 10 / 27
Functions With/Without Return Values
11

This type of function does not return a value. The function performs
some actions.

PrintGradeF.py & ReturnGrade.py

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 9 November 16, 2021 11 / 27
The None Value
12

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 9 November 16, 2021 12 / 27
Passing Arguments by Positions
13

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 9 November 16, 2021 13 / 27
Keyword Arguments
14

Keyword Arguments

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 9 November 16, 2021 14 / 27
Pass by Value
15

In Python, all data are objects. A variable for an object is actually a


reference to the object. When you invoke a function with a parameter,
the reference value of the argument is passed to the parameter. This is
referred to as pass-by-value. For simplicity, we say that the value of an
argument is passed to a parameter when invoking a function.
Precisely, the value is actually a reference value to the object.

If the argument is a number or a string, the argument is not affected,


regardless of the changes made to the parameter inside the function.

Increment.py

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 9 November 16, 2021 15 / 27
Content
16

1 Introduction

2 Defining Functions

3 Modularizing Code

4 Scope of Variables

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 9 November 16, 2021 16 / 27
Modularizing Code
17

Functions can be used to reduce redundant coding and enable code


reuse. Functions can also be used to modularize code and improve
the quality of the program.
GCD.py , TestGCD.py & PrimeNumber.py

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 9 November 16, 2021 17 / 27
Content
18

1 Introduction

2 Defining Functions

3 Modularizing Code

4 Scope of Variables

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 9 November 16, 2021 18 / 27
Scope of Variables
19

Scope of Variables
Scope: the part of the program where the variable can be referenced.
A variable created inside a function is referred to as a local variable.
Local variables can only be accessed inside a function. The scope of a
local variable starts from its creation and continues to the end of the
function that contains the variable.
In Python, you can also use global variables. They are created outside
all functions and are accessible to all functions in their scope.

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 9 November 16, 2021 19 / 27
Examples
20

Example 1
globalVar = 1
def f1():
localVar = 2
print(globalVar)
print(localVar)
f1()
print(globalVar)
print(localVar) # Out of scope. This gives an error

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 9 November 16, 2021 20 / 27
Examples
21

Example 2
x=1
def f1():
x=2
print(x) # Displays 2
f1()
print(x) # Displays 1

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 9 November 16, 2021 21 / 27
Examples
22

Example 3
x = eval(input("Enter a number: "))
if x > 0:
y=4
print(y) # This gives an error if y is not created

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 9 November 16, 2021 22 / 27
Examples
23

Example 4
sum = 0
for i in range(5):
sum += i
print(i)

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 9 November 16, 2021 23 / 27
Examples
24

Example 5
x=1
def increase():
global x
x=x+1
print(x) # Displays 2
increase()
print(x) # Displays 2

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 9 November 16, 2021 24 / 27
Default Arguments
25

Default Arguments
Python allows you to define functions with default argument values.
The default values are passed to the parameters when a function is
invoked without the arguments.
DefaultArgsDemo.py

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 9 November 16, 2021 25 / 27
Returning Multiple Values
26

Python allows a function to return multiple values. Listing 6.9 defines a


function that takes two numbers and returns them in non-descending
order.
MultiReturnVal.py

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 9 November 16, 2021 26 / 27
End of Week 10
27

End of Week 10.

Ilenius Ildephonce (The UWI, Five Islands) COMP0001-Lecture 9 November 16, 2021 27 / 27

You might also like