Challenge - 5 Problems
Function Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this R function call?
Consider the following R code that defines a function and then calls it. What will be printed?
R Programming
greet <- function(name) {
message <- paste("Hello," , name)
return(message)
}
result <- greet("Alice")
print(result)Attempts:
2 left
💡 Hint
Look at how the paste function combines strings with a comma and a space.
✗ Incorrect
The paste function combines "Hello," and "Alice" with a space by default, so the output is "Hello, Alice". The function returns this string, which is then printed.
🧠 Conceptual
intermediate1:30remaining
Why use functions to organize code?
Which of the following is the main reason to use functions in R programming?
Attempts:
2 left
💡 Hint
Think about how functions help avoid repeating yourself.
✗ Incorrect
Functions let you write a block of code once and use it many times, which saves effort and reduces mistakes.
🔧 Debug
advanced2:00remaining
Identify the error in this function
What error will this R code produce when run?
R Programming
add_numbers <- function(a, b) {
sum <- a + b
print(sum)
}
result <- add_numbers(3)Attempts:
2 left
💡 Hint
Check how many arguments the function expects and how many are given.
✗ Incorrect
The function add_numbers requires two arguments, but only one is provided, causing an error about a missing argument.
📝 Syntax
advanced1:30remaining
Which option correctly defines a function in R?
Choose the correct syntax to define a function named 'square' that returns the square of a number x.
Attempts:
2 left
💡 Hint
Remember how functions are assigned in R using the <- operator.
✗ Incorrect
In R, functions are assigned to names using <- and defined with function(). Option B follows this syntax correctly.
🚀 Application
expert2:00remaining
How many times is the message printed?
What will be the output count of the following R code?
R Programming
print_message <- function() {
print("Hello")
}
for(i in 1:3) {
print_message()
}
print_message()Attempts:
2 left
💡 Hint
Count how many times the function is called inside and outside the loop.
✗ Incorrect
The function print_message is called 3 times inside the loop and once after the loop, so it prints "Hello" 4 times.