언어별 가변길이 매개변수

variable length arguments
variable-length argument lists

1 Bash[ | ]

foo() {
  for p in "$@"; do
    echo "[$p]"
  done
}
foo a b "hello world"
# [a]
# [b]
# [hello world]

2 Java[ | ]

3 JavaScript[ | ]

function test() {
	console.log(arguments);
}

test();
test('hello');
test('hello', 'world');
// []
// ["hello"]
// ["hello", "world"]

4 PHP[ | ]

PHP 5.6+
function sum(...$numbers) {
    $acc = 0;
    foreach ($numbers as $n) $acc += $n;
    return $acc;
}
echo sum(1, 2, 3, 4);
# 10

5 Python[ | ]

def sum( *args ):
    s = 0
    for arg in args:
        s += arg
    return s

print( sum( 1, 2, 3, 4 ) )
# 10

6 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}