Procedures
Cambridge Pseudocode Lesson 12 2:02 English narration · English + 中文 subtitles burned in
Chapters
Transcript
A procedure is a named block of steps.
过程是一段有名字的步骤。
You write it once, between PROCEDURE and ENDPROCEDURE, and then run it with CALL — as many times as you like.
你在 PROCEDURE 和 ENDPROCEDURE 之间把它写一次, 然后用 CALL 来运行它——想运行多少次都行。
And notice what the output shows: the block itself did not run when it was written.
再注意输出显示的东西:这段代码在被写下来的时候并没有运行。
Nothing inside a procedure happens until something calls it.
在有东西调用它之前,过程里面什么都不会发生。
A parameter lets you send a value in, and it goes in brackets after the name, with its type — exactly like a DECLARE.
参数让你把一个值送进去,它写在名字后面的括号里,还带上类型—— 和 DECLARE 一模一样。
Now the same block can do its job to two different values: Ada the first time, Sam the second.
现在同一段代码可以对两个不同的值做同样的事: 第一次是 Ada,第二次是 Sam。
That is the whole point of a parameter, and it is why one procedure can replace ten copies of almost-the-same code.
这就是参数的全部意义, 也是为什么一个过程可以替掉十份几乎一样的代码。
Now the idea you cannot get from the words, because the words are nearly identical.
现在说那个光看字面学不会的概念,因为两种写法的字几乎一样。
A normal parameter is a COPY.
普通参数是一份"副本"。
The procedure gets its own box, and whatever it does to that box, the caller's variable never moves.
过程拿到它自己的盒子, 不管它对那个盒子做什么,调用者的变量都不会动。
BYREF changes that completely: there is only one box, shared between the two, so the name inside the procedure and the name outside it are the same store.
BYREF 把这件事彻底改变: 只有一个盒子,被两边共用, 所以过程里面那个名字和外面那个名字是同一个存储。
Here it is doing something.
这是它在起作用的样子。
X is five, the procedure doubles the parameter, and X comes out as ten — the procedure reached out and changed the caller's variable.
X 是 5,过程把参数翻倍,而 X 出来是 10—— 过程伸出手去改动了调用者的变量。
Take the word BYREF back out of that header and the same program prints five instead, because Double would have doubled its own copy and thrown it away.
把那个头部里的 BYREF 拿掉,同样的程序会打印 5, 因为 Double 只会把它自己的副本翻倍,然后扔掉。
Four things to take with you.
带走四点。
One: PROCEDURE names a block, and ENDPROCEDURE closes it.
第一:PROCEDURE 给一段代码起名字,ENDPROCEDURE 关闭它。
Two: you must write CALL to run it.
第二:你必须写 CALL 才能运行它。
Three: a plain parameter is a copy, so the caller is safe.
第三:普通参数是副本,所以调用者是安全的。
Four: BYREF makes it the real variable, so it can change.
第四:BYREF 让它变成真正的那个变量,所以它可以被改变。
Now do the three tasks.
现在去做那三道题。