WeixiangYan/CodeTransOcean huggingface dataset:
Task: Leonardo numbers







Python: def Leonardo(L_Zero, L_One, Add, Amount):
    terms = [L_Zero,L_One]
    while len(terms) < Amount:
        new = terms[-1] + terms[-2]
        new += Add
        terms.append(new)
    return terms

out = ""
print "First 25 Leonardo numbers:"
for term in Leonardo(1,1,1,25):
    out += str(term) + " "
print out

out = ""
print "Leonardo numbers with fibonacci parameters:"
for term in Leonardo(0,1,0,25):
    out += str(term) + " "
print out
Task: Leonardo numbers








VB: Option Explicit

Private Sub LeonardoNumbers()
Dim L, MyString As String
    Debug.Print "First 25 Leonardo numbers :"
    L = Leo_Numbers(25, 1, 1, 1)
    MyString = Join(L, "; ")
    Debug.Print MyString
    Debug.Print "First 25 Leonardo numbers from 0, 1 with add number = 0"
    L = Leo_Numbers(25, 0, 1, 0)
    MyString = Join(L, "; ")
    Debug.Print MyString
    Debug.Print "If the first prarameter is too small :"
    L = Leo_Numbers(1, 0, 1, 0)
    MyString = Join(L, "; ")
    Debug.Print MyString
End Sub

Public Function Leo_Numbers(HowMany As Long, L_0 As Long, L_1 As Long, Add_Nb As Long)
Dim N As Long, Ltemp

    If HowMany > 1 Then
        ReDim Ltemp(HowMany - 1)
        Ltemp(0) = L_0: Ltemp(1) = L_1
        For N = 2 To HowMany - 1
             Ltemp(N) = Ltemp(N - 1) + Ltemp(N - 2) + Add_Nb
        Next N
    Else
        ReDim Ltemp(0)
        Ltemp(0) = "The first parameter is too small"
    End If
    Leo_Numbers = Ltemp
End Function
Task: Leonardo numbers




Go: package main

import "fmt"

func leonardo(n, l0, l1, add int) []int {
    leo := make([]int, n)
    leo[0] = l0
    leo[1] = l1
    for i := 2; i < n; i++ {
        leo[i] = leo[i - 1] + leo[i - 2] + add
    }
    return leo
}

func main() {
    fmt.Println("The first 25 Leonardo numbers with L[0] = 1, L[1] = 1 and add number = 1 are:")
    fmt.Println(leonardo(25, 1, 1, 1))
    fmt.Println("\nThe first 25 Leonardo numbers with L[0] = 0, L[1] = 1 and add number = 0 are:")
    fmt.Println(leonardo(25, 0, 1, 0))
}
Task: Leonardo numbers



C#: using System;
using System.Linq;

public class Program
{
    public static void Main() {
        Console.WriteLine(string.Join(" ", Leonardo().Take(25)));
        Console.WriteLine(string.Join(" ", Leonardo(L0: 0, L1: 1, add: 0).Take(25)));
    }

    public static IEnumerable<int> Leonardo(int L0 = 1, int L1 = 1, int add = 1) {
        while (true) {
            yield return L0;
            (L0, L1) = (L1, L0 + L1 + add);
        }
    }
}








Fortran:       SUBROUTINE LEONARDO(LAST,L0,L1,AF)	
       INTEGER LAST	
       INTEGER L0,L1	
       INTEGER AF	
       OPTIONAL AF	
       INTEGER EMBOLISM	
       INTEGER N,LN,LNL1,LNL2	
        IF (PRESENT(AF)) THEN	
          EMBOLISM = AF			
         ELSE			
          EMBOLISM = 1			
        END IF			
        WRITE (6,1) LAST,L0,L1,EMBOLISM	
    1   FORMAT ("The first ",I0,	
     1   " numbers in the Leonardo sequence defined by L(0) = ",I0,
     2   " and L(1) = ",I0," with L(n) = L(n - 1) + L(n - 2) + ",I0)
        IF (LAST .GE. 1) WRITE (6,2) L0	
        IF (LAST .GE. 2) WRITE (6,2) L1	
    2   FORMAT (I0,", ",$)	
        LNL1 = L0	
        LN = L1		
        DO N = 3,LAST	
          LNL2 = LNL1		
          LNL1 = LN		
          LN = LNL1 + LNL2 + EMBOLISM	
          WRITE (6,2) LN	
        END DO		
        WRITE (6,*)	
      END SUBROUTINE LEONARDO	

      PROGRAM POKE

      CALL LEONARDO(25,1,1,1)	
      CALL LEONARDO(25,0,1,0)	
      END
