Example of variable length arguments
Here given code implementation process.
/*
C program for
Example of variable length arguments
*/
#include <stdio.h>
#include <stdarg.h> // For variable arguments
void example(int n,...)
{
va_list record;
// va_list with num number of arguments
va_start(record, n);
printf("\n Given %d paramter is ",n);
// Execute loop through by parameter length
for (int i = 0; i < n; ++i)
{
// Display i-th paramter value
printf("\n %d",va_arg(record, int));
}
// Clear the allocate memory
va_end(record);
}
int main()
{
// First paramter indicate number of variable arguments
// Test A
// number of parameters 5
example(5, 1, 4, 3, 9, 6);
// Test B
// number of parameters 2
example(2, 11, 42);
}
Output
Given 5 paramter is
1
4
3
9
6
Given 2 paramter is
11
42
/*
Java Program for
Example of variable length arguments
*/
public class VariableLengthArgs
{
public void example(int...number)
{
// Get the length of parameter list
int n = number.length;
System.out.print("\nGiven " + n + " paramter is \n");
// Execute loop through by parameter length
for (int i = 0; i < n; ++i)
{
// Display parameter value
System.out.println(number[i]);
}
}
public static void main(String[] args)
{
VariableLengthArgs task = new VariableLengthArgs();
// Test
task.example(1, 2, 3);
task.example(4, 6, 2, 6, 2, 88, 45);
}
}
Output
Given 3 paramter is
1
2
3
Given 7 paramter is
4
6
2
6
2
88
45
// Include namespace system
using System;
/*
Csharp Program for
Example of variable length arguments
*/
public class VariableLengthArgs
{
public void example(params int[]number)
{
// Get the length of parameter list
int n = number.Length;
Console.Write("\nGiven " + n + " paramter is \n");
// Execute loop through by parameter length
for (int i = 0; i < n; ++i)
{
// Display parameter value
Console.WriteLine(number[i]);
}
}
public static void Main(String[] args)
{
VariableLengthArgs task = new VariableLengthArgs();
// Test
task.example(1, 2, 3);
task.example(4, 6, 2, 6, 2, 88, 45);
}
}
Output
Given 3 paramter is
1
2
3
Given 7 paramter is
4
6
2
6
2
88
45
package main
import "fmt"
/*
Go Program for
Example of variable length arguments
*/
func example(number...int) {
// Get the length of parameter list
var n int = len(number)
fmt.Print("\nGiven ", n, " paramter is \n")
// Execute loop through by parameter length
for i := 0 ; i < n ; i++ {
// Display parameter value
fmt.Println(number[i])
}
}
func main() {
// Test
example(1, 2, 3)
example(4, 6, 2, 6, 2, 88, 45)
}
Output
Given 3 paramter is
1
2
3
Given 7 paramter is
4
6
2
6
2
88
45
<?php
/*
Php Program for
Example of variable length arguments
*/
class VariableLengthArgs
{
public function example(...$number)
{
// Get the length of parameter list
$n = count($number);
echo("\nGiven ".$n." paramter is \n");
// Execute loop through by parameter length
for ($i = 0; $i < $n; ++$i)
{
// Display parameter value
echo($number[$i]."\n");
}
}
}
function main()
{
$task = new VariableLengthArgs();
// Test
$task->example(1, 2, 3);
$task->example(4, 6, 2, 6, 2, 88, 45);
}
main();
Output
Given 3 paramter is
1
2
3
Given 7 paramter is
4
6
2
6
2
88
45
/*
Node JS Program for
Example of variable length arguments
*/
class VariableLengthArgs
{
example()
{
// Get the length of parameter list
var n = arguments.length;
process.stdout.write("\nGiven " + n + " paramter is \n");
// Execute loop through by parameter length
for (var i = 0; i < n; ++i)
{
// Display parameter value
console.log(arguments[i]);
}
}
}
function main()
{
var task = new VariableLengthArgs();
// Test
task.example(1, 2, 3);
task.example(4, 6, 2, 6, 2, 88, 45);
}
main();
Output
Given 3 paramter is
1
2
3
Given 7 paramter is
4
6
2
6
2
88
45
# Python 3 Program for
# Example of variable length arguments
class VariableLengthArgs :
def example(self, *number) :
# Get the length of parameter list
n = len(number)
print("\nGiven ", n ," paramter is ")
i = 0
# Execute loop through by parameter length
while (i < n) :
# Display parameter value
print(number[i])
i += 1
def main() :
task = VariableLengthArgs()
# Test
task.example(1, 2, 3)
task.example(4, 6, 2, 6, 2, 88, 45)
if __name__ == "__main__": main()
Output
Given 3 paramter is
1
2
3
Given 7 paramter is
4
6
2
6
2
88
45
# Ruby Program for
# Example of variable length arguments
class VariableLengthArgs
def example(*number)
# Get the length of parameter list
n = number.length
print("\nGiven ", n ," paramter is \n")
i = 0
# Execute loop through by parameter length
while (i < n)
# Display parameter value
print(number[i], "\n")
i += 1
end
end
end
def main()
task = VariableLengthArgs.new()
# Test
task.example(1, 2, 3)
task.example(4, 6, 2, 6, 2, 88, 45)
end
main()
Output
Given 3 paramter is
1
2
3
Given 7 paramter is
4
6
2
6
2
88
45
/*
Scala Program for
Example of variable length arguments
*/
class VariableLengthArgs()
{
def example(number : Int *): Unit = {
// Get the length of parameter list
var n: Int = number.length;
print("\nGiven " + n + " paramter is \n");
var i: Int = 0;
// Execute loop through by parameter length
while (i < n)
{
// Display parameter value
println(number(i));
i += 1;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: VariableLengthArgs = new VariableLengthArgs();
// Test
task.example(1, 2, 3);
task.example(4, 6, 2, 6, 2, 88, 45);
}
}
Output
Given 3 paramter is
1
2
3
Given 7 paramter is
4
6
2
6
2
88
45
import Foundation;
/*
Swift 4 Program for
Example of variable length arguments
*/
class VariableLengthArgs
{
func example(_ number :Int...)
{
// Get the length of parameter list
let n: Int = number.count;
print("\nGiven ", n ," paramter is ");
var i: Int = 0;
// Execute loop through by parameter length
while (i < n)
{
// Display parameter value
print(number[i]);
i += 1;
}
}
}
func main()
{
let task: VariableLengthArgs = VariableLengthArgs();
// Test
task.example(1, 2, 3);
task.example(4, 6, 2, 6, 2, 88, 45);
}
main();
Output
Given 3 paramter is
1
2
3
Given 7 paramter is
4
6
2
6
2
88
45
/*
Kotlin Program for
Example of variable length arguments
*/
class VariableLengthArgs
{
fun example(vararg number: Int): Unit
{
// Get the length of parameter list
val n: Int = number.count();
print("\nGiven " + n + " paramter is \n");
var i: Int = 0;
// Execute loop through by parameter length
while (i < n)
{
// Display parameter value
println(number[i]);
i += 1;
}
}
}
fun main(args: Array < String > ): Unit
{
val task: VariableLengthArgs = VariableLengthArgs();
// Test
task.example(1, 2, 3);
task.example(4, 6, 2, 6, 2, 88, 45);
}
Output
Given 3 paramter is
1
2
3
Given 7 paramter is
4
6
2
6
2
88
45
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment