如何在Pascal中的另一个过程中调用一个过程 [英] How do I Invoke a procedure when inside another procedure in Pascal

查看:273
本文介绍了如何在Pascal中的另一个过程中调用一个过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.

所以这将工作:

procedure add;
begin
  //do something;
end;

procedure questiontype;  
begin  
  add;  
end;

但这不会编译:

procedure questiontype;  
begin  
  add;  
end;

procedure add;
begin
  //do something;
end;

Pascal及其变体在单次传递中编译,如果编译器不了解例程提到的点,它不能继续。

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确实支持A呼叫B和B呼叫A的协同程序,通过使用* forward声明`。例如:

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.

作为最后一个,在使用之前发出的声明的排序约束在Brian Kernighan着名的文章中明确提到,为什么Pascal不是我最喜欢的编程语言

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.

这篇关于如何在Pascal中的另一个过程中调用一个过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