Check out a free preview of the full Python Fundamentals course

The "Functions" Lesson is part of the full, Python Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Nina introduces why functions are useful, demonstrates the syntax for a function, and gives examples to demonstrate what returning a value means.

Preview
Close

Transcript from the "Functions" Lesson

[00:00:00]
>> Nina Zakharenko: Okay, so covered all the basic data types, now let's talk about functions. And again, I'm just putting the comments in here to make it easier for you guys to read you don't have to put them into your own code. Functions help us organize our code in a way that's reusable and accepts arguments and even provides defaults where needed.

[00:00:26]
>> Nina Zakharenko: And the purpose of these functions, it's to make our code reusable, right? So if we find ourselves copying and pasting the same thing, the same code multiple times all over a place, that's a good sign that a function might help you. Very quickly, the anatomy of a function, this is the recipe for defining a Python function.

[00:00:48]
We always start the function with the keyword def, followed by a space, that tells Python that we're starting a function definition. Then we come up with a name for the function, so this isn't a good name but for now let's just call it fool. Then we always need the opening parenthesis and optionally we can type in some arguments here or if our function does not accept arguments we type in the closing parenthesis.

[00:01:22]
>> Nina Zakharenko: If it had arguments, we would separate those with a comma. Okay, lastly, a colon, that's our recipe for a function. Now, if we press Enter in the REPL, we'll see these three dots again, it's waiting for more input. Now for those of you who've come from other languages, let's say JavaScript, what did you not see in the function definition?

[00:01:52]
>> Class: Brackets.
>> Nina Zakharenko: Brackets, gold star, exactly right Python doesn't use brackets to differentiate between the scope of what belongs to a function and what doesn't. In Python instead we indicate that with indentation, so, important concept you'll see again and again. But to put in the contents of our function, we can just press tab to insert one level of indentation.

[00:02:27]
What's that?
>> Class: Type eight I think just spaces.
>> Nina Zakharenko: Yes, so in the REPL you can hit tab to just kind of quickly type things in. In your Python files, I recommend using spaces, so when you use an ID, if you hit tab while you're typing your Python code, it'll just convert it into spaces under the hood, so.

[00:02:51]
And to verify that, if you look at the blue bar down here, you'll see mine says, spaces 4, if yours says tab, you might wanna change that but not a big deal right now. Okay, so we're in the REPL we hit tab and now we're gonna have to use that print function, right?

[00:03:13]
Because when we call our function in the REPL if it doesn't do anything, if it just declares some variables, we're not gonna see any output. So let's use print and print out hello.
>> Nina Zakharenko: And let's press Enter again. And you'll see here that Python is still in this, or the Python REPL is still in this dot, dot, dot mode it's still waiting for more input, that's because we can type in more stuff to our function.

[00:03:56]
When we decide that we're done with it, in the REPL, we'll want to hit Enter an extra time until we see these three brackets again, saying that our prompt is ready for input. To call our function we type the name of it, if it accepts arguments we'd wanna pass those in, if it doesn't like in the case of fool, we can just pass in empty parentheses.

[00:04:20]
And when i call that function i see my result result hello, cool right? Okay so, functions can accept no arguments, in this case the function also returns nothing. The print statement, it just does something that displays text, right? But in our production code lets say we probably don't have print statements littered all over the place, right, we know what the code does and so we want function to return a value.

[00:05:01]
So let's make new function and call it, meaning of life, and it's just two empty parenthesis and a colon. And our function is gonna return a value 42, press enter and enter again.
>> Nina Zakharenko: Now we can call the meaning of life function, and we'll see our returned value 42.

[00:05:29]
>> Nina Zakharenko: Just to be very clear on the distinction, I'm gonna make a new variable that says call fool and I'm gonna assign the value of calling foo to it. If I checked what the value of that variable is, we'll see that there’s nothing there, a non-type, right? This function prints something out and does something but does not return anything.

[00:06:00]
If I set the result of calling, the meaning of life function, to the variable x, what will the value of x be?
>> Nina Zakharenko: 42.
>> Class: 42.
>> Nina Zakharenko: Cool, whether your function returns arguments or not depends on what it does, right? Not all functions need to return anything, it's optional.

[00:06:33]
Our functions can also accept arguments, so let's say I had a function called add numbers. After my empty parenthesis, I'm gonna put in the variable that I expect, so I'm gonna expect two numbers, x, and y. I'm gonna separate them with a comma, put in my closing parenthesis, and then put in the colon.

[00:06:59]
That means that no matter what my function gets called in, within the scope of my function, those variables will be called x and y. So I hit enter for new line, I hit tab and then I'm gonna return x + y.
>> Nina Zakharenko: Now I can call this function with any numbers and it will return the result of adding them.

[00:07:34]
I can also set variables here with some numbers in them, say a and b, I can call my add_numbers function with a and b. But remember, add_numbers doesn't know anything about a and b, and it doesn't care, right? Add_numbers only knows about two arguments and within the scope of the function, they're gonna be called x and y.

[00:08:04]
>> Nina Zakharenko: Now, if we forget this recipe for trying to define a function in Python, Python's gonna help us out with the syntax error. So let's try to create some functions, I'm gonna call this one, oops, and if I forget the colon and press enter syntax error, invalid syntax with a helpful arrow pointing to the end of oops, okay?

[00:08:38]
If I forget, my parenthesis syntax error, invalid syntax, right? Makes sense, we're not following the recipe.
>> Nina Zakharenko: The recipe for our function contents, so we have a new line, we have an indentation, and then we can have one or more lines in there, right, so we've seen some simple functions.

[00:09:06]
But let's make a new one called greeting.
>> Nina Zakharenko: And our greeting will take a name.
>> Nina Zakharenko: You can also define a new variable within the scope of this function.
>> Nina Zakharenko: And I'm gonna make my greeting, hello, but you can make it whatever you want and then we're gonna return the greeting plus the name.

[00:09:38]
So to add more content to the function, remember you always need to indent, everything that is indented one level under the function belongs to it. Press enter twice call greeting with Nina and it'll return, hello Nina, remember that return statement is optional and if you don't provide one, the function just returns none.

[00:10:05]
>> Nina Zakharenko: You can also kind of very occasionally, you can use a return statement that has no value, so you'll see that in kind of, when we talk about control flow you'll see that a little bit more. But let's say I have a function fool here, and I set x equal to 5 and I type return with no value, right?

[00:10:31]
It's still valid Python syntax, but if I call let's say x is equal to fool, if I check the type of x, it's gonna be NoneType.
>> Nina Zakharenko: This return statement will return from anywhere within the function In the REPL you don't get helpful hints, like you do when typing a Python file in the IDE.

[00:11:00]
In the IDE if you put anything after the return statement and you've installed the linter like we did in the beginning, you'll see an error about unreachable code or not an error, but a warning about unreachable code. So, if there's no control flow to your program, everything after the return is ignored.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now