自己寫一個函式(2)
有些情況下,函式不需要輸入參數。假設你想寫一個函式,回傳擲一顆公平骰子的隨機結果:
throw_die <- function() {
number <- sample(1:6, size = 1)
number
}
throw_die()
接下來就交給你,撰寫一個不接受任何參數的函式吧!
本練習屬於課程
R 中級
練習說明
- 定義一個函式
hello()。它會印出「Hi there!」,並回傳TRUE。它不需要任何參數。 - 呼叫
hello()函式,當然不需指定參數。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Define the function hello()
# Call the function hello()