Task: Leonardo numbers


C++: #include <iostream>

void leoN( int cnt, int l0 = 1, int l1 = 1, int add = 1 ) {
    int t;
    for( int i = 0; i < cnt; i++ ) {
        std::cout << l0 << " ";
        t = l0 + l1 + add; l0 = l1; l1 = t;
    }
}
int main( int argc, char* argv[] ) {
    std::cout << "Leonardo Numbers: "; leoN( 25 );
    std::cout << "\n\nFibonacci Numbers: "; leoN( 25, 0, 1, 0 );
    return 0;
}









Fortran:       SUBROUTINE LEONARDO(LAST,L0,L1,AF)	
       INTEGER LAST	
       INTEGER L0,L1	
       INTEGER AF	
       OPTIONAL AF	
       INTEGER EMBOLISM	
       INTEGER N,LN,LNL1,LNL2	
        IF (PRESENT(AF)) THEN	
          EMBOLISM = AF			
         ELSE			
          EMBOLISM = 1			
        END IF			
        WRITE (6,1) LAST,L0,L1,EMBOLISM	
    1   FORMAT ("The first ",I0,	
     1   " numbers in the Leonardo sequence defined by L(0) = ",I0,
     2   " and L(1) = ",I0," with L(n) = L(n - 1) + L(n - 2) + ",I0)
        IF (LAST .GE. 1) WRITE (6,2) L0	
        IF (LAST .GE. 2) WRITE (6,2) L1	
    2   FORMAT (I0,", ",$)	
        LNL1 = L0	
        LN = L1		
        DO N = 3,LAST	
          LNL2 = LNL1		
          LNL1 = LN		
          LN = LNL1 + LNL2 + EMBOLISM	
          WRITE (6,2) LN	
        END DO		
        WRITE (6,*)	
      END SUBROUTINE LEONARDO	

      PROGRAM POKE

      CALL LEONARDO(25,1,1,1)	
      CALL LEONARDO(25,0,1,0)	
      END
Task: Leonardo numbers

C: #include<stdio.h>

void leonardo(int a,int b,int step,int num){
	
	int i,temp;
	
	printf("First 25 Leonardo numbers : \n");
	
	for(i=1;i<=num;i++){
		if(i==1)
			printf(" %d",a);
		else if(i==2)
			printf(" %d",b);
		else{
			printf(" %d",a+b+step);
			temp = a;
			a = b;
			b = temp+b+step;
		}
	}
}

int main()
{
	int a,b,step;
	
	printf("Enter first two Leonardo numbers and increment step : ");
	
	scanf("%d%d%d",&a,&b,&step);
	
	leonardo(a,b,step,25);
	
	return 0;
}










Fortran:       SUBROUTINE LEONARDO(LAST,L0,L1,AF)	
       INTEGER LAST	
       INTEGER L0,L1	
       INTEGER AF	
       OPTIONAL AF	
       INTEGER EMBOLISM	
       INTEGER N,LN,LNL1,LNL2	
        IF (PRESENT(AF)) THEN	
          EMBOLISM = AF			
         ELSE			
          EMBOLISM = 1			
        END IF			
        WRITE (6,1) LAST,L0,L1,EMBOLISM	
    1   FORMAT ("The first ",I0,	
     1   " numbers in the Leonardo sequence defined by L(0) = ",I0,
     2   " and L(1) = ",I0," with L(n) = L(n - 1) + L(n - 2) + ",I0)
        IF (LAST .GE. 1) WRITE (6,2) L0	
        IF (LAST .GE. 2) WRITE (6,2) L1	
    2   FORMAT (I0,", ",$)	
        LNL1 = L0	
        LN = L1		
        DO N = 3,LAST	
          LNL2 = LNL1		
          LNL1 = LN		
          LN = LNL1 + LNL2 + EMBOLISM	
          WRITE (6,2) LN	
        END DO		
        WRITE (6,*)	
      END SUBROUTINE LEONARDO	

      PROGRAM POKE

      CALL LEONARDO(25,1,1,1)	
      CALL LEONARDO(25,0,1,0)	
      END
