EN
Python - functions
0 points
In this article, we would like to show you how to create and call a function in Python.
Quick solution:
xxxxxxxxxx
1
def my_function():
2
print("Hello World!")
3
4
5
my_function() # Hello World!
In the Quick solution we presented how to create a function without arguments.
In this example, we create a function that takes 2 arguments separated by a comma (a
, b
) and returns the sum of them.
xxxxxxxxxx
1
def my_function(a, b):
2
return a + b
You can call a function by typing the function name with the parenthesis. If your function takes some arguments, you need to pass them when the function is called (in our example a
= 1
, b
= 2
).
xxxxxxxxxx
1
def my_function(a, b):
2
return a + b
3
4
5
result = my_function(1, 2)
6
print("result =", result)
Output:
xxxxxxxxxx
1
result = 3