פרוצדורות
Code Pseudocode נושא 12 2:14 קריינות באנגלית · כתוביות באנגלית ובסינית 中文 שאפשר להחליף ביניהן
פרקים
תרגום טקסט
A long algorithm written as one block is hard to mark and harder to read, and the exam rewards splitting it.
一个长算法写成一整块,既难评分,也更难读, 而考试会奖励把它拆开。
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.
现在说那个光看字面学不会的概念。
A parameter is a COPY.
参数是一份"副本"。
The procedure gets its own box, and whatever it does to that box, the caller's variable never moves.
过程拿到它自己的盒子, 不管它对那个盒子做什么,调用者的变量都不会动。
In your syllabus every parameter works like this.
在你们的大纲里,每个参数都是这样。
A-Level adds a keyword, BYREF, where there is only one box, shared between the two, so the name inside and the name outside are the same store.
A-Level 多了一个关键字 BYREF:只有一个盒子,被两边共用, 所以里面那个名字和外面那个名字是同一个存储。
It is not in the IGCSE syllabus, so never use it in Paper 2.
它不在 IGCSE 大纲里,所以 Paper 2 里绝不要用。
So how does a result get back to the caller?
那结果怎样回到调用者那里?
Make it a FUNCTION and return it.
把它写成 FUNCTION,然后 RETURN 它。
X is five, the function doubles its copy and returns the answer, and the caller stores it, so X comes out as ten.
X 是 5,函数把它的副本翻倍并返回答案,调用者把它存起来,所以 X 出来是 10。
Without the assignment, nothing changes X, and the program prints five instead: a function never touches the caller's variable, it only hands back a value.
没有那个赋值,就没有东西改变 X,程序会打印 5: 函数从不碰调用者的变量,它只交回一个值。
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: to hand a value back, make it a FUNCTION and RETURN it.
第四:要交回一个值,就写成 FUNCTION 并 RETURN 它。
Now do the three tasks.
现在去做那三道题。
הערות
עדיין אין תגובות. היו הראשונים לשאול או לשתף משהו.