Task: Leonardo numbers





Java: import java.util.Arrays;
import java.util.List;

@SuppressWarnings("SameParameterValue")
public class LeonardoNumbers {
    private static List<Integer> leonardo(int n) {
        return leonardo(n, 1, 1, 1);
    }

    private static List<Integer> leonardo(int n, int l0, int l1, int add) {
        Integer[] leo = new Integer[n];
        leo[0] = l0;
        leo[1] = l1;
        for (int i = 2; i < n; i++) {
            leo[i] = leo[i - 1] + leo[i - 2] + add;
        }
        return Arrays.asList(leo);
    }

    public static void main(String[] args) {
        System.out.println("The first 25 Leonardo numbers with L[0] = 1, L[1] = 1 and add number = 1 are:");
        System.out.println(leonardo(25));
        System.out.println("\nThe first 25 Leonardo numbers with L[0] = 0, L[1] = 1 and add number = 0 are:");
        System.out.println(leonardo(25, 0, 1, 0));
    }
}






Fortran:       SUBROUTINE LEONARDO(LAST,L0,L1,AF)	
       INTEGER LAST	
       INTEGER L0,L1	
       INTEGER AF	
       OPTIONAL AF	
       INTEGER EMBOLISM	
       INTEGER N,LN,LNL1,LNL2	
        IF (PRESENT(AF)) THEN	
          EMBOLISM = AF			
         ELSE			
          EMBOLISM = 1			
        END IF			
        WRITE (6,1) LAST,L0,L1,EMBOLISM	
    1   FORMAT ("The first ",I0,	
     1   " numbers in the Leonardo sequence defined by L(0) = ",I0,
     2   " and L(1) = ",I0," with L(n) = L(n - 1) + L(n - 2) + ",I0)
        IF (LAST .GE. 1) WRITE (6,2) L0	
        IF (LAST .GE. 2) WRITE (6,2) L1	
    2   FORMAT (I0,", ",$)	
        LNL1 = L0	
        LN = L1		
        DO N = 3,LAST	
          LNL2 = LNL1		
          LNL1 = LN		
          LN = LNL1 + LNL2 + EMBOLISM	
          WRITE (6,2) LN	
        END DO		
        WRITE (6,*)	
      END SUBROUTINE LEONARDO	

      PROGRAM POKE

      CALL LEONARDO(25,1,1,1)	
      CALL LEONARDO(25,0,1,0)	
      END
Task: Leonardo numbers







Python: def Leonardo(L_Zero, L_One, Add, Amount):
    terms = [L_Zero,L_One]
    while len(terms) < Amount:
        new = terms[-1] + terms[-2]
        new += Add
        terms.append(new)
    return terms

out = ""
print "First 25 Leonardo numbers:"
for term in Leonardo(1,1,1,25):
    out += str(term) + " "
print out

out = ""
print "Leonardo numbers with fibonacci parameters:"
for term in Leonardo(0,1,0,25):
    out += str(term) + " "
print out




Fortran:       SUBROUTINE LEONARDO(LAST,L0,L1,AF)	
       INTEGER LAST	
       INTEGER L0,L1	
       INTEGER AF	
       OPTIONAL AF	
       INTEGER EMBOLISM	
       INTEGER N,LN,LNL1,LNL2	
        IF (PRESENT(AF)) THEN	
          EMBOLISM = AF			
         ELSE			
          EMBOLISM = 1			
        END IF			
        WRITE (6,1) LAST,L0,L1,EMBOLISM	
    1   FORMAT ("The first ",I0,	
     1   " numbers in the Leonardo sequence defined by L(0) = ",I0,
     2   " and L(1) = ",I0," with L(n) = L(n - 1) + L(n - 2) + ",I0)
        IF (LAST .GE. 1) WRITE (6,2) L0	
        IF (LAST .GE. 2) WRITE (6,2) L1	
    2   FORMAT (I0,", ",$)	
        LNL1 = L0	
        LN = L1		
        DO N = 3,LAST	
          LNL2 = LNL1		
          LNL1 = LN		
          LN = LNL1 + LNL2 + EMBOLISM	
          WRITE (6,2) LN	
        END DO		
        WRITE (6,*)	
      END SUBROUTINE LEONARDO	

      PROGRAM POKE

      CALL LEONARDO(25,1,1,1)	
      CALL LEONARDO(25,0,1,0)	
      END
