Languages
[Edit]
EN

Python - functions

0 points
Created by:
Molly
351

In this article, we would like to show you how to create and call a function in Python.

Quick solution:

def my_function():
    print("Hello World!")


my_function()  # Hello World!

 

1. Create function

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 (ab) and returns the sum of them.

def my_function(a, b):
    return a + b

2. Call function

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).

def my_function(a, b):
    return a + b


result = my_function(1, 2)
print("result =", result)

Output:

result = 3

Alternative titles

  1. Python - how to create and call function
  2. Python - how to define and call function
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Python - fundamentals

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join