Writing Functions
Parts of a Function
- Declaration: It starts with the
defkeyword. - Name: The identifier for the function like
knight_controller,loot_box, etc. - Parameters: Variables listed in parentheses that accept values when the function is called.
- Body: A block of code indented under the function declaration, which performs the function's actions.
- Return Value: Optional statement to output a value. Without it, functions return
None.
Example
knight.py
-
knight_controller: Controls the knight by callingloot_boxtwice and moving right four times. -
loot_box: Defines a set of actions (moving, picking coin) for the knight. -
Parameter: Both functions take a knight object that has methods like
go_right(),go_up(), etc.
Swipe to start coding
Solution
Thanks for your feedback!
knight.py
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you explain what the knight object is and what methods it has?
Can you show how to call these functions with a knight object?
What does the `loot_box` function do in detail?
Awesome!
Completion rate improved to 2.94
Writing Functions
Swipe to show menu
Parts of a Function
- Declaration: It starts with the
defkeyword. - Name: The identifier for the function like
knight_controller,loot_box, etc. - Parameters: Variables listed in parentheses that accept values when the function is called.
- Body: A block of code indented under the function declaration, which performs the function's actions.
- Return Value: Optional statement to output a value. Without it, functions return
None.
Example
knight.py
-
knight_controller: Controls the knight by callingloot_boxtwice and moving right four times. -
loot_box: Defines a set of actions (moving, picking coin) for the knight. -
Parameter: Both functions take a knight object that has methods like
go_right(),go_up(), etc.
Swipe to start coding
Solution
Thanks for your feedback!
knight.py