Go to the Next or Previous section, the Detailed Contents, or the Amiga E Encyclopedia.


3.1.5 Changing the example

Before we change the example we must learn something about WriteF. We already know that the characters `\n' in a string mean a linefeed. However, there are several other important combinations of characters in a string, and some are special to procedures like WriteF. One such combination is `\d', which is easier to describe after we've seen the changed example.

PROC main()
  WriteF('My first program\n')
  fred()
ENDPROC

PROC fred()
  WriteF('...brought to you by the number \d\n', 236)
ENDPROC

You might be able to guess what happens, but compile it and try it out anyway. If everything's worked you should see that the second message prints out the number that was passed as the second parameter to WriteF. That's what the `\d' combination does--it marks the place in the string where the number should be printed. Here's the output the example should generate:

My first program
...brought to you by the number 236

Try this next change:

PROC main()
  WriteF('My first program\n')
  fred()
ENDPROC

PROC fred()
  WriteF('...the number \d is quite nice\n', 16)
ENDPROC

This is very similar, and just shows that the `\d' really does mark the place where the number is printed. Again, here's the output it should generate:

My first program
...the number 16 is quite nice

We'll now try printing two numbers.

PROC main()
  WriteF('My first program\n')
  fred()
ENDPROC

PROC fred()
  WriteF('...brought to you by the numbers \d and \d\n', 16, 236)
ENDPROC

Because we're printing two numbers we need two lots of `\d', and we need to supply two numbers as parameters in the order in which we want them to be printed. The number 16 will therefore be printed before the word `and' and before the number 236. Here's the output:

My first program
...brought to you by the numbers 16 and 236

We can now make a big step forward and pass the numbers as parameters to the procedure fred. Just look at the differences between this next example and the previous one.

PROC main()
  WriteF('My first program\n')
  fred(16, 236)
ENDPROC

PROC fred(a,b)
  WriteF('...brought to you by the numbers \d and \d\n', a,b)
ENDPROC

This time we pass the (local) variables a and b to WriteF. This is exactly the same as passing the values they store (which is what the previous example did), and so the output will be the same. In the next section we'll manipulate the variables by doing some arithmetic with a and b, and get WriteF to print the results.


Go to the Next or Previous section, the Detailed Contents, or the Amiga E Encyclopedia.