8. Functions
1. Simple Definition & Meaning
A Function is a block of code that is given a name. You write the code once, and then you can use that name to run the code as many times as you want without re-writing it!
2. Real-World Examples
- Example 1: Think of a function like a "Bark" button on a robot dog. When you press it, the dog barks. You don't have to teach it how to bark every single time!
- Example 2: A juice machine. You put fruits in (Arguments), the machine works (Function), and gives you juice (Return Value).
3. Important Points
- Start a function using the word
def. Example:def say_hello(): - You must "call" a function to make it run by typing its name with parentheses:
say_hello(). - Functions can take inputs called arguments and give back results using
return.
📝 Exam Notes (Quick Revision)
- def Keyword: Used to define a function.
- Arguments: Information passed into a function.
- Return: Sends a value back to the line that called the function.