Complete the code to define a function named greet that prints "Hello!".
greet <- function() {
print([1])
}The function prints the string "Hello!" using print(). The string must be in quotes.
Complete the code to define a function add that takes two numbers x and y and returns their sum.
add <- function(x, y) {
return([1])
}The function should return the sum of x and y, which is x + y.
Fix the error in the function definition to correctly calculate the square of a number n.
square <- function(n) {
result <- n [1] n
return(result)
}To calculate the square, multiply n by itself using the * operator.
Fill both blanks to define a function named is_even that returns TRUE if a number num is even, otherwise FALSE.
is_even <- function(num) {
if (num [1] 2 == 0) {
return([2])
} else {
return(FALSE)
}
}The modulo operator %% checks remainder. If remainder is 0, number is even, so return TRUE.
Fill all three blanks to define a function named greet_person that takes a name and returns a greeting string like "Hello, Alice!".
greet_person <- function([1]) { greeting <- paste([2], [3], sep = ", ") return(greeting) }
The function takes a parameter name, then uses paste() to join "Hello" and the name with a comma and space.