Named Arguments [1/9]
Named Arguments
Arguments of a println statement must be separated by comma symbols (,). The first statement contains 3 arguments. The arguments are:
- "A day has "
- 24*60
- " minutes"
The second statement contains 2 arguments. The arguments are:
- fg: /pink
- "ZedLX"
Named argument fg of println statement can be used to change the foreground color.
Names of named arguments must be followed by a colon symbol (:).
When a named argument is used in a println statement, all arguments of the println statement must be enclosed inside square brackets [].
Named arguments
x and
y of
println statement change the position of text on the screen.
[Run the program]
Named arguments must be specified before other arguments in a println statement.
Exercises:
1. Un-comment the last line of the program. To un-comment it, remove the two initial symbols << . [Run the program]
2. Add a statement such that the program also displays the result of expression 7*8 at position (100, 400).
3. Add a statement such that the program also displays the result of expression 22/3 at position (200, 400) in orange color. The foreground color can be set by the named argument fg.
- To track how far you have progressed, click on the 'Overview' button at the top left menu.
- Remember to do some regular breaks from your learning journey.
The first argument of the clearScr procedure sets the global background color. The second argument sets the global foreground color. [Run the program]
The named argument height of println statement sets the line height.
Remember:
- when a named argument is used in a println statement, all arguments must be put inside square brackets [].
- The strings to be printed out must be placed after all named arguments.
Exercises 1-5:
Un-comment the last five lines, one by one, by removing the symbols /// . Each time, examine the produced error messages. Then try to correct each error.
In a free form programming language, the whitespace is mostly unimportant. A program can be divided across lines and separated by blanks as one pleases.
The words of the language cannot be split up. Also, literal expressions like strings and numbers cannot be split up.
Freedom is sometimes dangerous. Badly formatted programs are hard to read, so many errors tend to creep in. That's why we won't write disorderly programs, written like the second statement of this program.
Both statements of this program are identical and produce identical results.
[Run the program]
Exercise:
Change the second statement of the program so that it prints out the number of minutes required to make one week.
By default, the println statement prints text
on a black background rectangle.
[Run the program]
To change the color of the background rectangle, use the named argument bg.
Keywords are words in the source code that have a special meaning in the program. They are usually displayed in dark blue color. This program uses two keywords: println and print.
print statements are almost identical to println statements. The only difference is that print statements don't advance the text output to a new line.
Exercises:
1. Change the last statement of the program
so that the text is printed on a red background.
[Run the program]
2. Change the first two println keywords in this program
into print keywords.
[Run the program]
To print a text without the background rectangle, the named argument eff should be set to /off.
Important:
All the named arguments described in this chapter
(fg, bg,
x, y,
height, eff)
are valid only in the println
and print statements.
Exercises:
1. Change the first print statement in the program so that
the text is displayed without the black background rectangle.
[Run the program]
2. Un-comment the last line of the source code
by removing the symbols << .
Correct the error.
[Run the program]
Solution
disc(660, 400, 150, /indigo);
This program animates two strings, displayed in two colors.
[Run the program]
This program uses several keywords: for , println and if .
The keyword if is explained later in this tutorial. For the time being it will remain unexplained.
* * *
You can change the text of the animated strings in this program, and then you can send the program to your friends by using the Share button.
You will have to type in the title of the program, then enter your name as the author, and select a license.
Then, click on the "Create Link" button to create a program link. Click on the program link to see your program in action. To show your program to others, just send them the program link URL, or use the sharing buttons on the run page.
The last line of the program contains two statements. It has been commented out. It pauses the program until the Esc key is pressed. It is desirable to uncomment this line if you decide to share this program.
This program contains multiple errors. Correct all of them.
Programming languages have a very strict syntax. No mistakes are allowed, because computers can't just figure out what you had in mind. All the errors in the source code have to be corrected before a program can be executed.
When reporting an error, the automatic verifier cannot always guess the exact nature of the error. It is not possible to make a perfect automatic verifier, which would be able to correct all errors. Therefore, you cannot assume that an error message can always tell the true nature of an error.
If you can't solve an exercise, take a peek at the solution. You don't need to solve every exercise by yourself. A lot can be learned by simply reading the solution and applying it.
Hints
- when a named argument is used, all arguments must be put inside square brackets [].
- The strings to be printed out must be placed after named arguments.
Solution for the first statement
Change the first println statement into:
println [x:370, y:300, height:100, "ZedLX"];
Solution for the second statement
Change the second println statement into:
println[x:i, y:i, height:50, "ZedLX"];
Solution for the third statement
Change the third println statement into:
println[fg:/yellow, "ZedLX"];
Solution for the fourth statement
Change the fourth println statement into:
println[x:900-i, y:i, height:50, fg:/cyan, "Zed", "LX"];
If you couldn't solve this by yourself, you should reload the page and try again, until you can do it without solutions.
This is a demonstrational program.
Many features of this program will remain unexplained.
[Run the program]
Variables are explained in the next chapter. The first two statements in the source code set the values of variables x, y, and h.
Modify the first two statements so that the variables x, y, and h are assigned some other values in the range from 40 to 500. Try several combinations. From those experiments, try to figure out the meaning of those three variables.
You can also modify the initial value of variable text.
* * *
-- the Share button --
You can change the value of variable text, and then send this program to your friends by using the Share button.
You will have to type in the title of the program, then enter your name as the author, and select a license.
Then, click on the "Create Link" button to create a program link. Click on the program link to see your program in action. To show your program to others, just send them the program link URL, or use the sharing buttons on the run page.
<< F2:Prev - - F4:Next >>