Task: Leonardo numbers








VB: Option Explicit

Private Sub LeonardoNumbers()
Dim L, MyString As String
    Debug.Print "First 25 Leonardo numbers :"
    L = Leo_Numbers(25, 1, 1, 1)
    MyString = Join(L, "; ")
    Debug.Print MyString
    Debug.Print "First 25 Leonardo numbers from 0, 1 with add number = 0"
    L = Leo_Numbers(25, 0, 1, 0)
    MyString = Join(L, "; ")
    Debug.Print MyString
    Debug.Print "If the first prarameter is too small :"
    L = Leo_Numbers(1, 0, 1, 0)
    MyString = Join(L, "; ")
    Debug.Print MyString
End Sub

Public Function Leo_Numbers(HowMany As Long, L_0 As Long, L_1 As Long, Add_Nb As Long)
Dim N As Long, Ltemp

    If HowMany > 1 Then
        ReDim Ltemp(HowMany - 1)
        Ltemp(0) = L_0: Ltemp(1) = L_1
        For N = 2 To HowMany - 1
             Ltemp(N) = Ltemp(N - 1) + Ltemp(N - 2) + Add_Nb
        Next N
    Else
        ReDim Ltemp(0)
        Ltemp(0) = "The first parameter is too small"
    End If
    Leo_Numbers = Ltemp
End Function



Fortran:       SUBROUTINE LEONARDO(LAST,L0,L1,AF)	
       INTEGER LAST	
       INTEGER L0,L1	
       INTEGER AF	
       OPTIONAL AF	
       INTEGER EMBOLISM	
       INTEGER N,LN,LNL1,LNL2	
        IF (PRESENT(AF)) THEN	
          EMBOLISM = AF			
         ELSE			
          EMBOLISM = 1			
        END IF			
        WRITE (6,1) LAST,L0,L1,EMBOLISM	
    1   FORMAT ("The first ",I0,	
     1   " numbers in the Leonardo sequence defined by L(0) = ",I0,
     2   " and L(1) = ",I0," with L(n) = L(n - 1) + L(n - 2) + ",I0)
        IF (LAST .GE. 1) WRITE (6,2) L0	
        IF (LAST .GE. 2) WRITE (6,2) L1	
    2   FORMAT (I0,", ",$)	
        LNL1 = L0	
        LN = L1		
        DO N = 3,LAST	
          LNL2 = LNL1		
          LNL1 = LN		
          LN = LNL1 + LNL2 + EMBOLISM	
          WRITE (6,2) LN	
        END DO		
        WRITE (6,*)	
      END SUBROUTINE LEONARDO	

      PROGRAM POKE

      CALL LEONARDO(25,1,1,1)	
      CALL LEONARDO(25,0,1,0)	
      END
Task: Variadic function



C#: using System;

class Program {
    static void Main(string[] args) {
        PrintAll("test", "rosetta code", 123, 5.6);
    }

    static void PrintAll(params object[] varargs) {
        foreach (var i in varargs) {
            Console.WriteLine(i);
        }
    }
}









Ada: with Ada.Strings.Unbounded, Ada.Text_IO;

procedure Variadic is

   subtype U_String is Ada.Strings.Unbounded.Unbounded_String;
   use type U_String;

   function "+"(S: String) return U_String
     renames Ada.Strings.Unbounded.To_Unbounded_String;

   function "-"(U: U_String) return String
     renames Ada.Strings.Unbounded.To_String;

   type Variadic_Array is array(Positive range <>) of U_String;

   procedure Print_Line(Params: Variadic_Array) is
   begin
      for I in Params'Range loop
         Ada.Text_IO.Put(-Params(I));
         if I < Params'Last then
            Ada.Text_IO.Put(" ");
         end if;
      end loop;
      Ada.Text_IO.New_Line;
   end Print_Line;

