Skip to content

Defining functions

Defining a function

Steps to define a function:

  1. Start with def keyword which introduces function definition

  2. def keyword must be followed by a function name and the parenthesis containing parameters if the function would be expecting inputs.

  3. The body of the function starts on the next line and must be indented

  4. The first statement of the function body can optionally be a function’s documentation string docstring

def function_name():
function body

Example

Let’s define a function that prints a message

def my_function():
print("Hello from my function")

After we have defined the function above, now it’s time to call it

# calling a function
my_function() # output: Hello from my function