usingSystem;classProgram{publicvoidIntroduceYourself(stringfirstName,stringlastName="Doe"){Console.WriteLine("Hello, my name is "+firstName+""+lastName);}staticvoidMain(string[]args){Programprogram=newProgram();program.IntroduceYourself("John","Smith");program.IntroduceYourself("John");}}
Hello, my name is John Smith
Hello, my name is John Doe
using System;
class GFG {
static public void my_mul(int a) {
Console.WriteLine(a * a);
}
static public void my_mul(int a, int b, int c) {
Console.WriteLine(a * b * c);
}
static public void Main() {
my_mul(4);
my_mul(5, 6, 100);
}
}
16
3000
using System;
using System.Runtime.InteropServices;
class GFG {
static public void my_mul(int num, [Optional] int num2) {
Console.WriteLine(num * num2);
}
static public void Main() {
my_mul(4); // 4*0
my_mul(2, 10); // 2*10
}
}
0
20
using System;
class GFG {
static public void my_mul(int a, params int[] a1) {
int mul = 1;
if (a1.Length > 0) {
foreach (int num in a1) {
mul *= num;
}
} else {
mul = 1;
}
Console.WriteLine(mul * a);
}
static public void Main() {
my_mul(1);
my_mul(2, 4);
my_mul(3, 3, 100);
}
}
1
8
900
#include <stdio.h>
#include <stdarg.h>
int sum(int num, ...) {
va_list args;
va_start(args, num);
int total = 0;
for (int i = 0; i < num; i++) {
total += va_arg(args, int);
}
va_end(args);
return total;
}
int main() {
printf("Sum: %d\n", sum(3, 5, 10, 15));
return 0;
}
Sum: 30
public class Example {
public void display(String mandatory) {
System.out.println("Mandatory: " + mandatory);
}
public void display(String mandatory, String optional) {
System.out.println("Mandatory: " + mandatory + ", Optional: " + optional);
}
public static void main(String[] args) {
Example example = new Example();
example.display("Hello");
example.display("Hello", "World"); ง
}
}
Mandatory: Hello
Mandatory: Hello, Optional: World
public class Example {
public void display(String... args) {
for (String arg : args) {
System.out.println(arg);
}
}
public static void main(String[] args) {
Example example = new Example();
example.display("Hello", "World", "Java");
example.display("Single Argument");
example.display(); // ไม่มีการส่งค่า
}
}
Hello
World
Java
Single Argument
def function(a, b=999):
return a + b
print(function(2, 3))
print(function(1))
5
1000
def myFun(**kwargs):
for key, value in kwargs.items():
print("%s == %s" % (key, value))
myFun(first='Geeks', mid='for', last='Geeks')