Loading video player…

Defining and Calling Python Functions (Overview)

A function is a self-contained block of code that encapsulates a specific task or related group of tasks. This course will show you how to define your own Python function. You’ll learn when to divide your program into separate user-defined functions and what tools you’ll need to do this.

You’ll also learn the various ways to pass data into a function when calling it, which allows for different behavior from one invocation to the next.

In this course, you’ll learn:

  • How functions work in Python and why they’re beneficial
  • How to define and call your own Python function
  • Mechanisms for passing arguments to your function
  • Some differences between how to work with functions in Python vs C++
  • How to return data from your function back to the calling environment

This course will be the most helpful for you if you’re already familiar with the fundamental concepts of Python, including basic data types, lists and tuples, dictionaries, the import statement, conditional statements, and for loops.

Download

Sample Code (.zip)

12.9 KB
Download

Course Slides (.pdf)

2.9 MB
Avatar image for Alan Lawrence

Alan Lawrence on Sept. 10, 2024

Hi, I am really enjoying the Real Python courses - thank you! I am new to Python and I have been working through the Fundamentals course. Today I covered functions which included annotations of function arguments and return values, and using mypy to catch errors with some static analysis.

I ran in to the problem explained in the comments of the code below. In summary, mypy is complaining about my argument and return value annotations but they seem to work fine when I run it. Any suggestions for how I resolve the errors while keeping the annotations?

# mypy doesn't seem to handle the function arg dictionary syntax on WSL which
# is running mypy version 0.971 which it believes is the latest.
# The code runs fine in python though which is version 3.8.
# $ mypy --version
# mypy 0.971 (compiled: yes)
# $ python --version
# Python 3.8.0
#
# I also tried it on Crostini in my Pixelbook in a python venv which is
# running later versions but gives the same errors.
# $ mypy --version
# mypy 1.11.2 (compiled: yes)
# $ python --version
# Python 3.11.2
# $ mypy annotation-dict-args.py 
# annotation-dict-args.py:31: error: Invalid type comment or annotation  [valid-type]
# annotation-dict-args.py:32: error: Invalid type comment or annotation  [valid-type]
# Found 2 errors in 1 file (checked 1 source file)
# $ python annotation-dict-args.py 
# {'r': {'desc': 'radius', 'type': <class 'float'>}, 'return': {'desc': 'area', 'type': <class 'float'>}}
# 3.1415926
# 40.715040096
# 12.5663704
#
# Note: specifiying mypy to use a particular python version with
# --python-version x.y didn't help either.
#
def area(r: {'desc': 'radius', 'type': float}) \
         -> {'desc': 'area', 'type': float}:
    return 3.1415926 * (r**2)

print(area.__annotations__)

print(area(1))
print(area(3.6))
#area('barf on this')
print(area(*[2]))
#area(*(2, 'barf on this too'))
#area(['barf', 'on', 'this'])

Become a Member to join the conversation.