Passing arguments by reference - Return Type Declarations
พศวัต วิชิตสโร 630710472
Passing Arguments by Reference
การส่งพารามิเตอร์โดยการอ้างอิงช่วยให้ฟังก์ชันสามารถเข้าถึงและแก้ไขค่าของตัวแปรที่ส่งเข้าไปได้
PHP: สามารถส่งพารามิเตอร์ในการ passed by reference ได้โดยใช้สัญลักษณ์ "&" ตอนประกาศพารามิเตอร์
Example Code:
<?php
function foo(&$var)
{
$var++;
}
$a = 5;
foo($a);
// $a is 6 here
?>
Java: ส่งพารามิเตอร์ด้วย passed by reference จะใช้ได้เฉพาะกับ Object แต่ primitive type ส่งจะต้องใช้แบบ passed by value แทน
Example Code:
C: ใช้ pointer ในการส่งพารามิเตอร์แบบ passed by reference
Example Code:
Python: ส่ง Object โดยการ passed by reference และใช้ได้กับทุก data tpye
Example Code:
Variable Number of Arguments
ข้อมูลสามารถส่งผ่านไปยังฟังก์ชันได้โดยใช้ Arguments ซึ่งก็เหมือนกับตัวแปร Arguments จะถูกระบุไว้หลังชื่อฟังก์ชันภายในวงเล็บ
PHP: สามารถรับจำนวนพารามิเตอร์ที่แปรผันโดยใช้ "..."
Example Code:
Java: ใช้ "..." แบบเดียวกับ PHP
Example Code:
C: ใช้ "va_list" สำหรับจำนวนพารามิเตอร์ที่แปรผัน
Example Code:
Python: ใช้ "*args" สำหรับจำนวนพารามิเตอร์ที่แปรผัน
Example Code:
Return Type Declarations
การประกาศประเภทค่าที่คืนช่วยให้แน่ใจว่าฟังก์ชันคืนค่า type ที่ถูกต้อง
PHP: ใช้เพื่อระบุประเภทของค่าที่ฟังก์ชันจะคืนค่า
Example Code:
Java: มีการประกาศประเภทค่าที่คืนโดยใช้ชื่อประเภทก่อนหน้าชื่อฟังก์ชัน
Example Code:
C: ต้องระบุ type ค่าที่คืนในฟังก์ชัน
Example Code:
Python: ประกาศประเภทค่าที่คืนได้ด้วย "->"
Example Code:
Reference :
php Passing Arguments by Reference : https://www.php.net/manual/en/language.references.pass.php
java Passing Arguments by Reference : https://www.geeksforgeeks.org/different-ways-to-achieve-pass-by-reference-in-java/
C Passing Arguments by Reference : https://www.geeksforgeeks.org/pass-by-reference-in-c/
Python Passing Arguments by Reference : https://www.geeksforgeeks.org/pass-by-reference-vs-value-in-python/
php Variable Number of Arguments : https://www.w3schools.com/php/php_functions.asp
java Variable Number of Arguments : https://www.geeksforgeeks.org/variable-arguments-varargs-in-java/
C Variable Number of Arguments : https://www.tutorialspoint.com/cprogramming/c_variable_arguments.htm
Python Variable Number of Arguments : https://www.geeksforgeeks.org/args-kwargs-python/
php Return Type Declarations : https://www.w3schools.com/php/php_functions.asp
java Return Type Declarations : https://www.javatpoint.com/return-statement-in-java
C Return Type Declarations : https://learn.microsoft.com/th-th/cpp/c-language/return-type?view=msvc-170
Python Return Type Declarations : https://docs.python.org/3/library/typing.html