Вы находитесь на странице: 1из 14

Lecture 5 – Functions in Python.

BBT 4211 - BCOM – Kurui Daniel


Content

• What are functions?


• Why do we use functions?
• How to declare a function in Python
• Calling/invoking a function in python

22 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 2


What is a function?

• A function is a group of statements that together perform a task.


• You can divide up your code into separate functions.
• How you divide up your code among different functions is up to you, but
logically the division usually is such that each function performs a specific
task
• As you already know, Python gives you many built-in functions like print(),
etc. but you can also create your own functions. These functions are
called user-defined functions.

22 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 3


Why do we use functions?
• Imagine given a task to calculate the
average marks for some students given
some marks.

22 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 4


Why do we use functions? Cont’d

22 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 5


Why do we use functions?
We need to define our own functions because of the following:

a) Readability: sqrt(5) is clearer than copy-pasting in an algorithm to compute


the square root

b) Maintainability: To change the algorithm, just change the function (vs


changing it everywhere you ever used it)

c) Code reuse: Lets other people use algorithms you’ve implemented

22 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 6


Defining a Function in Python

• You can define functions to provide the required functionality. Here are
simple rules to define a function in Python.
• Function blocks begin with the keyword def followed by the function name
and parentheses ( ( ) ).
• Any input parameters or arguments should be placed within these
parentheses. You can also define parameters inside these parentheses.
• The first statement of a function can be an optional statement - the
documentation string of the function or docstring.
• The code block within every function starts with a colon (:) and is indented.
• The statement return [expression] exits a function, optionally passing back an
expression to the caller. A return statement with no arguments is the same as
return None.

22 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 7


Defining a Function in Python cont’d

• Python function Syntax

22 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 8


Defining a Function in Python cont’d

• Python function example

• Function name: my_function


• The function prints what is passed in

22 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 9


Calling/invoking a function in python

• Defining a function only gives it a name, specifies the parameters that are
to be included in the function and structures the blocks of code.
• Once the basic structure of a function is finalized, you can execute it by
calling it from another function or directly from the Python prompt.
• Following is the example to call my_funtion() function

22 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 10


Our previous problem solution

• We create a function then call it every time we need to get average mark
for our students:

22 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 11


The End!

22 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 12


Lab Exercises

Using functions, write python programs to :


1. find the area and perimeter of :
• Circle
• Rectangle
• Triangle
2. Calculate simple interest of a loan
3. Calculate Compound interest of a Loan
4. Calculate the age of someone given his/her dob

22 maj 2020 BBT 4211 - Comp Programming - Kurui Daniel 13


END
Any Questions?
Thank you!
BBT 4211 - Comp Programming - Kurui Daniel

Вам также может понравиться