Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

An Example Function

Before you can understand decorators, you must first understand how functions work. This lesson covers

  • What functions are
  • What goes into functions
  • How functions can be used as arguments for other functions

00:00 In order to talk about decorators, I need to first talk about functions, so you’ll start with an example function. A function is made up of a couple parts.

00:08 It starts with a header, initially with def—short for define—at the beginning, and then the function’s name, followed by parentheses and a colon.

00:17 I’ll discuss what can go in those parentheses in a moment. Below it, you can have a docstring that will describe what this function does—a very good habit. And then below your header, you have the statement.

00:28 The statement is the code that will run when the function is called.

00:32 In order for you to call the function, you use the function’s name and the pair of parentheses. As the article that this tutorial is based upon states, “a function returns a value based on the given arguments.” Arguments, also known as parameters, are what go in those pair of parentheses. Arguments then can be used inside of the function to return values. For this case—let’s say it’s an integer—then my_var is being set to that argument * 5, and that variable is then returned. So again, a very simple example, but you can have multiple arguments, and at the end of the statement, you could be returning multiple values. Let me show you some examples.

01:11 For this example, I’ll be using a REPL named bpython.

01:17 Let me have you make a function. Define the function, then give it a name. In this case, the parameter that we’re putting in is a number.

01:31 Here’s your docstring. And lastly, you will return the number + 1.

01:37 So what happens if you type in add_one? You’ll see that add_one is a function. This is the memory location it’s in. So to call it, we need to add our parentheses. If we put nothing inside there, the interpreter gives you an error saying that this function requires one positional argument, 'number'. So, add 1 to 3, and your function returns 4.

02:01 Add 1 to 9, and you get 10. So, this object add_one—can you copy it also? Sure. So, try this out. Make another variable, add_one_also, and copy add_one into it.

02:15 So, technically, what is add_one_also? If you hit Return, you’ll see that it’s a function. It shows as a reference to add_one, and it’s in the exact same memory location.

02:27 As a function, add_one_also() works the same. Add 1 to 11, and you get 12. So, what does that mean? Functions are first-class objects, just like any other object.

02:40 Let me explain that a little bit deeper next.

Peter T on March 22, 2019

You created “def my_function():” but then you called my_func(), but you should have called my_function(), since my_func() will call another function “def my_funct():” which wasn’t defined yet. This might be confusing to a beginner. I hope this is clear to everyone. Great course so far!

Peter T on March 22, 2019

Arguments are used in between the parenthesis in the “def” header {e.g. def my_function(arg1, arg2)}, but Parameters are used in between the parenthesis when calling the function {e.g. my_function(param1, param2)}.

Chris Bailey RP Team on March 22, 2019

Thanks for the comments Peter! I had noticed the error in the slide when we released the video, and Dan put up the fixed version, calling the correct my_function(). It somehow reverted back, but should be fixed again.

On the arguments vs parameters, I do see a mix of both used when explaining them, example here on python.org: docs.python.org/3/tutorial/controlflow.html#defining-functions

I agree that the use case you have is solid.

Anonymous on April 5, 2019

Great tutorial. What is the name of the Terminal you use.

Chris Bailey RP Team on April 9, 2019

Thanks so much! I’m using Bpython as a REPL (Read, Eval, Print, Loop) tool inside of Visual Studio Code.

Become a Member to join the conversation.