Functions
Welcome to the lesson on functions. Functions are reusable blocks of code that perform specific tasks. They help in organizing and managing code efficiently.
Defining Custom Functions
In Python, you can define your own functions using the def (define) keyword. A function definition starts with the def keyword, followed by the function name and parentheses «( )». The code block within the function is indented.
The syntax for a function:
def function_name :
tab indented code
#Example: Defining a custom function
def greet():
print("Hello, welcome to the Academy!")
#Calling the function
greet()
In this example, we define a function named greet that prints a welcome message. The function is then called using its name followed by parentheses.
Calling Functions
Once a function is defined, you can call it anytime in your code by using its name followed by parentheses «( )». This executes the code within the function.
#Example: Calling a function
def show_message():
print("I’ve Always Wanted To Use That Spell!")
#Calling the function
show_message()
In this example, the show_message() function is defined to print a message. When show_message() is called, the message is printed.
Practical Application
In the Sanctum of Incantations, you will begin creating your own spells (functions).
⋆˖⁺‧₊ Create a function that prints "Welcome to The Magic Collegium!" and call it twice. ₊‧⁺˖⋆
Sample Output:
Welcome to The Magic Collegium!
Welcome to The Magic Collegium!