程式參數
外表
編程上講嘅參數係指傳俾子程式或者方法嘅數值,用嚟控制電腦程式行嗰陣嘅行為:編程員定義一個函數嗰時,可以喺括號入便寫低一啲變數名,嗰啲就係參數;之後個程式每次呼叫呢個函數嗰時,就要提供實際嘅引數(英文叫 argument)俾佢[1][2]。
基礎
[編輯]想像以下嘅程式:
函數 greet(meng2, age) # 界定個函數先
PRINT "哈佬呀," + meng2 + "!"
PRINT "你今年 " + age + " 歲。"
END FUNCTION
CALL greet("阿珍", 20)
CALL greet("阿明", 35)
個程式有 greet 呢個函數,個程式呼叫咗佢兩次。最後結果會出嘅係:
哈佬呀,阿珍! 你今年 20 歲。 哈佬呀,阿明! 你今年 35 歲。
當中 meng2, age 就係個函數嘅參數,每次呼叫個函數嗰陣,入嘅參數值都唔同,而個函數編寫成識得按參數嘅值調整自己嘅行為。噉即係話參數嘅存在,令到函數變得更有彈性,靈活噉處理唔同情況。
用途
[編輯]喺軟件工程上,參數被指係可以提高程式嘅可返用度。有人稱呢種做法為參數化[3],意思即係話同啲程式設多啲參數,並且將程式寫到識得按輸入參數調整行為,噉同一嚿模塊就可以參數改少少,就攞得去第啲地方用[4]。
睇埋
[編輯]引述
[編輯]- ↑ "Passing Information to a Method or a Constructor (Learning the Java Language > Classes and Objects)". The Java™ Tutorials. 喺2021-09-09搵到.
Parameters refers to the list of variables in a method declaration. Arguments are the actual values that are passed in when the method is invoked. When you invoke a method, the arguments used must match the declaration's parameters in type and order.
- ↑ Prata, Stephen (2004). C primer plus (第5版). Sams. pp. 276–277. ISBN 978-0-672-32696-7.
- ↑ 英文:parameterization
- ↑ SWEBOK Pierre Bourque; Robert Dupuis; Alain Abran; James W. Moore, 編 (2004). "Chapter 4: Software Construction". Guide to the Software Engineering Body of Knowledge. IEEE Computer Society. pp. 4–1–4–5. ISBN 0-7695-2330-7.