How to Use Python Functions Effectively: 6 Tips to Know

Here are six things you need to know about using these powerful tools in order to write more Pythonic code.

Written by Peter Grant
python-functions
Brand Studio Logo
UPDATED BY
Abel Rodriguez | May 29, 2025
Summary: Python functions help simplify, reuse, and clarify code. Key practices include keeping syntax simple, using return values effectively, handling arguments with defaults or keywords, managing variable scope with namespaces, and documenting inputs, outputs and purpose clearly.

Functions are a powerful programming feature that makes code more readable and maintainable, and utilizing them will improve your code. Here are six tips to help you learn those skills, and start writing excellent functions in Python.

6 Tips for Writing Python Functions

  1. If they’re easy to write, they’re easy to read.
  2. Keep the syntax simple.
  3. Use the return command.
  4. Work with arguments.
  5. Separate the namespace.
  6. Document your functions.

 

1. If They’re Easy to Write, They’re Easy to Read

Functions allow you to write a block of code once and use it multiple times. You only need to write and debug the code once. This saves you lots of time in the future. Functions also make your code easier to maintain because, if you need to update your function at some point, you only need to edit it in one place.

Writing functions can also make your code easier for the reader to understand. If your function has a descriptive name (e.g. a function designed to calculate the tip on a restaurant bill could be called calculate_tip) then anybody reading your code can understand what each line does without having to make sense of the calculations.

Pretend that I’ve written the previously mentioned calculate_tip function and I want to calculate the tip for both myself and my friend. My friend’s bill is 21 dollars, mine is 32 dollars and we each want to leave a 20 percent tip. I can write the following code calling calculate_tip twice to get the tip for each. Don’t worry about the syntax of the function for now, I’ll cover that in a second.

friends_bill = 21

my_bill = 32

tip_percent = 20



friends_tip = calculate_tip(friends_bill, tip_percent)

my_tip = calculate_tip(my_bill, tip_percent)



print(friends_tip)

print(my_tip)

The output of this code will be the amount that both my friend and I should leave for a tip. Notice how easy it is to read and understand that code. For both calculations we simply see a line stating that the tip amount is calculated based on the bill and the desired percentage, which is quite intuitive.

More From Peter Grant4 Python Tools to Simplify Your Life

 

2.  Keep the Syntax Simple

All Python functions use the same basic structure. First you need to define the function, providing it a name and the arguments necessary for the function to process. Then you need to define the code that it will process each time you call it. And, finally, you need to specify the values to return (if any) when you call the function. Let’s use our calculate_tip function example again: 

def calculate_tip(bill, percent):

    tip = bill * percent/100

    return tip

The first line of code defines the function. def tells Python you’re creating a new function. calculate_tip is the name of the function, so Python will run the associated code whenever you call  calculate_tip. The (bill, percent) part means the function requires two inputs when it’s called. The : at the end tells the program to run the following indented code whenever the function is used.

The second line represents the calculations used by this function. Whenever somebody wants to calculate a tip this function multiplies the bill by the desired percentage and divides by 100 (converting the percentage to a decimal) to identify the intended tip amount.

Finally, the last line says the calculated tip is the output from the function, which can be passed to a variable as desired.

Clean Up Your Code5 Ways to Write More Pythonic Code

 

3. Use the Return Command

One neat trick of Python functions is that you can return as many outputs as you like, so long as you assign them to enough variables.

For instance, we can make calculate_tip more useful by having it also return the total bill with the tip included. Then the user can see both how much to leave for a tip and how much to pay in total. To do so we would modify calculate_tip as follows.

def calculate_tip(bill, percent):

    tip = bill * percent/100

    total = bill + tip

    return tip, total

There are two additions to that code. First, we added a line between calculating and returning tip. That line adds the tip to the bill to find the total payment. The second change added total to the return line, telling Python it should return both values whenever we call calculate_tip.

We can store and read both outputs from these functions using slightly updated code as follows:

friends_tip, friends_total = calculate_tip(friends_bill, tip_percent)

my_tip, my_total = calculate_tip(my_bill, tip_percent)



print(friends_tip, friends_total)

print(my_tip, my_total)

Since calculate_tip now returns two outputs, we needed to update the lines of code calling it to receive both outputs. To do so, both lines now have two variables. One stores the calculated tip, the other stores the calculated total. The same is true of the statements printing the outputs. Each line prints the tip, then prints the total.

Looking for More Python Help? We Got You.3 Ways to Write Pythonic Conditional Statements

 

4. Work With Arguments

The inputs required by a function are typically called “arguments.” While they are sometimes called “parameters,” too, arguments is the more accurate term to describe the values passed through the function.

