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
In a program, some operations occur repeatedly in multiple parts of the code. In chess, for example, every time a player makes a move the program must check that the move is not null, that the coordinates are within the board, that the displacement follows the piece’s rules, and, afterwards, that the path is clear of obstacles. Functions prevent duplication because they encapsulate a block of code that can be called whenever needed. As a result, the program becomes more readable, easier to verify through testing, and easier to extend with new rules or features.
Syntax for defining a function
A function is a block of instructions identified by a name. In code, a function definition starts with the header line, which begins with the keyword def, followed by the function name and a pair of parentheses. After the header comes the function body, i.e., the block of instructions that will be executed when the function is called: this block must be indented relative to the header.
# FUNCTION DEFINITION
def greet_white_player():
print("Welcome to the chess game!")
print("White moves first.")
To call a function, you write its name followed by parentheses ().
# FUNCTION CALL
greet_white_player()
Output:
Welcome to the chess game!
White moves first.
Parameters and Arguments
Typically, a function must operate on different data each time it is called. For example, the rook’s movement rule is always the same, but it must be applied each time to different coordinates. To make the operation performed by the function depend on the values it has to work on, one or more variables called parameters are defined in the function header. When the function is called, the arguments—that is, the actual values provided to the function—are specified inside the parentheses: they can be variables or literal values, separated by commas.
At call time, arguments are bound to parameters based on their order: the first argument is assigned to the first parameter, the second to the second, and so on. In this sense, parameters can be seen as “containers” that receive concrete values when the function is called.
In the following example, pezzo and casella are the parameters of the describe_move() function. When the function is called, "King" and "e4" are the arguments and are assigned, in order, to the two parameters.
# piece and square are the parameters of the describe_move function
def describe_move(piece, square):
print(f"The {piece} moved to {square}")
# "King" and "e4" are string literals passed as arguments to the function
describe_move("King", "e4")
Output: The King moved to e4