Local Function ถูกนำเข้ามาใช้ใน C# 7.0 คือ Function ที่ถูกสร้างอยู่ใน Function หลัก เพื่อใช้ทำงานเฉพาะตัวของ Local Function นั้นๆ โดยจะช่วยให้โค้ดมีความเป็นระเบียบและอ่านง่าย
Local Function สามารถประกาศและเรียกใช้ได้จากเมธอด, คอนสตรัคเตอร์, Property accessors, Event accessors, Anonymous methods, Lambda expressions, Finalizers และ Local Function อื่นๆ
ไม่สามารถประกาศ Local Function ใน expression-bodied member ได้
Local Function สามารถเข้าถึงตัวแปรและพารามิเตอร์ของ Function หลักได้
Modifiers ที่สามารถใช้กับ Local Function คือ async, unsafe, static และ extern
ไม่สามารถใช้ private กับ Local Function ได้เพราะตัว Local Function เป็น private อยู่แล้ว และไม่สามารถทำให้เป็น public ได้
ไม่สามารถใช้การ Overloading กับ Local Function ได้
มี Local Function หลายตัวได้
ไม่สามารถใช้ Attribute กับ Local Function รวมทั้งพารามิเตอร์และประเภทพารามิเตอร์ของมัน
// C# program to illustrate local functionusingSystem;publicclassProgram{ // Main methodpublicstaticvoidMain(){ // Local FunctionvoidAddValue(inta,intb){Console.WriteLine("Value of a is: "+a);Console.WriteLine("Value of b is: "+b);Console.WriteLine("Sum of a and b is: {0}",a+b);Console.WriteLine();} // Calling Local functionAddValue(20,40);AddValue(40,60);}}
Output
เปรียบเทียบกับภาษา Java/C/Python
Java
ในภาษา Java จะไม่มี Local Function โดยตรงแต่จะสามารถทำเลียนแบบ Local Function ได้โดยใช้ inner classes, anonymous classes และ lambda expressions ในตัวอย่างที่นำมาเสนอจะเป็นการใช้ lambda expressions เพื่อหาค่าแฟกทอเรียล
C
ในภาษา C ก็ไม่มี Local Function เช่นเดียวกับภาษา Java แต่ก็สามารถใช้วิธีอื่นเพื่อมาทดแทน Local Function ได้ คือ static function, การใช้ Macros และ Function Pointers ตัวอย่างเป็นการใช้ Function Pointers เพื่อเป็นการจำลอง Local Function ส่วนนี่คือส่วนที่เป็น Local Function
Python
ในภาษา Python มี Local Function ให้ใช้งานเหมือนกับ C# แต่จะเป็น Function คืนค่า dictionary ที่มีข้อมูลชื่อและค่าของตัวแปรที่อยู่ในขอบเขตของ Function หลัก โดยมี Syntax คือ
และ Local Function ใน Python ไม่ต้องมีการรับพารามิเตอร์ใดๆ
ในตัวอย่างจะมีการเรียกใช้ locals() เพื่อดึงค่าตัวแปร n ที่ส่งเข้ามาในพารามิเตอร์มาดำเนินการหาแฟกทอเรียลของ 5
// C# program to illustrate how can we
// out parameter in local function
using System;
public class Program {
// Main method
public static void Main()
{
// Local Function with out parameter
void MyMethod(string str, out string s)
{
s = str + "for"
+ "Geeks";
}
string a = null;
// Calling Local function
MyMethod("Geeks", out a);
Console.WriteLine(a);
}
}
// Some codeusing System;
using System;
namespace CSharpFeatures
{
public class LocalMethodExample
{
public static void Main(string[] args)
{
int result = add(10, 20); // calling local method
Console.WriteLine("sum of 10 and 20 is: " + result);
// Creating local method
int add(int a, int b)
{
return a + b;
}
}
}
}
Value of a is: 20
Value of b is: 40
Sum of a and b is: 60
Value of a is: 40
Value of b is: 60
Sum of a and b is: 100
GeeksforGeeks
sum of 10 and 20 is: 30
int factorial(int x) {
if (x == 0 || x == 1) {
return 1;
} else {
return x * factorial(x - 1);
}
}
locals()
import java.util.function.Function;
public class LambdaExample {
public static void main(String[] args) {
Function<Integer, Integer> nthFactorial = createFactorialFunction();
// เรียกใช้ฟังก์ชันและแสดงผลลัพธ์
System.out.println(nthFactorial.apply(5)); // Output: 120
}
// สร้างฟังก์ชันแฟกทอเรียลแบบ lambda expression
private static Function<Integer, Integer> createFactorialFunction() {
return new Function<Integer, Integer>() {
@Override
public Integer apply(Integer n) {
return n < 2 ? 1 : n * this.apply(n - 1);
}
};
}
}
#include <stdio.h>
// ประกาศฟังก์ชันเพื่อคำนวณแฟกทอเรียล
int nthFactorial(int n) {
// ฟังก์ชันภายในที่เลียนแบบ local function
int factorial(int x) {
if (x == 0 || x == 1) {
return 1;
} else {
return x * factorial(x - 1); // เรียกใช้ตัวเองแบบ recursive
}
}
return factorial(n); // เรียกใช้ฟังก์ชัน factorial
}
int main() {
// เรียกใช้ฟังก์ชัน nthFactorial และแสดงผลลัพธ์
int result = nthFactorial(5);
printf("Factorial of 5 is: %d\n", result);
return 0;
}
def main():
# การประกาศ Local Function สำหรับคำนวณแฟกทอเรียล
def nth_factorial(n):
if n == 0 or n == 1:
return 1
else:
# ใช้ locals() เพื่อเรียกใช้ฟังก์ชัน nth_factorial ผ่าน symbol table
return n * locals()['nth_factorial'](n - 1)
# เรียกใช้ Local Function และแสดงผลลัพธ์
print(nth_factorial(5))
if __name__ == "__main__":
main()