Method Parameters
นิสิต หนูนวน 650710560
1. Named parameters
Named parameters คือ การส่ง parameters โดยระบุชื่อ parameter ในการส่ง ซึ่งมีข้อดีคือ ไม่ต้องระบุค่าตามลำดับของ parameters ที่กำหนดใน method
Named parameters เป็นสิ่งที่อำนวยความสะดวกในการเขียน Code โดยเฉพาะ method ที่มี parameter จำนวนมาก ทำให้ง่ายต่อการทำความเข้าใจ และลดความสับสนใจการเขียน
1.1 ข้อควรระวัง !!
หากมีการใช้งานร่วมกับ Positional parameters จะต้องส่งค่าของ Positional parameters มาก่อน Named parameters เสมอ
1.2 Example
public class GFG {
// addstr มี 3 parameters
public static void addstr(string s1, string s2, string s3)
{
string result = s1 + s2 + s3;
Console.WriteLine("Final string is: " + result);
}
// Main Method
static public void Main()
{
// เรียกใช้ Method โดยใช้ Named parameter ในการส่งค่า
addstr(s1: "Geeks", s2: "for", s3: "Geeks");
}
} Output
Final string is: GeeksforGeeksclass NamedExample
{
static void Main(string[] args)
{
// การเรียกใช้ Method แบบปกติ (Positional parameters)
PrintOrderDetails("Gift Shop 1", 31, "Red Mug");
// Named parameter สามารถเรียกใช้ โดยไม่ต้องเรียงลำดับใน method
PrintOrderDetails(orderNum: 31, productName: "Red Mug", sellerName: "Gift Shop 2");
PrintOrderDetails(productName: "Red Mug", sellerName: "Gift Shop 3", orderNum: 31);
// การใช้ Named parameters ร่วมกับ positional parameters
// สามารถใช้งานร่วมกันได้ !! แต่ต้องกำหนดตำแหน่งให้ถูกต้องตามลำดับใน method !!
PrintOrderDetails("Gift Shop 4", 31, productName: "Red Mug");
PrintOrderDetails(sellerName: "Gift Shop 5", 31, productName: "Red Mug");
PrintOrderDetails("Gift Shop 6", orderNum: 31, "Red Mug");
}
static void PrintOrderDetails(string sellerName, int orderNum, string productName)
{
if (string.IsNullOrWhiteSpace(sellerName))
{
throw new ArgumentException(message: "Seller name cannot be null or empty.", paramName: nameof(sellerName));
}
Console.WriteLine($"Seller: {sellerName}, Order #: {orderNum}, Product: {productName}");
}
}Output
Output
หากใส่ Named parameters ก่อน Positional parameters จะทำให้ compiler แจ้งเตือนข้อผิดพลาด เช่น
ตัวอย่าง
s3: "for" เป็น Named arguments ซึ่งประกาศก่อน "Geeks" ที่เป็น positional argument ทำให้เกิดการแจ้งเตือนข้อผิดพลาดจาก compiler
productName: "Red Mug" เป็น Named arguments ซึ่งประกาศก่อนค่า 31 ที่เป็น positional argument ทำให้เกิดการแจ้งเตือนข้อผิดพลาดจาก compiler
"lastName: "Rahul" เป็น Named parameters ซึ่งประกาศก่อน "Kumar" ที่เป็น Positional parameters ทำให้เกิดการแจ้งเตือนข้อผิดพลาดจาก compiler
1.3 ความแตกต่าง
ความแตกต่างของ Named parameters ระหว่าง C, Python และ Java มีดังนี้
C ไม่รองรับการใช้ Named Parameters
ซึ่งในการส่งค่า Parameters จำนวนหลายค่า ต้องแน่ใจว่าจำนวน Parameters ที่ส่ง เท่ากับจำนวน Parameter ใน function และมีการเรียงลำดับค่าที่ถูกต้อง
Output
Java ไม่รองรับการใช้ Named Parameters เช่นเดียวกับภาษา C
ซึ่งจำเป็นจะต้องกำหนด parameters ให้เท่ากับจำนวน parameters ใน method และต้องมีการเรียงลำดับในการส่งให้ตรงกัน
Output
Python รองรับการใช้ Named parameters แต่ใช้ชื่อ Keyword-Only Arguments
Output
2. Reference parameters
Reference parameters หรือ Passing by reference ใน C# ใช้ keyword คือ ref เป็นการส่งค่า parameter ไปยัง method โดยการอ้างอิงตัวแปรนั้นในการทำงาน ซึ่งหากมีการเปลี่ยนแปลงค่าใน method จะมีผลต่อตัวแปรนั้นเมื่อส่งค่ากลับไปยังตัวแปรต้นทาง ซึ่งก่อนที่จะใช้งาน จะต้องทำการกำหนดค่าของตัวแปรนั้นก่อน
2.1 ข้อควรระวัง !!
การใช้งาน Reference parameter ควรคำนึงผลลัพธ์ที่เกิดขึ้น เนื่องจากมีการเปลี่ยนแปลงค่าของตัวแปรเมื่อส่งกลับมายัง method ที่เรียกใช้ ทำให้อาจเกิดการแสดงผลที่ไม่ตรงตามความต้องการได้
2.2 Example
Output
Output
Output
2.3 ความแตกต่าง
ความแตกต่างของ Reference parameters ระหว่าง C, Python และ Java มีดังนี้
ภาษา C ไม่รองรับการใช้งาน Reference parameters หรือ Passing by reference แต่จะใช้ pointer เข้าถึงตำแหน่งของค่า ซึ่งอาจใช้ชื่อว่า Call by reference, Call by pointers, and Pass by pointers
Output
java ไม่รองรับ Reference parameter แต่สามารถใช้ Object เพื่อการทำงานที่เทียบเคียงได้
Python รองรับ Ref parameters ในชื่อ pass by Reference และมีข้อกำหนดบางประการ ดังนี้
Pass by value เมื่อส่ง parameter ที่เป็น Immutable Objects (ไม่สามารถเปลี่ยนแปลงสถานะหรือค่าในอ็อบเจกต์นั้นได้) เช่น integers, floats, strings, และ tuples
Pass by reference เมื่อส่ง parameter ที่เป็น mutable Objects (สามารถเปลี่ยนแปลงได้หลังจากที่ถูกสร้างขึ้นแล้ว) เช่น list และ dictionaries
3. Out Parameters
Out parameters ใน C# ใช้ keyword คือ out เป็นการส่งค่า parameter ไปยัง method โดยการอ้างอิงตัวแปรนั้นในการทำงาน เช่นเดียวกับ reference parameters แต่มีความแตกต่างคือ ต้องมีการกำหนดค่าของตัวแปรภายใน method และไม่จำเป็นต้องกำหนดค่าของตัวแปรเริ่มต้นก่อนใช้งาน เพราะจะไม่มีผลต่อค่าของตัวแปรที่กำหนดใน method
3.1 Example
Output
Output
3.2 ความแตกต่าง
ความแตกต่างของ Out parameters ระหว่าง C, Python และ Java มีดังนี้
ภาษา C ไม่รองรับการส่ง multiple values จาก function โดยตรง แต่สามารถใช้ "Pass by pointers" ในการรับข้อมูลของตัวแปร จากการใช้ pointer อ้างอิงตำแหน่งของข้อมูล
Output
java ไม่รองรับ Out parameter แต่สามารถใช้ List เพื่อการทำงานที่เทียบเคียงได้
Output :
Python ไม่รองรับ Out parameter แต่สามารถใช้การคืนหลายค่าด้วย tuple
Output :
4. Default or Optional Parameters
Optional Parameters คือ parameter ที่มีการกำหนดค่า default value ไว้ใน method ซึ่งหากเรียกใช้ method และส่ง parameter โดยไม่ระบุค่าของตัวแปรที่เป็น Optional parameter ระบบจะนำค่า default value ที่กำหนดใน method มาใช้
4.1 ข้อควรระวัง !!
การกำหนดค่าของ optional parameters จะต้องอยู่หลัง regular parameter เสมอ
4.2 Example
Output
Output
4.3 ความแตกต่าง
ความแตกต่างของ Optional parameters ระหว่าง C, Python และ Java มีดังนี้
ภาษา C ไม่รองรับการใช้ Optional parameter แต่สามารถใช้ การทำ null pointer ในการตรวจสอบการส่งค่า parameter
Output :
Java ไม่รองรับ Optional parameter แต่สามารถใช้ method overloading เพื่อให้มี method มี parameter หลายรูปแบบ
Output :
Python รองรับ Optional parameter ซึ่งสามารถกำหนดค่า default value ได้เหมือน C#
Output :
5. Dynamic Parameters
Dynamic parameters คือ parameter ที่กำหนดขึ้นโดยใช้ keyword dynamic ทำให้ไม่ต้องระบุชนิดของตัวแปร เนื่องจาก Compiler จะไม่มีการตรวจสอบชนิดของตัวแปรในขณะ compile-time แต่จะรับชนิดของตัวแปรในขณะ run time
Dynamic parameters มีประโยชน์สำหรับการจัดการกับชนิดข้อมูลที่ไม่แน่นอน ช่วยเพิ่มความยืดหยุ่นในการเขียน Code
5.1 Example
Output
Output
5.2 ความแตกต่าง
ความแตกต่างของ Dynamic parameters ระหว่าง C, Python และ Java มีดังนี้
C เป็นภาษาแบบ Static typing และไม่มี keyword สำหรับการเปลี่ยนแปลงค่า (dynamic) แบบ C#
Java เป็นภาษาแบบ Static typing และไม่มี keyword สำหรับการเปลี่ยนแปลงค่า (dynamic) แบบ C#
Python เป็น Dynamically typed อยู่แล้ว ซึ่งจะทำการตรวจสอบชนิดข้อมูลในขณะ run-time ทำให้ไม่จำเป็นต้องกำหนดชนิดข้อมูลก่อนใช้งาน
6. Value Parameters
Value parameters คือ parameter ที่ส่งข้อมูลหรือค่าของตัวแปรที่กำหนดขึ้น ไปยัง method ซึ่งหากมีการเปลี่ยนแปลงค่าของตัวแปรใน method จะไม่ส่งผลต่อค่าของตัวแปรต้นทางที่เรียกใช้
Value parameters เป็นการส่ง parameter แบบปกติในภาษา C#
6.1 Example
Output
Output
6.2 ความแตกต่าง
ความแตกต่างของ Value parameters ระหว่าง C, Python และ Java มีดังนี้
ภาษา C รองรับ Value parameters ในชื่อ Pass by value
Output :
Java รองรับ Value parameter และ จะส่ง Parameter ด้วยวิธีนี้เสมอไม่ว่าจะเป็น Primative data หรือ Object
Python รองรับ Value parameters ในชื่อ pass by value และมีข้อกำหนดบางประการ ดังนี้
Pass by value เมื่อส่ง parameter ที่เป็น Immutable Objects (ไม่สามารถเปลี่ยนแปลงสถานะหรือค่าในอ็อบเจกต์นั้นได้) เช่น integers, floats, strings, และ tuples
Pass by reference เมื่อส่ง parameter ที่เป็น mutable Objects (สามารถเปลี่ยนแปลงได้หลังจากที่ถูกสร้างขึ้นแล้ว) เช่น list และ dictionaries
7. Params
keyword params เป็นการกำหนดให้สามารถส่งค่า argument ได้จำนวนหลายค่าตามที่ต้องการโดยไม่ต้องกำหนดจำนวนของ parameters ก่อน ซึ่งจะเป็นประโยชน์เมื่อไม่ทราบจำนวนของ argument ที่ต้องการจะส่ง
7.1 ข้อควรระวัง !!
ในหนึ่ง method สามารถใช้ paramsได้เพียงตัวเดียว และ parameter ที่ใช้ params ต้องเป็นชนิด Array เท่านั้น และจะต้องกำหนดให้เป็น parameters สุดท้ายของ method
7.1 Example
Output
Output
7.2 ความแตกต่าง
ความแตกต่างของ Params ระหว่าง C, Python และ Java มีดังนี้
ภาษา C ใช้ Variadic function สำหรับการรับ argument ที่ไม่จำกัดจำนวน ซึ่งฟังก์ชันที่ใช้บ่อยที่สุดอย่าง printf() , scanf() ก็เป็น Variadic function เนื่องจากสามารถส่งจำนวน argument ได้ตามต้องการ
โดยจะต้องทำการเพิ่ม #include <stdarg.h> ไว้บน header และ ต้องกำหนด fixed argument อย่างน้อย 1 ค่า แล้วตามด้วย (...) เพื่อให้ compiler เข้าใจว่า สามารถรับ arguments ที่ไม่จำกัดจำนวนได้
Output
Java ใช้ Varargs ในการรับ arguments ที่ไม่จำกัดจำนวน โดยใช้ ... ด้านหลังชนิดข้อมูลของ parameter
Output
Python ใช้ keyword *args และ *kwargs ในการรับ parameter ที่ไม่จำกกัดจำนวน arguments โดย
*argsใช้สำหรับรับ argument ที่เป็น Non-Keyword Arguments หรือ ไม่มี keyword ซึ่งจะเก็บในรูปแบบของ tuple และเรียกใช้งานในลักษณะของ list
*kwargsใช้สำหรับ argument ที่ส่งเข้ามาต้องเป็นแบบคีย์เวิร์ด Keyword Argument ซึ่งจะถูกรับเข้ามาแบบ dict ( หรือ dictionary) ทำให้ต้องกำหนด Key ให้กับ argument ด้วย แทนที่จะใส่เฉพาะ Value
Video Presentation
Slide Presentation
Resource
Key resource
Saini, A. (2019). C# | Method Parameters. GeeksforGeeks. https://www.geeksforgeeks.org/c-sharp-method-parameters/
Additional Resources
IronPdf. (2024). C# Optional Parameters (How It Works For Developers). https://ironpdf.com/blog/net-help/csharp-optional-parameters/
Javatpoint. (n.d.). C# Named and Optional Arguments. https://www.javatpoint.com/csharp-named-and-optional-arguments
Javatpoint. (n.d.). C# Out Parameter. https://www.javatpoint.com/c-sharp-out-parameter
Narayan, B. (2015). Different Types Of Method Parameters in C#. C#Corner.https://www.c-sharpcorner.com/UploadFile/efa3cf/different-types-of-method-parameters-in-C-Sharp/
Pal, V. (2023). Understanding Ref and Out Parameters in C#. Medium. https://medium.com/@vndpal/understanding-ref-and-out-parameters-in-c-3259e936a6eb
Shivakumar, S. (2023). C# Dynamic. EDUCBA. https://www.educba.com/c-sharp-dynamic/
Tutorials.EU. (n.d.). C# params in easy words. https://tutorials.eu/a-guide-to-using-csharp-params-to-work-with-parameters/
Wagner et al., (2024). Named and Optional Arguments (C# Programming Guide). Microsoft Learn. https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/named-and-optional-arguments?redirectedfrom=MSDN
Wagner et al., (2024). Methods in C#. Microsoft Learn. https://learn.microsoft.com/en-us/dotnet/csharp/methods#passing-parameters
Joshi, V. (2023). How can we return multiple values from a function in C/C++?. tutorialspoint. https://www.tutorialspoint.com/how-can-we-return-multiple-values-from-a-function-in-c-cplusplus
Kumari, B. (2023). What is Null Pointer in C?. scaler. https://www.scaler.com/topics/null-pointer-in-c/
Sanfoundry. (n.d.). Pass by Value in C. https://www.sanfoundry.com/c-program-pass-by-value/
Shetti, P. (2023). Difference Between Arguments and Parameters in C. Scaler.https://www.scaler.com/topics/c/difference-between-arguments-and-parameters/
Tanisha. (2023). Pass By Reference In C. GeeksforGeeks.https://www.geeksforgeeks.org/pass-by-reference-in-c/
Tutorialspoint. (n.d.). Variadic Functions in C. https://www.tutorialspoint.com/cprogramming/c_variadic_functions.htm
Agile Education. (n.d.). Objects and references. java-programming. https://java-programming.mooc.fi/part-5/4-objects-and-references
Anderson and Pankaj. (2022). Java is Pass by Value, Not Pass by Reference. digitalocean. https://www.digitalocean.com/community/tutorials/java-is-pass-by-value-and-not-pass-by-reference
Great Learning Editorial Team. (2024). Method Overloading In Java With Examples. mygreatlearning. https://www.mygreatlearning.com/blog/method-overloading-in-java/
Jackson-Barnes. (2024). Dynamic Typing Vs. Static Typing, Explained. orientsoftware. https://www.orientsoftware.com/blog/dynamic-typing-vs-static-typing/
Programiz. (n.d.). Java Varargs. https://www.programiz.com/java-programming/varargs
W3schools. (n.d.). Java Method Parameters. https://www.w3schools.com/java/java_methods_param.asp
DH Team. (2024). การใช้งาน *args และ **kwargs ในภาษา Python. devhub. https://devhub.in.th/blog/args-kwargs-python
Educative. (n.d.). Is Python a dynamically typed language?. https://www.educative.io/answers/is-python-a-dynamically-typed-language
ElHousieny, R. (2023). Understanding Pass-by-Value vs. Pass-by-Reference. Linkedin. https://www.linkedin.com/pulse/understanding-pass-by-value-vs-pass-by-reference-elhousieny-phd%E1%B4%AC%E1%B4%AE%E1%B4%B0
Gruppetta, S. (2022). Using Positional Arguments and Named or Keyword Arguments in Python Functions. The Python Coding Book. https://thepythoncodingbook.com/2022/11/11/positional-arguments-and-keyword-arguments-in-python/
Harshkumarchoudhary144. (2024). How to pass optional parameters to a function in Python?. GeeksforGeeks. https://www.geeksforgeeks.org/how-to-pass-optional-parameters-to-a-function-in-python/
Last updated
