You are currently viewing Defining and calling functions

Defining and calling functions

def greet(name):
    print(f"Hello, {name}! How are you doing today?")


In this example, the greet() function is defined with a single parameter name. When the function is called with an argument (in this case, the string “John”), the function prints a personalized greeting to the console.

To define a function, we start with the def keyword followed by the name of the function (in this case, greet). Inside the parentheses, we specify the parameters that the function takes (in this case, just name). We then define the body of the function, which contains the code that will be executed when the function is called.

To call a function, we simply use its name followed by the parentheses containing any arguments we want to pass to the function. In this example, we call the greet() function with the argument “John”.

When we run this code, we get the following output:

Hello, John! How are you doing today?

Functions are useful for encapsulating reusable pieces of code into a single entity that can be called from multiple places in our program. This can help make our code more modular and easier to maintain. We can also pass arguments to functions to make them more flexible and customizable based on the specific use case.

Leave a Reply