🌟 Chapter 1: A New Mission from King Nyaomaru!
Remember my previous adventures on Necoz?
I survived the mysterious UFO, explored the UFO Kingdom, learned Python loops, and even experienced the biggest earthquake on Necoz!
I thought I had finally earned a peaceful vacation.
But suddenly...
BEEP! BEEP! BEEP! 🚨
My communicator started ringing.
KING NYAOMARU: STAR! EMERGENCY! REPORT TO THE ROYAL PALACE IMMEDIATELY!
I rushed to the palace.
When I arrived, I saw hundreds of robots standing in a line.
Some robots were printing welcome messages. Others were calculating food bills, while some were preparing delicious cat food.
I looked at the king.
STAR: Your Majesty, why are all these robots doing the same tasks again and again?
KING NYAOMARU: Because every time we need to repeat a task, our engineers write the same code again! Our robot factory is becoming too slow!
I smiled.
STAR: Your Majesty, don't worry! I know a magical Python trick that can solve this problem.
KING NYAOMARU: A magical trick? Tell me immediately!
STAR: It's called PYTHON FUNCTIONS! 🐍
⚙️ Chapter 2: The Magic of Python Functions
I opened my laptop and explained:
A function is a reusable block of code that performs a specific task.
Instead of writing the same code repeatedly, we can write it once and use it whenever we need it.
Let's create our first function!
Python Code:
def greet(): print("Welcome to Necoz!") greet() Output:
Welcome to Necoz! King Nyaomaru jumped from his throne!
KING NYAOMARU: WAIT! You wrote the message only once, but you can use it whenever you want?
STAR: Exactly, Your Majesty!
Let's understand the code:
-
def– The keyword used to define a function. -
greet– The name of our function. -
()– Parentheses used for parameters. -
:– Starts the function body. - Indentation – Identifies the code inside the function.
-
greet()– Calls the function and executes its code.
Remember: Defining a function does not execute it. We must call the function to run it!
King Nyaomaru immediately ordered all robots to learn this trick.
Unfortunately, one robot misunderstood the instruction.
It started greeting itself!
ROBOT: Hello, Robo! Nice to meet you, Robo!
STAR: Okay... I think that robot needs a software update. 😂
🍗 Chapter 3: Hungry Cats and Function Parameters
Just when I thought everything was under control...
LAZY CAT: STAR! I WANT FOOD!
Then Milo appeared.
MILO: I WANT FISH!
And Luna appeared.
LUNA: I WANT Chicken!
I looked at the king.
STAR: Your Majesty, we can use something called function parameters!
I opened my laptop and wrote:
Python Code:
def feed_cat(cat_name, food): print(cat_name, "received", food) feed_cat("Lazy Cat", "Fish") feed_cat("Milo", "Chicken") feed_cat("Luna", "Tuna") Output:
Lazy Cat received Fish Milo received Chicken Luna received Tuna King Nyaomaru looked surprised!
KING NYAOMARU: So one function can serve different cats with different food?
STAR: Exactly!
Let's understand two important terms:
- Parameters: Variables written inside the function definition.
- Arguments: Actual values passed to the function when calling it.
In our example:
-
cat_nameandfoodare parameters. -
"Lazy Cat"and"Fish"are arguments.
Now we can use the same function for hundreds of cats!
Lazy Cat raised its paw.
LAZY CAT: Can I receive five meals?
STAR: YOUR MAJESTY, WE HAVE A NEW PROBLEM! 😂
🎣 Chapter 4: The Mystery of the Missing Fish – Python return
Suddenly, the royal kitchen reported a serious problem.
ALL THE FISH SUPPLIES HAD DISAPPEARED!
King Nyaomaru shouted:
KING NYAOMARU: STAR! FIND OUT HOW MANY FISH ARE LEFT!
I smiled.
STAR: Don't worry! Python has another magical keyword called return.
The return statement sends a value back from a function.
Let's create a function:
Python Code:
def remaining_fish(total_fish, eaten_fish): return total_fish - eaten_fish result = remaining_fish(100, 35) print("Remaining fish:", result) Output:
Remaining fish: 65 KING NYAOMARU: AMAZING! So return sends the result back?
STAR: Exactly, Your Majesty!
Remember the difference:
-
print()displays information on the screen. -
returnsends a value back so that we can store it or use it in another calculation.
King Nyaomaru was finally able to calculate the remaining fish.
Lazy Cat looked disappointed.
LAZY CAT: ONLY 65 FISH? THAT'S TERRIBLE NEWS!
🤖 Chapter 5: The Robot Factory Gets Smarter!
The royal engineers were now excited.
They wanted to create a function that could calculate the power level of every robot.
I showed them another example.
Python Code:
def calculate_power(battery): power = battery * 2 return power robot1 = calculate_power(50) robot2 = calculate_power(80) print("Robot 1 Power:", robot1) print("Robot 2 Power:", robot2) Output:
Robot 1 Power: 100 Robot 2 Power: 160 STAR: See? We can reuse the same function with different arguments!
The engineers cheered.
ENGINEERS: WOW! WE DON'T NEED TO WRITE THE SAME CODE AGAIN AND AGAIN!
King Nyaomaru stood up.
KING NYAOMARU: FROM TODAY, EVERY ROBOT MUST USE PYTHON FUNCTIONS!
But suddenly...
BOOOOOOM! 💥
Bad Cat entered the factory.
It pressed a mysterious red button.
The entire factory stopped working!
BAD CAT: HAHAHA! Let's see who can save the factory now!
🚨 Chapter 6: The Final Challenge – Save the Factory!
The factory computer displayed an error.
The robots needed a function to calculate their final energy after charging.
But the engineers had forgotten to write it!
King Nyaomaru looked at me.
KING NYAOMARU: STAR! THIS IS YOUR FINAL TEST!
I quickly opened my code editor.
Python Code:
def charge_robot(current_energy, extra_energy): total_energy = current_energy + extra_energy return total_energy robot_energy = charge_robot(40, 60) if robot_energy >= 100: print("Robot fully charged!") else: print("Robot needs more energy!") Output:
Robot fully charged! Suddenly...
THE FACTORY STARTED WORKING AGAIN!
All the robots began dancing.
Even Bad Cat joined them!
King Nyaomaru stood up and announced:
KING NYAOMARU: CONGRATULATIONS, STAR! YOU HAVE MASTERED THE MAGIC OF PYTHON FUNCTIONS!
I smiled.
STAR: Thank you, Your Majesty. But I think I deserve a vacation now!
Suddenly, Lazy Cat shouted:
LAZY CAT: WAIT! WHO WILL WRITE A FUNCTION TO FEED ME?
I immediately ran towards the UFO.
Some missions are simply too dangerous! 🚀

