You are on page 1of 10

PYTHON PROGRAMMING

(PYTHON 3.X using Jupyter Notebook)


Function

DSC551 – PROGRAMMING FOR DATA SCIENCE

Pn Marhainis Jamaludin
Faculty of Computer and Mathematical Sciences
What is function?
• A small program or subprogram to perform a specific task
• Python provides many built-in function, however, we can
create our own function
• Function in Python is defined by the def statement followed
by the function name and parentheses (). Must end with
colon :
#function which has no argument/parameter list and does not return any value back to
the caller
def f1():
print("Hello")
#function can receive arguments/parameter list
def f1(a,b):
print(a,b)
#function may return a value to the caller, by using keyword "return"
def f1(a,b):
return a+b
Function syntax
• Function definition – actual program that
executes/performs the task. It contains:
• Keyword def
• Function name
• Arguments/parameter lists enclosed in parentheses
• Colon :
• Function body – contains all the statements to be executed, if
function needs to return a value to the caller, then keyword return
to be used
• Function Call – the caller function to function definition
• Specify the function name and its parameter list
Examples:
#function definition
#function definition def my_f2(a,b):
def my_f1(a): z = pow(a)
print("Hello %s" %a) w=b+z
return w
#function call
my_f1("Sarah") #function call
print(my_f2(4,9))

#function which receives two list of numbers, merge and sort the list as an output
def my_f3(l1,l2):
m = l1 + l2
m.sort()
print(m)

#function call
a = [8,5,2,1]
b = [90,89,76,30]

my_f3(a,b)
Scope of variable
• Two types of variables : Global or Local variable.
• Global variable
• Variable defines outside the function.
• Global variable can be accessed anywhere in a program.
• Local variable
• Variable defines within a function.
• Local variable can be accessed within the function it is
declared.

global local
• Examples of scope of variables

def square(n):
m = n*n
return m

print("The square of 3 is: ",square(3))


print(m) #it is an error because m is local variable declared within function square

m=0
def square(n):
m = n*n
return m

print("The square of 3 is : ", square(3))


print(m) #it is not an error, because variable m is global variable declared before function
Multiple return values
• Python allows function to return many values
def my_f8(x):
return x*2, x*3, x*4

print(my_f8(10))

Function of functions
def iseven(x,f):
if (f(x) == f(-x)):
return True
else
return False

def square(n):
return n*n

def cube(n):
return n*n*n

print(iseven(2,square))
print(iseven(2,cube))
Passed by value or by reference
Exercise:
• Write a python function that receives a list of
elements and returns a minimum and maximum
elements from the list.
• Write a python function that receives a string,
count letter ‘a’ in a string. Display how many ‘a’ are
there in the string.
References
• https://www.programiz.com/python-
programming/list
• https://www.digitalocean.com/community/tutorial
s/understanding-lists-in-python-3

You might also like