We've looked briefly at the structure of Python code: variables, functions, strings, and arguments.

This post is part of #100DaysOfPython, check out yesterday's post here if you haven't already. Or go to the index of all 100 days.

Let's dive deeper into functions today. We'll look at:

  • Defining functions;
  • Calling functions
  • Parameters and arguments; and
  • Returning values;

Think of a function as a name for some code. You can write a few lines of code, give them a name, and then re-use them by just calling it by its name.

Defining functions

To define a function in Python, we use the def keyword. The snippet below defines a function called ask_user_for_name:

def ask_user_for_name():

All the code that should be executed by the function when it is called should be more indented than the line defining the function—that is, have more blank space in front. Notice above how pass has 4 spaces in front of it, whereas def does not.

pass just means "do nothing". It's common to do that while we figure out what this function is going to do!

We can make the function do something. In this case, we're going to make it ask the user for their name, using the input function.

def ask_user_for_name():
    user_input = input('Enter your name: ')

We've done a couple things here:

  1. We've created a variable, called user_input;
  2. We've called the input function (we know it's a function because it has brackets after the keyword);
  3. We've given the input function an argument. The argument is 'Enter your name: '.

When we give a string argument to the input function, it prints that string out to the console and then waits for the user to type something. Whatever the user types is assigned to the user_input variable.

Let's create a new file and put this function inside it. For example, create a file called functions.py and write the code in it.

Then, run the file from your console using python functions.py.

This is what you'll see:

Absolutely nothing.

We've defined the function, but we've not executed it! Let's look at how to do that.

Executing, running, and calling a function are all the same thing!

Functions vs. Methods?

The difference between functions and methods is mainly semantic, but can be important in some cases. We say method when the function is inside a class (we're going to look at this later on in the series!). We say function when it is not inside a class. For now, all we're doing are functions.

Calling functions

Now that we've got our function in the file, Python can call the function. All we have to do is type out the name of the function. Don't forget the brackets!

def ask_user_for_name():
    user_input = input('Enter your name: ')


Our function has a single line in it (indented 4 spaces more than the function definition line), and then we execute the function (not indented, and thus not inside the function itself).

If you put that code in your file, you can now run the program and you'll see some output:

Enter your name: 

We can enter our name...

Enter your name: Jose

And nothing happens. We've got the user input, but we aren't doing anything with it. Let's do something!

Returning values

A function can do anything—remember, it's just a name for a few lines of code.

One of the things that a function can do is return a value. Before returning a value, have a think about what the variable user_name contains in the following snippet:

def ask_user_for_name():
    user_input = input('Enter your name: ')

user_name = ask_user_for_name()  # What is the value of user_name ?

It's a trick question! The value of user_name is a special Python value: None. That just means "no value".

Every function in Python returns None unless we return something different.

Whenever we call our function, Python will run the function code. When it finishes, the function returns. Once the function has ran, we can thing of the code as being this:

user_name = None

We've executed the code inside the function, and all that's left is using the return value for something. In this case, it's getting assigned to the user_name variable.

Let's return something else instead:

def ask_user_for_name():
    return input('Enter your name: ')

user_name = ask_user_for_name()

Save this code in your file and run it. You'll see something like this:

Enter your name: Jose

Nothing happens again, but now let me tell you that user_name contains 'Jose'. We can verify this by printing out the name at the end:

def ask_user_for_name():
    return input('Enter your name: ')

user_name = ask_user_for_name()

Once again, save and run it. You'll see something like this:

Enter your name: Jose

The name is printed out!

Parameters and arguments

Something great about functions is that you can give them values to work with. That means they don't have to do all the data collection by themselves. For example, look at this function:

def greeting(name):
    print('Hello ' + name)


If we write that code above, what happens is the greeting function is called with an argument: 'Jose'. That value is assigned to the name variable inside the greeting function. Then, the function uses the variable to print a greeting out to the console.

The variable name only exists inside the function. You cannot use it outside the function. For example, the following would give you an error:

def greeting(name):
    print('Hello ' + name)


Even though the variable name is created inside the greeting function when it runs; it is destroyed at the end of the function. Then, trying to print(name) gives us an error because Python doesn't know what name is.

I hope this was a good intro to functions and some of the ways we can use them. Functions are extremely powerful (even if they don't seem like it just now!), and we are going to be using them extensively throughout the next 96 days.

Stay tuned, and I'll see you on the next one!