Functions
Start
Functions
A function is a block of code which only runs when it is called.
A function can return data as a result.
A function helps avoiding code repetition.
Creating a Function
def
def
In Python, a function is defined using the def keyword, followed by a function name and parentheses:
def
def my_function
def my_function()
def my_function():
def my_function():
def my_function():
print("Hello from a function")
Calling a Function
To call a function, write its name followed by parentheses
def my_function():
print("Hello from a function")
def my_function():
print("Hello from a function") my_function()
You can call the same function multiple times
def my_function():
print("Hello from a function") my_function() my_function()
my_function()
Function Names
A function name must start with a letter or underscore.
A function name can only contain letters, numbers, and underscores
Function names are case-sensitive (myFunction and myfunction are different)
Why Use Functions?
Imagine you need to convert temperatures from Fahrenheit to Celsius several times in your program. Without functions, you would have to write the same calculation code repeatedly.
temp1 = 77
celsius1 = (temp1 - 32) * 5 / 9
print(celsius1)
temp2 = 95
celsius2 = (temp2 - 32) * 5 / 9
print(celsius2)
temp3 = 50
celsius3 = (temp3 - 32) * 5 / 9
print(celsius3)
With functions, you write the code once and reuse it
def fahrenheit_to_celsius(fahrenheit):
return (fahrenheit - 32) * 5 / 9
print(fahrenheit_to_celsius(77))
print(fahrenheit_to_celsius(95))
print(fahrenheit_to_celsius(50))
Return Values
Functions can send data back to the code that called them using the return statement.
return
return
When a function reaches a return statement, it stops executing and sends the result back
def get_greeting():
return "Hello from a function"
def get_greeting():
return "Hello from a function"
message = get_greeting()
print(message)
A function that returns a value:
def get_greeting():
return "Hello from a function"
message = get_greeting()
print(message)
You can use the returned value directly:
Using the return value directly:
def get_greeting():
return "Hello from a function"
print(get_greeting())
Arguments
Information can be passed into functions as arguments.
Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma.
The following example has a function with one argument (fname). When the function is called, we pass along a first name, which is used inside the function to print the full name:
A function with one argument:
def my_function(fname):
print(fname + "Refsnes")
my_function("Emil")
my_function("Tobias")
my_function("Linus")
Parameters vs Arguments
The terms parameter and argument can be used for the same thing: information that are passed into a function.
An argument is the actual value that is sent to the function when it is called.
def my_function(name): # name is a parameter
print("Hello", name)
my_function("Emil") # "Emil" is an argument
Number of Arguments
By default, a function must be called with the correct number of arguments.
If your function expects 2 arguments, you must call it with exactly 2 arguments.
def my_function(fname, lname):
print(fname + " " + lname)
my_function("Emil", "Refsnes")
This function expects 2 arguments, and gets 2 arguments:
def my_function(fname, lname):
print(fname + " " + lname)
my_function("Emil", "Refsnes")
If you try to call the function with the wrong number of arguments, you will get an error:
def my_function(fname, lname):
print(fname + " " + lname)
my_function("Emil")
This function expects 2 arguments, but gets only 1:
def my_function(fname, lname):
print(fname + " " + lname)
my_function("Emil")
Default Parameter Values
You can assign default values to parameters. If the function is called without an argument, it uses the default value:
def my_function(name = "friend"):
print("Hello", name)
my_function("Emil")
my_function("Tobias")
my_function()
my_function("Linus")
Default value for country parameter:
def my_function(country = "Norway"):
print("I am from", country)
my_function("Sweden")
my_function("India")
my_function()
my_function("Brazil")
Keyword Arguments
You can send arguments with the key = value syntax.
def my_function(animal, name):
print("I have a", animal)
print("My", animal + "'s name is", name)
my_function(animal = "dog", name = "Buddy")
This way, with keyword arguments, the order of the arguments does not matter.
def my_function(animal, name):
print("I have a", animal)
print("My", animal + "'s name is", name)
my_function(name = "Buddy", animal = "dog")
Positional Arguments
When you call a function with arguments without using keywords, they are called positional arguments.
Positional arguments must be in the correct order:
def my_function(animal, name):
print("I have a", animal)
print("My", animal + "'s name is", name)
my_function("dog", "Buddy")
The order matters with positional arguments:
def my_function(animal, name):
print("I have a", animal)
print("My", animal + "'s name is", name)
my_function("Buddy", "dog")
Mixing Positional and Keyword Arguments
Positional arguments must come before keyword arguments:
def my_function(animal, name, age):
print("I have a", age, "year old", animal, "named", name) my_function("dog", name = "Buddy", age = 5)
Passing Different Data Types
You can send any data type as an argument to a function (string, number, list, dictionary, etc.).
Positional arguments must come before keyword arguments:
def my_function(fruits):
for fruit in fruits:
print(fruit)
my_fruits = ["apple", "banana", "cherry"]
my_function(my_fruits)
Sending a dictionary as an argument:
def my_function(person):
print("Name:", person["name"])
print("Age:", person["age"])
my_person = {"name": "Emil", "age": 25}
my_function(my_person)
References
https://www.w3schools.com/java
https://www.w3schools.com/java
Functions
fatemeh taghvaei
Created on May 31, 2026
Start designing with a free template
Discover more than 1500 professional designs like these:
View
Customer Service Course
View
Dynamic Visual Course
View
Dynamic Learning Course
View
Akihabara Course
Explore all templates
Transcript
Functions
Start
Functions
A function is a block of code which only runs when it is called.
A function can return data as a result.
A function helps avoiding code repetition.
Creating a Function
def
def
In Python, a function is defined using the def keyword, followed by a function name and parentheses:
def
def my_function
def my_function()
def my_function():
def my_function():
def my_function(): print("Hello from a function")
Calling a Function
To call a function, write its name followed by parentheses
def my_function(): print("Hello from a function")
def my_function(): print("Hello from a function") my_function()
You can call the same function multiple times
def my_function(): print("Hello from a function") my_function() my_function() my_function()
Function Names
A function name must start with a letter or underscore.
A function name can only contain letters, numbers, and underscores
Function names are case-sensitive (myFunction and myfunction are different)
Why Use Functions?
Imagine you need to convert temperatures from Fahrenheit to Celsius several times in your program. Without functions, you would have to write the same calculation code repeatedly.
temp1 = 77 celsius1 = (temp1 - 32) * 5 / 9 print(celsius1) temp2 = 95 celsius2 = (temp2 - 32) * 5 / 9 print(celsius2) temp3 = 50 celsius3 = (temp3 - 32) * 5 / 9 print(celsius3)
With functions, you write the code once and reuse it
def fahrenheit_to_celsius(fahrenheit): return (fahrenheit - 32) * 5 / 9 print(fahrenheit_to_celsius(77)) print(fahrenheit_to_celsius(95)) print(fahrenheit_to_celsius(50))
Return Values
Functions can send data back to the code that called them using the return statement.
return
return
When a function reaches a return statement, it stops executing and sends the result back
def get_greeting(): return "Hello from a function"
def get_greeting(): return "Hello from a function" message = get_greeting() print(message)
A function that returns a value:
def get_greeting(): return "Hello from a function" message = get_greeting() print(message)
You can use the returned value directly:
Using the return value directly:
def get_greeting(): return "Hello from a function" print(get_greeting())
Arguments
Information can be passed into functions as arguments.
Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma.
The following example has a function with one argument (fname). When the function is called, we pass along a first name, which is used inside the function to print the full name:
A function with one argument:
def my_function(fname): print(fname + "Refsnes") my_function("Emil") my_function("Tobias") my_function("Linus")
Parameters vs Arguments
The terms parameter and argument can be used for the same thing: information that are passed into a function.
An argument is the actual value that is sent to the function when it is called.
def my_function(name): # name is a parameter print("Hello", name) my_function("Emil") # "Emil" is an argument
Number of Arguments
By default, a function must be called with the correct number of arguments.
If your function expects 2 arguments, you must call it with exactly 2 arguments.
def my_function(fname, lname): print(fname + " " + lname) my_function("Emil", "Refsnes")
This function expects 2 arguments, and gets 2 arguments:
def my_function(fname, lname): print(fname + " " + lname) my_function("Emil", "Refsnes")
If you try to call the function with the wrong number of arguments, you will get an error:
def my_function(fname, lname): print(fname + " " + lname) my_function("Emil")
This function expects 2 arguments, but gets only 1:
def my_function(fname, lname): print(fname + " " + lname) my_function("Emil")
Default Parameter Values
You can assign default values to parameters. If the function is called without an argument, it uses the default value:
def my_function(name = "friend"): print("Hello", name) my_function("Emil") my_function("Tobias") my_function() my_function("Linus")
Default value for country parameter:
def my_function(country = "Norway"): print("I am from", country) my_function("Sweden") my_function("India") my_function() my_function("Brazil")
Keyword Arguments
You can send arguments with the key = value syntax.
def my_function(animal, name): print("I have a", animal) print("My", animal + "'s name is", name) my_function(animal = "dog", name = "Buddy")
This way, with keyword arguments, the order of the arguments does not matter.
def my_function(animal, name): print("I have a", animal) print("My", animal + "'s name is", name) my_function(name = "Buddy", animal = "dog")
Positional Arguments
When you call a function with arguments without using keywords, they are called positional arguments.
Positional arguments must be in the correct order:
def my_function(animal, name): print("I have a", animal) print("My", animal + "'s name is", name) my_function("dog", "Buddy")
The order matters with positional arguments:
def my_function(animal, name): print("I have a", animal) print("My", animal + "'s name is", name) my_function("Buddy", "dog")
Mixing Positional and Keyword Arguments
Positional arguments must come before keyword arguments:
def my_function(animal, name, age): print("I have a", age, "year old", animal, "named", name) my_function("dog", name = "Buddy", age = 5)
Passing Different Data Types
You can send any data type as an argument to a function (string, number, list, dictionary, etc.).
Positional arguments must come before keyword arguments:
def my_function(fruits): for fruit in fruits: print(fruit) my_fruits = ["apple", "banana", "cherry"] my_function(my_fruits)
Sending a dictionary as an argument:
def my_function(person): print("Name:", person["name"]) print("Age:", person["age"]) my_person = {"name": "Emil", "age": 25} my_function(my_person)
References
https://www.w3schools.com/java
https://www.w3schools.com/java