...

/

Customizing Functions

Customizing Functions

Learn to create your own functions for specific tasks.

So far, we have added plenty of functionalities to Edward's world. Be it being able to turn(), move(), perform tasks (place_plant() and remove trash()), communicate (using print() and input()), or even remember stuff (variables!), we have come a long way in learning about Python.

It’s time to improve his world some more. But first, interact with the widget below and then we have a question for you.

Did you find it a bit tiresome or redundant when we had to click the turn button multiple times to turn Edward toward a certain direction?

Wouldn’t it have been easier if we could just tell Edward to turn left or right? Let’s interact with the widget below to see what we mean.

Isn't this much better? We can now easily instruct Edward to turn left or right. But how did we do that?

By creating our own functions!

Press + to interact

User-defined functions in Python

So far, we've only used the ready-made functions. We weren’t concerned with how those functions worked or how they were coded. We would only call (use) them whenever we needed them. Built-in functions (input()print()int(), etc.) are like standard, ready-to-cook recipes that we already know how to use.

You might think that Python has a built-in (ready-made) function for every situation. But we cannot possibly have a ready-made function to meet every need in the world. Instead, Python lets us create functions of our own that we can then use as required.

It’s time we learn how we can create our own functions.

An analogy for functions

Think ...