Functions - Learning to Program with Python 3 (basics)




Greetings and welcome to part 6 of the Python 3 basics series, in this tutorial we are going to cover functions. The idea of functions is to either take blocks of code that you plan to run a few times, and just consolidate it to one spot, or to also take a block of code and make it slightly more dynamic, to take in unique parameters to output something based on those. We'll get a better on idea on what they do by just using them, however, so let's revisit our game. Let's say we've got our game board:

game = [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]]

Before any move is chosen, we want to visualize the board, we don't expect the players to memorize positions, so we'd have:

print("   0  1  2")
for count, row in enumerate(game):
    print(count, row)

Giving us:

   0  1  2
0 [0, 2, 0]
1 [0, 0, 0]
2 [0, 0, 0]
>>> 

Now, imagine a player inputs a move, and we set that move. For now, let's just set the move with code rather than taking in user input just yet. Say something like:

game[0][1] = 2

Well, after this, we're going want to visualize the map, so we'd call this again:

print("   0  1  2")
for count, row in enumerate(game):
    print(count, row)

Our code now looks like:

game = [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]]

print("   0  1  2")
for count, row in enumerate(game):
    print(count, row)

game[0][1] = 2

print("   0  1  2")
for count, row in enumerate(game):
    print(count, row)

So the block of code is repeated twice now already, and obviously we can't keep this up for long! Any time we find that we've got repetition in our code, it should probably be a red flag that you can make it better. In this case, the replication is exact, but, even if there are some slight differences, you should know to probably use a function to handle for it. So, in our case, we can begin defining our function with def then the name, then parameters inside of parenthesis, followed by a colon:

def game_board():

In this case, we're not going to pass parameters, but we will cover doing this later. Now, for our function, we're just going to pass the code that we've been using:

def game_board():
    print("   0  1  2")
    for count, row in enumerate(game):
        print(count, row)

Now, we can call this to run by saying game_board(). A common mistake is to forget the parenthesis, and call game_board instead... which just points to the function itself, not for it to actually run!

Finally, for us to display the game board, make a move, then display it again, our code can look like:

game = [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]]


def game_board():
    print("   0  1  2")
    for count, row in enumerate(game):
        print(count, row)


game_board()

game[0][1] = 2

game_board()

Giving us:

   0  1  2
0 [0, 0, 0]
1 [0, 0, 0]
2 [0, 0, 0]
   0  1  2
0 [0, 2, 0]
1 [0, 0, 0]
2 [0, 0, 0]
>>> 

Okay, now the moves that we play are also going to be getting repetitive, like, the pattern of moving and then calling game_board(). Thus, we probably want to just pass the next move as a parameter in the function, instead of separate from it. So, we're going to use the next tutorial to talk about function parameters!

The next tutorial:





  • Introduction to Python 3 (basics) - Learning to Program with Python 3
  • Tuples, Strings, Loops - Learning to Program with Python 3 (basics)
  • Lists and Tic Tac Toe Game - Learning to Program with Python 3 (basics)
  • Built-in Functions - Learning to Program with Python 3 (basics)
  • Indexes and Slices - Learning to Program with Python 3 (basics)
  • Functions - Learning to Program with Python 3 (basics)
  • Function Parameters and Typing - Learning to Program with Python 3 (basics)
  • Mutability Revisited - Learning to Program with Python 3 (basics)
  • Error Handling - Learning to Program with Python 3 (basics)
  • Calculating Horizontal Winner (tic tac toe) - Learning to Program with Python 3 (basics)
  • Vertical Winners - Learning to Program with Python 3 (basics)
  • Diagonal Winners - Learning to Program with Python 3 (basics)
  • Bringing Things Together - Learning to Program with Python 3 (basics)
  • Wrapping up Tic Tac Toe - Learning to Program with Python 3 (basics)
  • Conclusion - Learning to Program with Python 3 (basics)