Seller: Gift Shop 1, Order #: 31, Product: Red Mug
Seller: Gift Shop 2, Order #: 31, Product: Red Mug
Seller: Gift Shop 3, Order #: 31, Product: Red Mug
Seller: Gift Shop 4, Order #: 31, Product: Red Mug
Seller: Gift Shop 5, Order #: 31, Product: Red Mug
Seller: Gift Shop 6, Order #: 31, Product: Red Mug
#include<stdio.h>
int add(int first_number, int second_number); pe
void main() {
int first_number = 4;
int second_number = 6;
// เรียกใช้ function และ กำหนดค่า parameter และ
// เรียงลำดับให้ตรงกับ parameters ใน function ที่เรียก
int sum = add(first_number, second_number);
printf("Sum of %d and %d is %d", first_number, second_number, sum);
}
int add(int a, int b) {
int c = a + b;
return c;
}
Output
Sum of 4 and 6 is 10
Java ไม่รองรับการใช้ Named Parameters เช่นเดียวกับภาษา C
public class Main {
static void myMethod(String fname, int age) {
System.out.println(fname + " is " + age);
}
public static void main(String[] args) {
myMethod("Liam", 5);
myMethod("Jenny", 8);
myMethod("Anja", 31);
}
}
Output
Liam is 5
Jenny is 8
Anja is 31
Python รองรับการใช้ Named parameters แต่ใช้ชื่อKeyword-Only Arguments
def greet_person(person, number):
for greeting in range(number):
print(f"Hello {person}! How are you doing today?")
# 1.
greet_person("Elizabeth", 1)
# 2.
greet_person("Ishaan", number=2)
# 3.
greet_person(person="Messi", number=3)
# 4.
greet_person(number=4, person="Ronaldo")
# การใช้ Named parameters ก่อน
# Positional parameters จะทำให้เกิด Error !!
# greet_person(person="Stephen", 10)
Output
Hello Elizabeth! How are you doing today?
Hello Ishaan! How are you doing today?
Hello Ishaan! How are you doing today?
Hello Messi! How are you doing today?
Hello Messi! How are you doing today?
Hello Messi! How are you doing today?
Hello Ronaldo! How are you doing today?
Hello Ronaldo! How are you doing today?
Hello Ronaldo! How are you doing today?
Hello Ronaldo! How are you doing today?
class GFG {
public static void Main()
{
// กำหนดค่าเริ่มต้นของตัวแปร
string val = "Dog";
// เรียกใช้ method และส่งค่าด้วย ref
CompareValue(ref val);
Console.WriteLine(val);
}
static void CompareValue(ref string val1)
{
// Compare the value
if (val1 == "Dog")
{
Console.WriteLine("Matched!");
}
// เปลี่ยนค่าของตัวแปรและส่งกลับ
val1 = "Cat";
}
}
Output
Matched!
Cat
public static class ByRefExample
{
public static void Main()
{
// กำหนดค่าเริ่มต้นของตัวแปร value
var value = 20;
Console.WriteLine("In Main, value = {0}", value);
// เรียกใช้ method และส่งค่าด้วย ref
ModifyValue(ref value);
Console.WriteLine("Back in Main, value = {0}", value);
}
private static void ModifyValue(ref int i)
{
// เปลี่ยนค่าของตัวแปรและส่งกลับ
i = 30;
Console.WriteLine("In ModifyValue, parameter value = {0}", i);
return;
}
}
Output
In Main, value = 20
In ModifyValue, parameter value = 30
Back in Main, value = 30
class Program
{
// การใช้ ref parameters
static void ModifyValue(ref int number)
{
// เปลี่ยนค่าของตัวแปรและส่งกลับ
number *= 2;
}
static void Main()
{
// กำหนดค่าเริ่มต้นของตัวแปร value
int value = 5;
Console.WriteLine("Output: Original value = " + value);
// เรียกใช้ method และส่งค่าด้วย ref
ModifyValue(ref value);
Console.WriteLine("Output: Modified value = " + value);
}
}
Output
Output: Original value = 5
Output: Modified value = 10
ภาษา C ไม่รองรับการใช้งาน Reference parameters หรือ Passing by reference แต่จะใช้ pointer เข้าถึงตำแหน่งของค่า ซึ่งอาจใช้ชื่อว่า Call by reference, Call by pointers, and Pass by pointers
#include <stdio.h>
//ฟังก์ชันจะรับค่าของพารามิเตอร์โดยใช้ pointer (*)
void swap(int* a, int* b)
{
int temp = *a;
*a = *b;
*b = temp;
}
int main(void)
{
int n1 = 5;
int n2 = 10;
// value before swapping
printf(" Before swapping : n1 is %d and n2 is %d\n", n1,n2);
// เรียกใช้ function และใช้ (&) เพื่อส่งที่อยู่ของตัวแปร n1,n2
swap(&n1, &n2);
// value after swapping
printf(" After swapping : n1 is %d and n2 is %d\n", n1,n2);
return 0;
}
Output
Before swapping : n1 is 5 and n2 is 10
After swapping : n1 is 10 and n2 is 5
class Person {
private String name;
private int age;
private int weight;
private int height;
public Person(String name) {
this(name, 0, 0, 0);
}
public Person(String name, int age, int height, int weight) {
this.name = name;
this.age = age;
this.weight = weight;
this.height = height;
}
public void growOlder() {
this.age = this.age + 1;
}
@Override
public String toString() {
return this.name + ", age " + this.age + " years";
}
}
public class Test {
public static void main(String[] args) {
Person joan = new Person("Joan Ball");
System.out.println(joan);
Person ball = joan;
ball.growOlder();
ball.growOlder();
System.out.println(joan);
}
}
// Output:
// Joan Ball, age 0 years
// Joan Ball, age 2 yearsOutput:
// Output:
Joan Ball, age 0 years
Joan Ball, age 2 years
Python รองรับ Ref parameters ในชื่อ pass by Reference และมีข้อกำหนดบางประการ ดังนี้
Pass by value เมื่อส่ง parameter ที่เป็น Immutable Objects (ไม่สามารถเปลี่ยนแปลงสถานะหรือค่าในอ็อบเจกต์นั้นได้) เช่น integers, floats, strings, และ tuples
def modify_immutable(value):
value += 1
print("Inside function:", value)
x = 5
print("Before function:", x)
modify_immutable(x)
print("After function:", x)
# Output
# Before function: 5
# Inside function: 6
# After function: 5
Pass by reference เมื่อส่ง parameter ที่เป็น mutable Objects (สามารถเปลี่ยนแปลงได้หลังจากที่ถูกสร้างขึ้นแล้ว) เช่น list และ dictionaries
Out Parameters มักใช้งานเมื่อมีการส่งค่ากลับแบบหลายค่า (multiple value)
3.1 Example
using System;
class GFG
{
static public void Main()
{
// สร้างตัวแปรโดยไม่ต้องหนดค่าของตัวแปร
int num;
// ส่งตัวแปร num ไปยัง method
// โดยใช้ keyword 'out'
AddNum(out num);
Console.WriteLine("The sum of"
+ " the value is: {0}", num);
}
// method จะส่งค่าของ num กลับไปยังตัวแปรต้นทาง
public static void AddNum(out int num)
{
// กำหนดค่าของตัวแปรใน method
num = 40;
num += num;
}
}
Output
The sum of the value is: 80
using System;
namespace OutParameter
{
class Program
{
public void Show(out int val)
{
int square = 5;
// กำหนดค่าของตัวแปร val ใน method
val = square;
val *= val;
}
entry point of the program
static void Main(string[] args)
{
// กำหนดค่าเริ่มต้นของตัวแปร
// ซึ่งจะไม่มีผลต่อค่าตัวแปรใน method
int val =69;
// สร้าง Object
Program program = new Program();
Console.WriteLine("Value before passing out variable " + val);
// ส่งตัวแปร val ไปยัง method ด้วย out argument
program.Show(out val);
Console.WriteLine("Value after recieving the out variable " + val);
}
}
}
Output
Value before passing out variable 69
Value after recieving the out variable 25
3.2 ความแตกต่าง
ความแตกต่างของ Out parameters ระหว่าง C, Python และ Java มีดังนี้
ภาษา C ไม่รองรับการส่ง multiple values จาก function โดยตรง แต่สามารถใช้ "Pass by pointers" ในการรับข้อมูลของตัวแปร จากการใช้ pointer อ้างอิงตำแหน่งของข้อมูล
#include<stdio.h>
void div(int a, int b, int *quotient, int *remainder) {
*quotient = a / b;
*remainder = a % b;
}
main() {
int a = 76, b = 10;
int q, r;
div(a, b, &q, &r);
printf("Quotient is: %d\nRemainder is: %d\n", q, r);
}
Output
Quotient is: 7
Remainder is: 6
java ไม่รองรับ Out parameter แต่สามารถใช้ List เพื่อการทำงานที่เทียบเคียงได้
public class Outjava {
public static int[] assignValues() {
int[] values = new int[2];
values[0] = 50; // ค่าแรก
values[1] = 100; // ค่าที่สอง
return values;
}
public static void main(String[] args) {
int[] results = assignValues();
System.out.println("Value 1: " + results[0]);
System.out.println("Value 2: " + results[1]);
}
}
Output :
Value 1: 50
Value 2: 100
Python ไม่รองรับ Out parameter แต่สามารถใช้การคืนหลายค่าด้วย tuple
#include <stdio.h>
int fun1(int *ptrvarB)
{
if (ptrvarB == NULL)
{
// Handle NULL pointer input
printf("It is null pointer");
}
else
{
printf("It is not a null pointer");
}
}
int main()
{
int *ptrvarA = NULL;
fun1(ptrvarA);
}
class Addition {
// Method to add two numbers
public int sum(int num1, int num2) {
return num1 + num2;
}
// Method to add three numbers
public int sum(int num1, int num2, int num3) {
return num1 + num2 + num3;
}
}
public class overrideParam{
public static void main(String[] args){
Addition add = new Addition();
int result = add.sum(5, 10);
System.out.println("Sum of two numbers: " + result);
}
}
Output :
Sum of two numbers: 15
Python รองรับ Optional parameter ซึ่งสามารถกำหนดค่า default value ได้เหมือน C#
Value parameters คือ parameter ที่ส่งข้อมูลหรือค่าของตัวแปรที่กำหนดขึ้น ไปยัง method ซึ่งหากมีการเปลี่ยนแปลงค่าของตัวแปรใน method จะไม่ส่งผลต่อค่าของตัวแปรต้นทางที่เรียกใช้
Value parameters เป็นการส่ง parameter แบบปกติในภาษา C#
6.1 Example
using System;
public class GFG {
static public void Main()
{
// กำหนดค่าของตัวแปร
string str1 = "Geeks";
string str2 = "geeks";
//เรียกใช้ method โดยส่ง parameter เป็นค่าของตัวแปร
string res = addstr(str1, str2);
Console.WriteLine(res);
}
public static string addstr(string s1, string s2)
{
return s1 + s2;
}
}
Output
Geeksgeeks
using System;
class Valueparam{
static void Main(string[] args)
{
// กำหนดค่าของตัวแปร
int a = 10;
int b = 25;
//เรียกใช้ method โดยส่ง parameter เป็นค่าของตัวแปร และเก็บใน res
int res = Change(a, b);
Console.WriteLine("res = " + res);
Console.WriteLine("a = " + a);
Console.WriteLine("b = " + b);
}
public static int Change(int x, int y)
{
return x = y;
}
}
Output
res = 25
a = 10
b = 25
6.2 ความแตกต่าง
ความแตกต่างของ Value parameters ระหว่าง C, Python และ Java มีดังนี้
ภาษา C รองรับ Value parameters ในชื่อ Pass by value
#include <stdio.h>
void swap (int a, int b)
{
int temp = a;
a = b;
b = temp;
printf("swap in function, a = %d, b = %d\n", a,b);
}
int main ()
{
int a = 10;
int b = 20;
printf ("Before swap, a = %d, b = %d\n", a, b);
swap (a, b);
printf ("After swap, a = %d, b = %d\n", a, b);
return 0;
}
Output :
Before swap, a = 10, b = 20
swap in function, a = 20, b = 10
After swap, a = 10, b = 20
Java รองรับ Value parameter และ จะส่ง Parameter ด้วยวิธีนี้เสมอไม่ว่าจะเป็น Primative data หรือ Object
class Balloon {
private String color;
public Balloon() {}
public Balloon(String c) {
this.color = c;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
public class Test {
public static void main(String[] args) {
Balloon red = new Balloon("Red"); // memory reference = 50
Balloon blue = new Balloon("Blue"); // memory reference = 100
swap(red, blue);
System.out.println("After the swap method executes:");
System.out.println("`red` color value = " + red.getColor());
System.out.println("`blue` color value = " + blue.getColor());
changeValue(blue);
System.out.println("After the changeValue method executes:");
System.out.println("`blue` color value = " + blue.getColor());
}
// Generic swap method
public static void swap(Object o1, Object o2){
Object temp = o1;
o1 = o2;
o2 = temp;
}
private static void changeValue(Balloon balloon) {
// balloon = 100
balloon.setColor("Red"); // balloon = 100
balloon = new Balloon("Green"); // balloon = 200
balloon.setColor("Blue"); // balloon = 200
}
}
//Output
After the swap method executes:
`red` color value = Red
`blue` color value = Blue
After the changeValue method executes:
`blue` color value = Red
Python รองรับ Value parameters ในชื่อ pass by value และมีข้อกำหนดบางประการ ดังนี้
Pass by value เมื่อส่ง parameter ที่เป็น Immutable Objects (ไม่สามารถเปลี่ยนแปลงสถานะหรือค่าในอ็อบเจกต์นั้นได้) เช่น integers, floats, strings, และ tuples
def modify_immutable(value):
value += 1
print("Inside function:", value)
x = 5
print("Before function:", x)
modify_immutable(x)
print("After function:", x)
# Output
# Before function: 5
# Inside function: 6
# After function: 5
Pass by reference เมื่อส่ง parameter ที่เป็น mutable Objects (สามารถเปลี่ยนแปลงได้หลังจากที่ถูกสร้างขึ้นแล้ว) เช่น list และ dictionaries
using System;
class Param {
// ใช้ params keyword
public static int Add(params int[] numbers)
{
int sum = 0;
foreach (int number in numbers)
{
sum += number;
}
return sum;
}
static public void Main()
{
int result = Add(1, 2, 3, 4);
Console.WriteLine("Result = " + result);
}
}
// ไม่ใช้ params keyword (multiple overloads)
public static int Add(int a, int b)
{
return a + b;
}
public static int Add(int a, int b, int c)
{
return a + b + c;
}
public static int Add(int a, int b, int c, int d)
{
return a + b + c + d;
}
Output
Result = 10
using System;
class Geeks {
// การกำหนด method parameter แบบ params
public static int mulval(params int[] num)
{
int res = 1;
foreach(int j in num)
{
res *= j;
}
return res;
}
static void Main(string[] args)
{
// เรียกใช้ method และส่งค่า parameter
// โดยไม่ต้องกำหนดจำนวนล่วงหน้า
int x = mulval(20, 49, 56, 69, 78);
Console.WriteLine(x);
}
}
#include <stdio.h>
#include <stdarg.h>
// Variadic function to add numbers
int addition(int n, ...){
va_list args;
int i, sum = 0;
va_start (args, n);
for (i = 0; i < n; i++){
sum += va_arg (args, int);
}
va_end (args);
return sum;
}
int main(){
printf("Sum = %d ", addition(5, 1, 2, 3, 4, 5));
return 0;
}