home > 比較編 > Perl, PHP, Python, Ruby > functions >

ForNext

Only Do What Only You Can Do

142. variable number of arguments

VBScript

JScript

Perl

sub foo {
  if ( @_ >= 1 ) {
    print "first: $_[0]\n";
  }
  if ( @_ >= 2 ) {
    print "last: $_[-1]\n";
  }
}




PHP

function foo() {
  $arg_cnt = func_num_args();
  if ($arg_cnt >= 1) {
    $n = func_get_arg(0);
    echo "first: " . $n . "\n";
  }
  if ($arg_cnt >= 2) {
    $a = func_get_args();
    $n = $a[$arg_cnt-1];
    echo "last: " . $n . "\n";
  }
}

Python

def foo(*a):
  if len(a) >= 1:
    print('first: ' + str(a[0]))
  if len(a) >= 2:
    print('last: ' + str(a[-1]))







Ruby

def foo(*a)
  if a.size >= 1
    puts "first: #{a[0]}"
  end
  if a.size >= 2
    puts "last: #{a[-1]}"
  end
end




PowerShell

Scala

F#

C

C++

C++Builder

VC++

C#

Java

Objective-C

D

VB

VB.NET

Delphi

Ada

PL/SQL

T-SQL

関数型

inserted by FC2 system