# Challenge 1: Creating Karel’s world
Let’s start simple. This challenge will help you practice the basics of writing functions. Define
three new functions: 1) to ask the user for a positive integer (representing the size for Karel’s
world), 2) to display the dimensions of Karel’s world to the console, and 3) main(), which
organizes and calls your other functions. Details are below.
Function definition (main):
In main(), is where you will organize and call your other functions. It’s always good
practice to write main() first, so you can plan what your program needs to do.
In your function definitions, it is good practice to put main() either first or last so it is easy
to find. (This means - Don’t put the main() function definition in the middle of your
function definitions. Even though your code will still compile, this is bad programming
practice because it’s confusing for other programmers who may read your code later).
Your main() function should:
- call your (own) function to get user input. The get user input function should return a
value, representing the dimension of Karel’s world.
- Next, call your (own) function to display the dimensions of Karel’s world. We pass one
parameter into this function (the variable representing the dimension of Karel’s world,
that was just returned from the get user input function).
Since it’s your first time writing functions, here is a really big hint! You can use or adapt
this code to get yourself started.
Remember: “return values” return information OUT of a function to whoever needs it,
whereas “parameter passing” passes information INTO a function. We do both, to avoid
having variables with “global scope”, which is bad programming practice. By using return
values and parameter passing, our variables are “local” to a function and will not be
accidentally manipulated. (Review lecture notes if this doesn’t make sense!)
Function definition: Get user input
Ask the user to enter a positive integer representing the dimensions of Karel the Robot’s
world, like in the sample output below. If the user entered a zero or a negative integer,
prompt them to enter input again, until they enter a valid positive integer value. (Hint!
Use a loop).
1
You can assume the user will only enter positive or negative integer values (that is, they
won’t enter strings, booleans or other invalid inputs).
After the loop ends (the user has entered a valid positive integer): return the positive
integer the user entered. This integer represents the dimensions of Karel’s world.
Function definition: Display dimensions of Karel’s world
Pass in, as a parameter, the dimensions of Karel’s world. Print the dimension that the
user entered, like in the output below. (We can assume Karel lives in a square-sized
grid, so that rows and columns are the same dimension).
Now that your functions are all defined, don’t forget to call main(), to begin program execution!
SAMPLE INPUT AND EXPECTED OUTPUT:
Example 1:
Example 2:
Example 3:
2
Get Free Quote!
376 Experts Online