เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

Variable number of arguments

Sometimes we do not know how many arguments we want to pass into a function. This is where it is handy to allow an unlimited number of arguments, whether we want to pass 1 or 100.

This is possible using the ellipsis operator ....

In this exercise, write a function stock_to_buy that takes a variable number of arguments and then calls the function passing in four different arguments.

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

Intermediate Julia

ดูคอร์ส

คำแนะนำการฝึกหัด

  • Write the function stock_to_buy and supply the parameter stocks. Print each argument within the function.

  • Call stock_to_buy and pass four values: "AAPL", "MSFT", "GOOG", "TSLA".

แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ

ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์

# Define stock_to_buy, accepting varargs
function stock_to_buy(____)
	println(____)
end

# Call stock_to_buy with four arguments
stock_to_buy(____, ____, ____, ____)
แก้ไขและรันโค้ด