There are a few tricks you can use when working with arguments.

First off, you can provide a default value for each argument directly in the function definition. If you know that you normally tip 20 percent you can enter that as a default value. Then you only need to specify the tip percentage if you want to use a number different than 20 percent. For instance, consider the following code:

def calculate_tip(bill, percent = 20):

    tip = bill * percent/100

    total = bill + tip

    return tip, total



friends_bill = 21

my_bill = 32

tip_percent = 10



friends_tip, friends_total = calculate_tip(friends_bill, tip_percent)

my_tip, my_total = calculate_tip(my_bill)



print(friends_tip, friends_total)

print(my_tip, my_total)

In the function definition you can see that tip_percent is now set to 20, indicating that 20 percent will be used if you don’t specify a value when calling the function. The line calling calculate_tip to return my_tip and my_total only passes my_bill as an input. Since the code doesn’t overwrite the default value of 20 percent, calculate_tip uses 20 percent when performing the calculations.

On the other hand, the variable tip_percent is set to 10 percent and used when calling calculate_tip to identify friends_tip and friends_total. This overwrites the default 20 percent, and performs the calculations using 10 percent.

Python arguments can also be positional or keyword arguments, representing two different ways of specifying their values. Positional arguments are referenced based on their position in the function call, and keyword arguments are specified by referencing the name of the argument in the function call.

 

5. Separate the Namespace

In Python, a namespace is a collection of variables and information about those variables. Functions are able to read values from the main external namespace, but they do not return to the external namespace unless explicitly stated with the global keyword inside the function.

This is important to remember for two reasons:

  1. You need to be careful when defining variables inside your functions. If you aren’t, you may end up using the same variable name as outside the function, and you may end up using the wrong value. 
  2. You need to think carefully about the values that you want to return from the function. If you don’t return a variable as an output, it’s completely inaccessible to the external namespace.

Here’s an example highlighting those effects:

def calculate_tip(bill, percent = 20):

    tip = bill * percent/100

    total = bill + tip

    print(my_bill)



my_bill = 32

tip_percent = 10

calculate_tip(friends_bill, tip_percent)

print(total)

Notice a few specific things about this code. First, my_bill is neither passed into or calculated in calculate_tip. It only exists in the namespace external to calculate_tip. Second, no values are returned from calculate_tip, so the tip and total variables calculated within the function are within that namespace.

When we run that code we get the following output:

32

Traceback (most recent call last):

  File “main.py”, line 18, in <module>

    print(total)

NameError: name ‘total’ is not defined

You can see that calculate_tip successfully printed my_bill (32) because it inherited that information from the external namespace. But the code errored out when printing total because total only exists in the namespace for calculate_tip, and does not exist in the main namespace.

More Advice From Our ExpertsHow to Start Programming in Python: Anaconda 101

 

6. Documenting Your Functions

Documenting your code is extremely important. It’s your way of telling users how to use your code, and why you made the choices you did.

You may be thinking that documentation isn’t important in most of your code because you’re the only person who uses it. However, you’d be surprised at how quickly you’ll forget how your code works. If you ever write a function, then come back two years later, you’ll really appreciate the comments you left for yourself.

You should document any assumptions you made, or highlight reasons why the code has to be the way you programmed it. These inclusions are a good way to communicate with others who may need to understand the calculation methods at a later date. Here’s an example for our calculate_tip function:

def calculate_tip(bill, percent = 20):

    '''

    Calculates the tip and total for a given bill

    and tip percentage. Processes for a single

    bill at a time. 

    

    inputs

    bill: Float. The price to be paid by the person.

    percent: Float. The desired tip to be paid, expressed

             as a percent of the bill.

             

    outputs

    tip: Float. The tip to be paid for the bill and tip percent.

    total: Float. The total price to be paid, including the tip.

    '''

    

    tip = bill * percent/100

    total = bill + tip



    return tip, total

Reading that documentation provides the key information a user needs to use the function correctly. Someone else can now understand the purpose of the function, the limitations, as well as the structure and purpose of the inputs and outputs. 

And that’s it! Now you know six of the most important tips for using Python functions.

Frequently Asked Questions

A Python function is a reusable block of code designed to perform a specific task when given specific inputs from the user or programmer.

To write a function in Python, use the def keyword followed by a description name, using lowercase letters and underscores instead of spaces. After the name, add parentheses. Inside the parentheses, list any input values (arguments). If there’s more than one, separate them with comas. End the line a colon and one the next line add the return statement along with the specific task you want the function to perform.

def my_function (x, y)
    return x + y
 

The return statement in a function sends the values back to the caller, thus producing an output. 

 

Explore Job Matches.