How to call the function in Python

"Learn how to call functions in Python with an example: def add_numbers(a, b): return a + b; print(add_numbers(1, 2))"

Calling a Function in Python

The basics of calling a function in Python are simple: provide the function name followed by any necessary parameters that are required for that function. To show this in action, let's look at an example of a simple function:

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

To call the function, simply use the function name followed by parentheses. In this case, the function is called hello_world():

hello_world()
# Output: Hello World!

The parentheses are needed in order to execute the function. Without them, the function is not actually called. This can be seen in the following example:

hello_world
# Output: 

As you can see, the function does not actually get called until the parentheses are used. You can also provide parameters to the function when calling it. For example, here is a simple function that takes two parameters:

def hello_name(name, age):
    print("Hello", name + "!", "You are", age, "years old.")

To call this function, you need to provide two parameters, a name and an age:

hello_name("John", 20)
# Output: Hello John! You are 20 years old.

As you can see, the two parameters, "John" and 20, are passed to the function and used in the print statement. This is how you call functions in Python, by providing the necessary parameters. You can also call functions without parameters if they don't require any.

Answers (0)