How to Create a Python Function Without Arguments?

Estimated read time 1 min read

To create a Python function without arguments, you can define the function with an empty set of parentheses after the function name, like this:

def greet():
    print("Hello, world!")

In this example, we define a function called greet that doesn’t take any arguments.

The function body consists of a single statement that uses the print function to display the message “Hello, world!” to the console.

To call the function, you can simply use its name followed by an empty set of parentheses, like this:

greet()  # Output: "Hello, world!"

In this example, we call the greet function, which prints the message “Hello, world!” to the console.

Note that even though this function doesn’t take any arguments, you can still use variables and other functions within the function body to perform more complex operations.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply