Last modified: Feb 02, 2026 By Alexander Williams

Python Function Syntax Guide for Beginners

Functions are the building blocks of Python programs. They let you organize code into reusable blocks. This makes your code cleaner and easier to manage.

Understanding function syntax is your first step. This guide will explain every part clearly. You will learn by seeing practical examples.

What is a Function?

A function is a named block of code. It performs a specific task. You define it once and can call it many times.

This avoids writing the same code repeatedly. It is a core concept in programming. Python uses the def keyword to create one.

Basic Function Definition

The simplest function has a name and a body. You use the def keyword followed by the function name and parentheses.

A colon ends the definition line. The body is indented below it.


# Defining a simple function
def greet():
    """This function prints a greeting."""
    print("Hello, welcome!")
    

In this example, greet is the function name. The triple quotes contain a docstring. It describes what the function does.

The function body has one statement. It prints a message to the screen.

Calling a Function

Defining a function does not run its code. You must call it to execute the instructions. To call a function, use its name with parentheses.


# Calling the function
greet()
    

Hello, welcome!
    

When you run this code, Python executes the print statement inside greet. The output appears on your console.

You can call the same function many times. This is the power of reusability.

Function Parameters

Parameters make functions flexible. They are variables listed inside the parentheses in the definition. They act as placeholders for data.

You pass actual values when you call the function. These values are called arguments.


# Function with a parameter
def greet_person(name):
    """Greets a specific person."""
    print(f"Hello, {name}!")
    

# Calling with an argument
greet_person("Alice")
greet_person("Bob")
    

Hello, Alice!
Hello, Bob!
    

The function now accepts a name parameter. The value you provide ("Alice" or "Bob") is used inside the print statement.

This allows the same function to produce different outputs. For more advanced ways to handle arguments, you can explore Python function argument unpacking.

Multiple Parameters and Default Values

Functions can have more than one parameter. Separate them with commas. You can also give parameters default values.

A parameter with a default value becomes optional. If you don't provide an argument, the default is used.


# Function with multiple parameters and a default
def describe_pet(pet_name, animal_type="dog"):
    """Displays information about a pet."""
    print(f"I have a {animal_type} named {pet_name}.")
    

# Calling the function
describe_pet("Willow")
describe_pet("Whiskers", "cat")
    

I have a dog named Willow.
I have a cat named Whiskers.
    

In the first call, only pet_name is provided. The animal_type uses its default value of "dog".

In the second call, both arguments are provided. The default is overridden. Parameters with defaults must come after those without.

The Return Statement

Functions often calculate a value. To send that value back to the caller, use the return statement.

A function without a return statement gives back None. This is a special Python value meaning "nothing".


# Function with a return value
def square(number):
    """Returns the square of a number."""
    result = number ** 2
    return result
    

# Using the returned value
my_number = 5
squared_value = square(my_number)
print(f"The square of {my_number} is {squared_value}.")
    

The square of 5 is 25.
    

The squarereturn statement sends it back. You can store it in a variable like squared_value.

This is how functions become powerful tools for computation. They process input and give you useful output.

Docstrings: Documenting Your Functions

A docstring is a string literal right after the function definition. It describes the function's purpose.

Good docstrings explain what the function does, its parameters, and its return value. They are essential for readable code.


def add(a, b):
    """
    Returns the sum of two numbers.

    Parameters:
    a (int or float): The first number.
    b (int or float): The second number.

    Returns:
    int or float: The sum of a and b.
    """
    return a + b
    

You can access this docstring using help(add) or add.__doc__. It helps others understand your code quickly.

Always write a clear docstring for your functions. It is a best practice that saves time.

Common Pitfalls and Best Practices

Beginners often make simple mistakes. Here are key points to remember.

First, don't forget the colon at the end of the def line. Second, ensure all code in the function body is indented consistently.

Use descriptive names for functions and parameters. Names like calculate_average are better than func1.

Keep your functions small and focused. A function should do one thing well. This makes testing and debugging easier.

Remember that argument order matters when calling functions. For complex scenarios with lists or dictionaries, techniques like argument unpacking can be very useful.

Conclusion

Python function syntax is straightforward but powerful. You start with def, a name, and parentheses.

You add parameters to accept data. You use the return statement to send data back. Docstrings explain your function's job.

Mastering these basics allows you to write modular code. Your programs will be organized and efficient.