Moving Input to a Function
Learn how to ask the user for their name by using a function.
We'll cover the following
A function and its advantage
Whenever we have commonly used code, it’s a good idea to move it into a function.
This has two advantages:
- We don’t keep typing the same code
- A single call to
what_is_your_name()
is less disruptive of the overall flow of our function, which lets us concentrate on the important parts. This is a form of abstraction; we replace detailed code with a function call and move the detail into a function.
Try to use a function when typing the same code repeatedly. This is called the DRY-principle: Do not Repeat Yourself. We should also consider breaking code up into functions if it becomes very large. It’s much easier to read a shorter function that calls other functions, especially when we return to a piece of code after a break.
In Hello, World, we declared the main function, making our own functions similar:
Get hands-on with 1400+ tech skills courses.