begin
   Print_Line((+"Mary", +"had", +"a", +"little", +"lamb.")); 
   Print_Line((1 => +"Rosetta Code is cooool!")); 
end;
Task: Variadic function

C: #include <stdio.h>
#include <stdarg.h>

void varstrings(int count, ...)   
{
    va_list args;
    va_start(args, count);
    while (count--)
        puts(va_arg(args, const char *));
    va_end(args);
}

varstrings(5, "Mary", "had", "a", "little", "lamb");











Ada: with Ada.Strings.Unbounded, Ada.Text_IO;

procedure Variadic is

   subtype U_String is Ada.Strings.Unbounded.Unbounded_String;
   use type U_String;

   function "+"(S: String) return U_String
     renames Ada.Strings.Unbounded.To_Unbounded_String;

   function "-"(U: U_String) return String
     renames Ada.Strings.Unbounded.To_String;

   type Variadic_Array is array(Positive range <>) of U_String;

   procedure Print_Line(Params: Variadic_Array) is
   begin
      for I in Params'Range loop
         Ada.Text_IO.Put(-Params(I));
         if I < Params'Last then
            Ada.Text_IO.Put(" ");
         end if;
      end loop;
      Ada.Text_IO.New_Line;
   end Print_Line;

begin
   Print_Line((+"Mary", +"had", +"a", +"little", +"lamb.")); 
   Print_Line((1 => +"Rosetta Code is cooool!")); 
end;
Task: Variadic function


C++: #include <iostream>

template<typename T>
 void print(T const& t)
{
  std::cout << t;
}

template<typename First, typename ... Rest>
 void print(First const& first, Rest const& ... rest)
{
  std::cout << first;
  print(rest ...);
}

int main()
{
  int i = 10;
  std::string s = "Hello world";
  print("i = ", i, " and s = \"", s, "\"\n");
}










Ada: with Ada.Strings.Unbounded, Ada.Text_IO;

procedure Variadic is

   subtype U_String is Ada.Strings.Unbounded.Unbounded_String;
   use type U_String;

   function "+"(S: String) return U_String
     renames Ada.Strings.Unbounded.To_Unbounded_String;

   function "-"(U: U_String) return String
     renames Ada.Strings.Unbounded.To_String;

   type Variadic_Array is array(Positive range <>) of U_String;

   procedure Print_Line(Params: Variadic_Array) is
   begin
      for I in Params'Range loop
         Ada.Text_IO.Put(-Params(I));
         if I < Params'Last then
            Ada.Text_IO.Put(" ");
         end if;
      end loop;
      Ada.Text_IO.New_Line;
   end Print_Line;

begin
   Print_Line((+"Mary", +"had", +"a", +"little", +"lamb.")); 
   Print_Line((1 => +"Rosetta Code is cooool!")); 
end;
Task: Variadic function




Go: func printAll(things ... string) {
  
  for _, x := range things {
    fmt.Println(x)
  }
}








Ada: with Ada.Strings.Unbounded, Ada.Text_IO;

procedure Variadic is

   subtype U_String is Ada.Strings.Unbounded.Unbounded_String;
   use type U_String;

   function "+"(S: String) return U_String
     renames Ada.Strings.Unbounded.To_Unbounded_String;

   function "-"(U: U_String) return String
     renames Ada.Strings.Unbounded.To_String;

   type Variadic_Array is array(Positive range <>) of U_String;

   procedure Print_Line(Params: Variadic_Array) is
   begin
      for I in Params'Range loop
         Ada.Text_IO.Put(-Params(I));
         if I < Params'Last then
            Ada.Text_IO.Put(" ");
         end if;
      end loop;
      Ada.Text_IO.New_Line;
   end Print_Line;

begin
   Print_Line((+"Mary", +"had", +"a", +"little", +"lamb.")); 
   Print_Line((1 => +"Rosetta Code is cooool!")); 
end;
Task: Variadic function





