IF and DEF - Part 7

The missing manual for using PostScript

IF and DEF - Part 7

Postby showmyiq » Sun Apr 07, 2013 5:04 pm

In PostScript we can define functions too. The syntax is very simple.

/functionname { functionblock } def

For example let’s say our function is with name – addtwonumbers.
The function will just add the top two numbers from the stack. So the function block will be – add.
So, the definition of this function will be:
/addtwonumbers { add } def

Now let’s test the following code:

/addtwonumbers { add } def
1 2

When you execute the result should be 3. As you can see the calling of the function is constructed by the function name only.

If you want to define a variable – you should use the same method, but this time removing the brackets {}. For example you want to say that x=10. Then you should use this line: /x 10 def

Now about the if statement? The if statement is using conditional statements (like the other programming languages). You know what is Boolean value? It’s such a value that is either true or false. In computer language – 0 or 1 (0-false, 1-true). So the if statement is with the following syntax:

booleanvalue { block } if

It means if the booleanvalue is true, then proceed with the commands written in the block section. If the booleanvalue is not true – then do nothing, just continue with the next row from the source.

The conditional statements are very good organize, like those in C++, C, perl, etc.

Command Function
eq Equal (==)
gt Greater than (>)
ge Greater than or equal (>=)
lt Less than (<)
le Less than or equal (<=)
ne Not equal (!=)

Now, let’s see what is going with the stack. If you examine the behavior in close, you will realize that in fact the {} if statement is just looking the top value of the stack (the last pushed in). So, if the value is false – the block will not run and the function will proceed to the next row. If the value is true – the block will be triggered and the commands will run. So if you type something like that:

true { block } if

This will guarantee that the block will be triggered and completed.
So if we type something like that:

4 3 gt { block } def

This will always trigger the block. Why? Well because 4 is greater than 3. The condition statements are like the math operators we already talked about. For example X Y gt will check if the X is greater than Y. If yes, we push in the stack value true. If no, we push to the stack value false. As you will see the gt statement is comparing the top 2 values of the stack and destroy them (as most of the operators in PostScript).

Now let’s run the following script:

4 3 gt { 1 2 3 4 5 } if

When you executed it, you can see that you have now 5 elements in the stack – 1 2 3 4 5.
Try now this one:

3 4 gt { 1 2 3 4 5 } if

Now when you execute – the stack is still empty. Why? Well, because 3 is not greater than 4 and we are not triggering the block section – in our case { 1 2 3 4 5 }.

Now, we have all the tools in order to attack our first serious script!
User avatar
Site Admin
Posts: 390
Joined: Sat Sep 15, 2012 9:45 pm

Return to How to use PostScript

Who is online

Users browsing this forum: No registered users and 1 guest