Programming with Python

⌘K
  1. Home
  2. Docs
  3. Programming with Python
  4. Functions
  5. Creating and Calling Functions

Creating and Calling Functions

To create a function in Python, we use the def keyword followed by the function name and a pair of parentheses () which may include optional parameters. The function body is defined using indentation.

Syntax:

def function_name():
    # body of the function
    statement(s)

E.g:-

def say_hello():
    print("Hello, World!")

In this example:

  • def is the keyword used to define the function.
  • say_hello is the name of the function.
  • The function body prints “Hello, World!” when the function is called.

To call or execute a function, simply use the function name followed by parentheses.

say_hello()

How can we help?

Leave a Reply

Your email address will not be published. Required fields are marked *