Object-Oriented Programming
Python functions
Anastasia Dimou
[Link]@[Link]
Functions in Python
In Python a function is defined using the def keyword
syntax: def <name>():
2
Functions without content
If we have a function with no content, we use the pass statement.
def my_name():
pass = skip
my_name()
3
Functions with content
In Python a function is defined using the def keyword
syntax: def <name>():
def my_name():
print("Hello! My name is Anastasia")
>>>my_name()
→ Hello! My name is Anastasia
4
Functions with Parameters
Parameters are information that can be passed into functions
specified after the function name
inside the parentheses & separated by comma
syntax: def <name>(arg1,arg2,...,argN):
def my_name(arg):
print("Hello! My name is " + arg)
my_name("Anastasia")
my_name("Helen")
5
Functions with Parameters
Parameters are information that can be passed into functions
specified after the function name
inside the parentheses & separated by comma
syntax: def <name>(arg1,arg2,...,argN):
def my_name(arg):
print("Hello! My name is " + arg)
my_name("Anastasia") → Hello! My name is Anastasia
my_name("Helen") → Hello! My name is Helen
6
Functions with Parameters
Parameters: the variable listed inside the parentheses in the function definition.
Argument: the value that is sent to the function when it is called.
def my_name(arg): parameter
print("Hello! My name is " + arg)
my_name("Anastasia") argument
my_name("Helen")
7
Functions with Parameters
if our function expects N parameters,
we have to call the function with N arguments.
no more & no less
def my_name(arg): N attributes
print("Hello! My name is " + arg)
my_name("Anastasia") N arguments
my_name("Helen")
8
Functions with Parameters
if our function expects 1 parameter,
we have to call the function with 1 argument.
no more & no less
def my_name(arg): 1 parameter
print("Hello! My name is " + arg)
1 argument
my_name("Anastasia")
my_name("Helen")
9
Functions with Parameters
if our function expects 1 parameter,
we have to call the function with 1 argument.
no more & no less
def my_name(arg): 1 parameter
print("Hello! My name is " + arg)
1 argument
my_name("Anastasia") → Hello! My name is Anastasia
my_name("Helen") → Hello! My name is Helen
10
Functions with Parameters
if our function expects 2 parameters,
we have to call the function with 2 argument.
no more, no less - order matters
2 parameters
def my_name(name,surname):
print("Hello! My name is " + name + " " + surname) 2 arguments
my_name("Anastasia","Dimou")
my_name("Helen","Cool")
11
Functions with Parameters
if our function expects 2 parameters,
we have to call the function with 2 argument.
no more, no less - order matters
2 parameters
def my_name(name,surname):
print("Hello! My name is " + name + " " + surname) 2 arguments
my_name("Anastasia","Dimou")
→ Hello! My name is Anastasia Dimou
my_name("Helen","Cool") → Hello! My name is Helen Cool
12
Functions with Keyword Parameters
we can call the function following the syntax: key = value
order does NOT matter
def my_name(name,surname,age):
print("Hello! My name is " + name + " " + surname +
"and I am " + str(age) + " years old.")
my_name(age=20,surname="Dimou",name="Anastasia")
13
Functions with Keyword Parameters
we can call the function following the syntax: key = value
order does NOT matter
def my_name(name,surname,age):
print("Hello! My name is " + name + " " + surname +
"and I am " + str(age) + " years old.")
my_name(age=20,surname="Dimou",name="Anastasia")
→ Hello! My name is Anastasia Dimou and I’m 20 years old.
14
Functions with Keyword Parameters
we can call the function following the syntax: key = value
order DOES matter
def my_name(name,surname,age):
print("Hello! My name is " + name + " " + surname +
"and I am " + str(age) + " years old.")
my_name(age=20,surname=Dimou,name=Anastasia)
my_name(20,"Dimou","Anastasia")
15
Functions with Keyword Parameters
we can call the function following the syntax: key = value
order DOES matter
def my_name(name,surname,age):
print("Hello! My name is " + name + " " + surname +
"and I am " + str(age) + " years old.")
my_name(age=20,surname=Dimou,name=Anastasia)
my_name(20,Dimou,Anastasia)
→ Hello! My name is 20 Dimou and I’m Anastasia years old.
16
Functions with Keyword Parameters
we can call the function following the syntax: key = value
order DOES matter
def my_name(name,surname,age):
print("Hello! My name is " + name + " " + surname +
"and I am " + str(age) + " years old.")
my_name(age=20,surname=Dimou,name=Anastasia)
my_name(20,Dimou,Anastasia)
→ Hello! My name is 20 Dimou and I’m Anastasia years old.
17
Functions with Keyword Parameters
we can call the function following the syntax: key = value
order DOES matter
def my_name(name,surname,age):
print("Hello! My name is " + name + " " + surname +
"and I am " + str(age) + " years old.")
my_name(age=20,surname=Dimou,name=Anastasia)
my_name(20,"Dimou","Anastasia")
→ Error
18
Functions with Keyword Parameters
we can call the function following the syntax: key = value
order DOES matter
def my_name(name,surname,age):
print("Hello! My name is " + str(name) + " " +
str(surname) + "and I am " + str(age) + " years old.")
my_name(20,"Dimou","Anastasia")
→ Hello! My name is 20 Dimou and I’m Anastasia years old.
19
Functions with Default Parameters
If we call the function without argument, it uses the default value
wnr de gebruiker geen argument geeft gebruikt het de default
value maar wnr de gebruiker er eentje geeft, gebruikt de functie
die
def my_name(name="Anastasia"):
print("Hello! My name is " + name)
20
Functions with Default Parameters
If we call the function without argument, it uses the default value
def my_name(name="Anastasia"):
print("Hello! My name is " + name)
my_name("Bob")
my_name("Alice")
my_name()
21
Functions with Default Parameters
If we call the function without argument, it uses the default value
def my_name(name="Anastasia"):
print("Hello! My name is " + name)
my_name("Bob") → Hello! My name is Bob
my_name("Alice") → Hello! My name is Alice
my_name() → Hello! My name is Anastasia
22
Functions with Arbitrary Parameters
If we do not know how many arguments will be passed into the function,
* is added before the parameter name in the function definition
def my_friends(*friends):
print("My friend are: ")
for x in friends: print(x + " ")
my_friends("Bob")
my_friends("Bob","Alice")
23
Functions with Arbitrary Parameters
If we do not know how many arguments will be passed into the function,
* is added before the parameter name in the function definition
def my_friends(*friends):
print("My friend are: ")
for x in friends: print(x + " ")
my_friends("Bob")
My friends are
Bob
24
Functions with Arbitrary Parameters
If we do not know how many arguments will be passed into the function,
* is added before the parameter name in the function definition
def my_friends(*friends):
print("My friend are: ")
for x in friends: print(x + " ")
my_friends("Bob","Alice")
My friends are
Bob
Alice
25
Functions with Arbitrary Keyword Parameters
If we do not know how many keyword arguments will be passed into the
function, ** is added before the parameter name in the function definition
def my_friends(**friends):
print("My female friend is " + friends["female"])
my_friends(male="Bob",female="Alice")
26
Functions with Arbitrary Keyword Parameters
If we do not know how many keyword arguments will be passed into the
function, ** is added before the parameter name in the function definition
def my_friends(**friends):
print("My female friend is " + friends["female"])
my_friends(male="Bob",female="Alice")
→ My female friend is Alice
27
Functions which return values
If we want the function to return a value, we use the return statement
def my_name(surname,name="Anastasia"):
fullname = str(name) + " " + str(surname)
return fullname
callMe = my_name("Dimou","Helen")
print("Call me with my name: " + callMe)
→
28
Functions which return values
If we want the function to return a value, we use the return statement
default value
def my_name(surname,name="Anastasia"):
fullname = str(name) + " " + str(surname)
return fullname
callMe = my_name("Dimou","Helen")
print("Call me with my name: " + callMe)
→ Call me with my name: Helen Dimou
29
Functions which return values
If we want the function to return a value, we use the return statement
def my_name(surname,name="Anastasia"):
fullname = str(name) + " " + str(surname)
return fullname
callMe = my_name("Dimou","Helen")
print("Call me with my name: " + callMe)
→ Call me with my name: Helen Dimou
30
Wrap up: Functions syntax
def <nameA>():
pass
def <nameB>(arg1,arg2,...,argN=<default>):
return <value>
<nameA>()
<variable> = <nameB>(arg1=<value>,arg2,...,argN-1)
31
Wrap up: Functions syntax
no
parameters
def <nameA>():
no content
pass
no
arguments
<nameA>()
32
Wrap up: Functions syntax
default
parameters
parameters
def <nameB>(arg1,arg2,...,argN=<default>):
return <value>
return keyword
arguments
value arguments
<variable> = <nameB>(arg1=<value>,arg2,...,argN-1)
33
Wrap up: Functions syntax
arbitrary
keyword
parameters
arbitrary
parameters
def <nameC>(*args): def <nameD>(**args):
… … arbitrary
keyword
arguments
arbitrary
arguments
<nameC>(arg1,arg2,...,argN-1) <nameD>(arg1="..",argN="..")
34
Object-Oriented Programming
Anastasia Dimou
[Link]@[Link]