Java: module VariadicFunction
    {
    void show(String[] strings)
        {
        @Inject Console console;
        strings.forEach(s -> console.print(s));
        }

    void run()
        {
        show(["hello", "world"]);

        String s1 = "not";
        String s2 = "a";
        String s3 = "constant";
        String s4 = "literal";
        show([s1, s2, s3, s4]);
        }
    }







Ada: with Ada.Strings.Unbounded, Ada.Text_IO;

procedure Variadic is

   subtype U_String is Ada.Strings.Unbounded.Unbounded_String;
   use type U_String;

   function "+"(S: String) return U_String
     renames Ada.Strings.Unbounded.To_Unbounded_String;

   function "-"(U: U_String) return String
     renames Ada.Strings.Unbounded.To_String;

   type Variadic_Array is array(Positive range <>) of U_String;

   procedure Print_Line(Params: Variadic_Array) is
   begin
      for I in Params'Range loop
         Ada.Text_IO.Put(-Params(I));
         if I < Params'Last then
            Ada.Text_IO.Put(" ");
         end if;
      end loop;
      Ada.Text_IO.New_Line;
   end Print_Line;

begin
   Print_Line((+"Mary", +"had", +"a", +"little", +"lamb.")); 
   Print_Line((1 => +"Rosetta Code is cooool!")); 
end;
Task: Variadic function







Python: def print_all(*things):
    for x in things:
        print x





Ada: with Ada.Strings.Unbounded, Ada.Text_IO;

procedure Variadic is

   subtype U_String is Ada.Strings.Unbounded.Unbounded_String;
   use type U_String;

   function "+"(S: String) return U_String
     renames Ada.Strings.Unbounded.To_Unbounded_String;

   function "-"(U: U_String) return String
     renames Ada.Strings.Unbounded.To_String;

   type Variadic_Array is array(Positive range <>) of U_String;

   procedure Print_Line(Params: Variadic_Array) is
   begin
      for I in Params'Range loop
         Ada.Text_IO.Put(-Params(I));
         if I < Params'Last then
            Ada.Text_IO.Put(" ");
         end if;
      end loop;
      Ada.Text_IO.New_Line;
   end Print_Line;

begin
   Print_Line((+"Mary", +"had", +"a", +"little", +"lamb.")); 
   Print_Line((1 => +"Rosetta Code is cooool!")); 
end;
Task: Variadic function








VB: Sub PrintArgs(ParamArray Args() As String)
  For i As Integer = 0 To Ubound(Args)
    Print(Args(i))
  Next
End Sub




Ada: with Ada.Strings.Unbounded, Ada.Text_IO;

procedure Variadic is

   subtype U_String is Ada.Strings.Unbounded.Unbounded_String;
   use type U_String;

   function "+"(S: String) return U_String
     renames Ada.Strings.Unbounded.To_Unbounded_String;

   function "-"(U: U_String) return String
     renames Ada.Strings.Unbounded.To_String;

   type Variadic_Array is array(Positive range <>) of U_String;

   procedure Print_Line(Params: Variadic_Array) is
   begin
      for I in Params'Range loop
         Ada.Text_IO.Put(-Params(I));
         if I < Params'Last then
            Ada.Text_IO.Put(" ");
         end if;
      end loop;
      Ada.Text_IO.New_Line;
   end Print_Line;

begin
   Print_Line((+"Mary", +"had", +"a", +"little", +"lamb.")); 
   Print_Line((1 => +"Rosetta Code is cooool!")); 
end;
Task: Variadic function

C: #include <stdio.h>
#include <stdarg.h>

void varstrings(int count, ...)   
{
    va_list args;
    va_start(args, count);
    while (count--)
        puts(va_arg(args, const char *));
    va_end(args);
}

varstrings(5, "Mary", "had", "a", "little", "lamb");
Task: Variadic function



C#: using System;

class Program {
    static void Main(string[] args) {
        PrintAll("test", "rosetta code", 123, 5.6);
    }

    static void PrintAll(params object[] varargs) {
        foreach (var i in varargs) {
            Console.WriteLine(i);
        }
    }
}
Task: Variadic function


C++: #include <iostream>

