Introduction to Python
Dr. Tharaka Ilayperuma
Functions
Functions
Developing a software application is a complex task
• We identify requirements using different means, for instance, through interviews.
• We then translate the requirements into a piece of code using a programming language – a program may
contain thousands of lines of code
How do you solve a complex task?
we usually do it by decomposing the problem into a manageable sub tasks
• these sub tasks may also include common parts
Functions provide us means to manage complexity of programming by dividing the code into
blocks that will:
• Allows to have code blocks with well-defined roles
• Promote reusability and minimise redundancy
• Increase readability
• easy maintenance
Functions
Python Functions
User-defined
Built-in Functions
Functions
e.g.
• print()
• len()
• range()
Full list of functions available at
[Link]
User defined functions
Function name parameters
keywords def <function_name>(arg1, arg2, ..., argN):
optional
<statements> Your code goes here
return <value>
def creates a function and assigns it a name def sum_two_numbers(a, b):
return sends a result back to the caller c = a + b
return c
Remember to give your function a short but descriptive name
def sum_two_numbers(a, b):
c = a + b
print(c)
Call function by name. e.g. sum_two_numbers(5, 6) def sum_two_numbers(a, b):
return a + b
Coding Functions (1)
A return without a value simply returns None, the default result)
If return is not present, the function exits when the control flow falls off the end of the
function body
Coding Functions (2)
Arguments are passed by position, unless you say otherwise, and they match from left to
right
can also pass arguments by name with name=value keyword syntax, and unpack
arbitrarily many arguments to send with *pargs and **kargs starred-argument notation
Functions - examples
sum_numbers()
Exercise 1
1. Write a python function to return the number of letters in a string
Functions with optional arguments
You can define optional arguments in following ways
def example2(arg1,arg2 = <value>):
<Statements>
return
The above defines the default value to an argument. This value will be taken in case you don’t pass a value
when you invoke the function.
Arbitrary arguments
defined by using *
Functions with optional arguments (2)
def example2(arg1,**arg2): dictionary of keyword arguments defined by using **
print(arg1)
for key, value in [Link]():
print(f’{key}: {value}’)
These have (key: value) pairs
More on Functions
All python functions return a value regardless of whether there is a return statement or
not. Returns NULL if there is no return statement or no return value
You need to define the function before calling it. i.e. define your functions at the
beginning of your program
The variables that are defined inside the function are local to that function. i.e. they
cannot be accessed outside the function
The variables that are defined outside the function are called a global variable. How to
change a value of global variable inside a function?
Use the keyword global to define a global variable inside a function
Two functions cannot have the same name. If two functions are declared with the same
name the latter will override the prior function
global and local variables
global variable
local variable
creating global variables inside functions
global variable
global variable
Flow of Execution with Functions
def sum_two_numbers(a, b): Enter 1st number:
c = a + b Enter 2nd number:
return c c = a + b calculation takes place
p = int(input(“Enter 1st number: ”)) value c is returned
q = int(input(“Enter 2st number: ”)) result is assigned the value c
Result is printed
result = sum_two_numbers(p, q)
print(result)
Exercise 2
Write a function to get two inputs; first name, last name and print the full name
Exercise 3
Write a Python program to reverse a string (hint: use slicing)
Exercise 4
Write a function to calculate salary of an employee when the following arguments are
given
name, employee type (full time, part time) , basic salary, hourly rate, number of hours
You should get employee name, type, basic salary, hourly rate , and number of hours (as
may be the case) as user input outside the function and pass the relevant values to the
function to calculate the salary
Exercise 5
Write a python function to get the substrings of the following string
“Introduction to Python”
Exercise 6
Write a python function to get the substrings of the following string
“Introduction-to-Python”
Questions?