procedure questiontype;
begin
writeln ('Enter the type of question you would like...');
writeln ('1. Add');
writeln ('2. Multiply');
writeln ('3. Subtraction');
writeln ('4. Division');
readln (typeofquestion);
case typeofquestion of
1: add;
2: multiply;
3: subraction;
4: division
else writeln ('Choose again');
end;
end;
The add, multiply, subtraction and division are all procedures. If i put this in the main program, it will work fine, but when i make this as a procedure itself, i get the error undeclared identifier. I've looked on many websites for an example that is like this but i can't find any.
How do make add, multiply, subtraction, division go to their procedures from inside this one?
You have to declare procedures before routines that call them. Although you haven't shown how the other routines are defined, I deduce that they are declared after the routine you have shown.
So you can simply re-order your code so that add, multiply, subtraction and division are defined before they procedure that calls them.
So this will work:
procedure add;
begin
//do something;
end;
procedure questiontype;
begin
add;
end;
But this will not compile:
procedure questiontype;
begin
add;
end;
procedure add;
begin
//do something;
end;
Pascal and its variants are compiled in a single pass and if the compiler does not know about a routine at the point at which it is mentioned, it cannot continue.
Pascal does support co-routines where A calls B and B calls A, by the use of a *forward declaration`. For example:
procedure B; forward;
procedure A;
begin
B;
end;
procedure B;
begin
A;
end;
Naturally this is an infinite loop as written which will terminate with a stack overflow (how appropriate!) but there are of course real examples where this is necessary.
However, forward declarations are rarely needed and should be avoided if possible since they increase complexity. Invariably a solution can be found by simply re-ordering your declarations.
As a final aside, the ordering constraint that declaration occurs before use is explicitly mentioned in Brian Kernighan famous article, Why Pascal is Not My Favorite Programming Language.