template<typename T>
 void print(T const& t)
{
  std::cout << t;
}

template<typename First, typename ... Rest>
 void print(First const& first, Rest const& ... rest)
{
  std::cout << first;
  print(rest ...);
}

int main()
{
  int i = 10;
  std::string s = "Hello world";
  print("i = ", i, " and s = \"", s, "\"\n");
}
Task: Variadic function





Java: module VariadicFunction
    {
    void show(String[] strings)
        {
        @Inject Console console;
        strings.forEach(s -> console.print(s));
        }

    void run()
        {
        show(["hello", "world"]);

        String s1 = "not";
        String s2 = "a";
        String s3 = "constant";
        String s4 = "literal";
        show([s1, s2, s3, s4]);
        }
    }
Task: Variadic function







Python: def print_all(*things):
    for x in things:
        print x
Task: Variadic function








VB: Sub PrintArgs(ParamArray Args() As String)
  For i As Integer = 0 To Ubound(Args)
    Print(Args(i))
  Next
End Sub
Task: Variadic function




Go: func printAll(things ... string) {
  
  for _, x := range things {
    fmt.Println(x)
  }
}
Task: Variadic function



C#: using System;

class Program {
    static void Main(string[] args) {
        PrintAll("test", "rosetta code", 123, 5.6);
    }

    static void PrintAll(params object[] varargs) {
        foreach (var i in varargs) {
            Console.WriteLine(i);
        }
    }
}








Fortran: program varargs

  integer, dimension(:), allocatable :: va
  integer :: i

  
  call v_func()
  call v_func( (/ 100 /) )
  call v_func( (/ 90, 20, 30 /) )

  
  allocate(va(5))
  va = (/ (i,i=1,5) /)
  call v_func(va)
  deallocate(va)

contains

  subroutine v_func(arglist)
    integer, dimension(:), intent(in), optional :: arglist

    integer :: i

    if ( present(arglist) ) then
       do i = lbound(arglist, 1), ubound(arglist, 1)
          print *, arglist(i)
       end do
    else
       print *, "no argument at all"
    end if
  end subroutine v_func

end program varargs
Task: Variadic function


C++: #include <iostream>

template<typename T>
 void print(T const& t)
{
  std::cout << t;
}

template<typename First, typename ... Rest>
 void print(First const& first, Rest const& ... rest)
{
  std::cout << first;
  print(rest ...);
}

int main()
{
  int i = 10;
  std::string s = "Hello world";
  print("i = ", i, " and s = \"", s, "\"\n");
}









Fortran: program varargs

  integer, dimension(:), allocatable :: va
  integer :: i

  
  call v_func()
  call v_func( (/ 100 /) )
  call v_func( (/ 90, 20, 30 /) )

  
  allocate(va(5))
  va = (/ (i,i=1,5) /)
  call v_func(va)
  deallocate(va)

contains

  subroutine v_func(arglist)
    integer, dimension(:), intent(in), optional :: arglist

    integer :: i

    if ( present(arglist) ) then
       do i = lbound(arglist, 1), ubound(arglist, 1)
          print *, arglist(i)
       end do
    else
       print *, "no argument at all"
    end if
  end subroutine v_func

end program varargs
Task: Variadic function

C: #include <stdio.h>
#include <stdarg.h>

void varstrings(int count, ...)   
{
    va_list args;
    va_start(args, count);
    while (count--)
        puts(va_arg(args, const char *));
    va_end(args);
}

varstrings(5, "Mary", "had", "a", "little", "lamb");










Fortran: program varargs

  integer, dimension(:), allocatable :: va
  integer :: i

  
  call v_func()
  call v_func( (/ 100 /) )
  call v_func( (/ 90, 20, 30 /) )

  
  allocate(va(5))
  va = (/ (i,i=1,5) /)
  call v_func(va)
  deallocate(va)

contains

  subroutine v_func(arglist)
    integer, dimension(:), intent(in), optional :: arglist

    integer :: i

    if ( present(arglist) ) then
       do i = lbound(arglist, 1), ubound(arglist, 1)
          print *, arglist(i)
       end do
    else
       print *, "no argument at all"
    end if
  end subroutine v_func

end program varargs