The ChessAIThon project (2025-1-ES01-KA220-VET-000354329) is co-funded by the European Union. The views and opinions expressed in this publication are those of the author(s) only and do not necessarily reflect those of the European Union or the Spanish Service for the Internationalisation of Education (SEPIE). Neither the European Union nor the National Agency SEPIE can be held responsible for them.
Table of Contents
What happens inside a function stays inside the function…
Print vs Return
Two concepts often confuse beginners: the difference between print() and return. The print() function is used to display text or the value of a variable on the console. The return keyword, instead, allows a function to send one or more values back to the code that called it; these values can be stored in suitable variables and used later.
The update_pawns() function is used to keep track, during a game, of how many white pawns and black pawns are still on the board. The function takes three inputs: the current count of white pawns, the current count of black pawns and the symbol of the piece captured in the last move.
Inside the function, it first checks which piece was captured. If the value of captured_piece is "P", it means a white pawn was captured; therefore the function decrements num_white_pawns by one. If, instead, captured_piece is "p", then a black pawn was captured, and the function decrements num_black_pawns by one. If the captured piece is not a pawn (i.e., it is neither "P" nor "p"), the function does not change either count. Finally, the function returns both updated values.
def update_pawns(num_white_pawns, num_black_pawns, captured_piece):
"""
Updates the pawn count based on the captured piece.
Returns the new pair (num_white_pawns, num_black_pawns).
"""
if captured_piece == "P":
num_white_pawns = num_white_pawns - 1
elif captured_piece == "p":
num_black_pawns = num_black_pawns - 1
# Returns two values; it does not print them
return num_white_pawns, num_black_pawns
Let us assume that both players still have all of their pawns available: initially, therefore, both the white pawns and the black pawns are equal to 8. Next, we simulate a capture: White captures a Black piece. The type of captured piece is stored in the captured_piece variable, which takes the value "p".
At this point, the program calls the update_pawns() function, passing it the current pawn counts and the information about the captured piece. The function returns the new updated values, which are reassigned to the white_pawns and black_pawns variables; however, this return does not produce any output on the console, because return is only used to “hand back” the results to the caller. The values are displayed only on the last line, when the program executes print(white_pawns, black_pawns), showing the effect of the capture on the number of remaining pawns for each colour.
# Let's assume both white and black pawns start at 8
white_pawns = 8
black_pawns = 8
# Let's assume White captures a black pawn
captured_piece = "p"
# Store the values returned by the function
white_pawns, black_pawns = update_pawns(white_pawns, black_pawns, captured_piece)
# Print the values of white_pawns and black_pawns
print(white_pawns, black_pawns)
Output: 8 7