writeln
and write
In the previous chapter we have seen that writeln
takes a string within parentheses and prints the string.
The parts of programs that actually do work are called functions and the information that they need to complete their work are called parameters. The act of giving such information to functions is called passing parameter values to them. Parameters are passed to functions within parentheses, separated by commas.
Note: The word parameter describes the information that is passed to a function at the conceptual level. The concrete information that is actually passed during the execution of the program is called an argument. Although not technically the same, these terms are sometimes used interchangably in the software industry.
writeln
can take more than one argument. It prints them one after the other on the same line:
import std.stdio; void main() { writeln("Hello, World!", "Hello, fish!"); }
Sometimes, not all of the information that is to be printed on the same line may be readily available to be passed to writeln
. In such cases, the first parts of the line may be printed by write
and the last part of the line may be printed by writeln
.
writeln
advances to the next line, write
stays on the same line:
import std.stdio; void main() { // Let's first print what we have available: write("Hello,"); // ... let's assume more operations at this point ... write("World!"); // ... and finally: writeln(); }
Calling writeln
without any parameter merely completes the current line, or if nothing has been written, outputs a blank line.
Lines that start with // are called comment lines or briefly comments. A comment is not a part of the program code in the sense that it doesn't affect the behavior of the program. Its only purpose is to explain what the code does in that particular section of the program. The audience of a comment is anybody who may be reading the program code later, including the programmer who wrote the comment in the first place.
Exercises
- Both of the programs in this chapter print the strings without any spaces between them. Change the programs so that there is space between the arguments as in "Hello, World!".
- Try calling
write
with more than one parameter as well.