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.
00:00 Hello! Welcome to this course on defining your own functions in Python. If you’ve reached a point in your own programming in Python where you’ve started to use other functions and you’re wanting to learn a little bit more about them and especially how to write them, then this course is exactly what you need.
00:19 In this course, we will take a look at what functions are and why they’re useful. We’ll talk about the difference between defining a function and calling a function.
00:28
We’ll spend a lot of time taking a look at how information is passed to and from a function through argument passing and the return
statement.
00:37 Then we’ll spend a lot of time taking a look at some of the features Python provides to really specialize how you provide arguments to a Python function. Then, lastly, we will take a look at how you can let other programmers know how to use your function.
00:58 The goals for this course: After this course, you will know how functions work in Python and why they’re beneficial. You’ll learn how to define and call your own Python functions.
01:08 You’ll learn a lot of mechanisms for passing arguments to your function, and how to return data from your function back to the calling environment.
01:17 I’m Howard, and I’m excited to be leading you through this course. We will start with just taking a general look at functions in Python.
Become a Member to join the conversation.
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?