To learn more about Python’s style guide, check out How to Write Beautiful Python Code With PEP 8.
Creating Your Own Functions
Then you add a pair of parentheses. When your function accepts arguments, you provide a list of parameters. Our
shout_and_return() function should only accept one argument, which we name
01:31 A parameter is sort of like a variable, except that it has no value. It’s a placeholder for actual values that will be provided whenever the function is called with one or more arguments. At the end of your function signature, you must put a colon. When you hit enter, you’re inside the function body.
You can see that IDLE shows three dots at the beginning of the line, and my cursor is indented. This is vitally important. Every line that is indented below the function signature is understood to be part of the function’s body. We’ll play around with the indentation a bit later. For now, let’s continue with the function body. Define a variable called
loud_input, which is equal to
input_string—that’s the parameter that we are passing in—
Press Enter. You can see on the three dots that you are still in the function body. And now you call the
print() function with
loud_input as argument. If we would stop now, then our function would return
None, exactly the same as the
print() function. To return
loud_input, we must close the function body with a
When you define a function in IDLE’s interactive window, you need to press Enter twice after the line containing
return for Python to register the function. So if I hit Return, as you can see, IDLE is smart enough to jump out of the function body after the
return statement, but there are still three dots on the beginning of the line.
So I have to press Enter another time to actually jump out of the function body. You can see with the three carets (
>>>) at the beginning that we are now outside of the function body, and Python is waiting for our input.
Awesome. Let’s try out our new function. You call a user-defined function just like any other function. Type the function name followed by a list of arguments enclosed by parentheses. So for our function, it’s
shout_and_return, and with an opening parenthesis, you can see that IDLE already recommends that we put an
input_string as an argument.
And the second time is the return value that IDLE shows. Let’s try to assign the return value to a variable. Let’s call the variable
my_return and set it equal to
shout_and_return, and this time let’s use
"hi" as a lowercase string, close the parentheses at the end, and press Enter.
Okay, that looks good. So now
HI is printed one time. Again, that’s the side effect of the
print() function. And if we are typing
my_return, Enter, then you see that
my_return now contains the value
'HI' in uppercase letters. That’s pretty cool, but there is a caveat. Unlike built-in functions, user-defined functions are not available until they’ve been defined with the
05:35 So if you wouldn’t have defined the function at the top of this window, it wouldn’t be there. So you must define the function before you can call it. To explore a bit further what I mean by this, let’s move to the editor window.
Now let’s see what happens when we just call the
shout_and_return() function in the IDLE editor window without doing anything else. So I type
shout_and_return() and type
"hello" and close it off.
Although you defined the
shout_and_return() function in the IDLE prompt window before, it’s not available in your Python files by default. So again, that’s different to Python’s built-in functions that are available for you.
As you can see in the window on the left side, the error is exactly the same like the error before. Although we now have our
shout_and_return() function in our
functions_and_loops file, the issue is this: we call the function
shout_and_return() in line 1, but we define the function only in line 3, so Python doesn’t know about the function yet when it executes the
shout_and_return() function in line 1—or, to be more precise, when Python tries to execute
shout_and_return() in line 1. Let’s switch
This also holds true for the function body. And you need to pay special attention to the
return statement at the end of the function body. Once Python executes a
return statement, the function stops running and returns the value.
So you really have to remember that the order is important and that you put your
return statement at the end of your function body. Oh, and speaking about order, when we first defined the
shout_and_return() function in the interactive window, I mentioned that the indentation is important.
I save the file, and now I run the file, and as you can see on the left window,
HELLO is printed in uppercase letters, so it works although we only have two spaces at the beginning of the function body.
While this is not a strict rule, there is one rule that you must follow when indenting code in a function’s body: every line must be indented by the same number of spaces. For example, if I only put two spaces here and I save the file and I try running it, then IDLE already shows a window:
unindent does not match any outer indentation level.
I put a line in between so we can see it better. So now, on line 5, it says
print(loud_input), but it’s indented without any spaces, so it’s basically not indented, in comparison to the situation before where it was indented just like the function body with four spaces.
Let’s see what happens now. I save the file and run it, and now we get a
NameError: name 'loud_input' is not defined. That’s because
loud_input right here is actually really not known to Python because that’s the variable that we are having inside of the function, but outside it’s not known.
So you have to be careful where you put your statements and references to variables inside of a function body. Again, you have to indent it correctly and also take good care about where in the order of the function body you put your function calls or statements. Again,
return should always be at the end of your function. Okay, let’s clean the code up a little bit so we don’t have so many blank lines sitting around here.
13:54 And we are almost finished with our own function. I may not point out the indentation level every time in the next lessons, so be careful at which position your cursor is when you’re programming and when you’re following along. All right. And that’s it for now, but your very own function is missing one last detail.
Become a Member to join the conversation.