0
0
R Programmingprogramming~10 mins

Function definition in R Programming - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a function named greet that prints "Hello!".

R Programming
greet <- function() {
  print([1])
}
Drag options to blanks, or click blank then click option'
A"Hello!"
BHello
Cprint("Hello!")
Dgreet
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around the text.
Trying to print without using print() function.
2fill in blank
medium

Complete the code to define a function add that takes two numbers x and y and returns their sum.

R Programming
add <- function(x, y) {
  return([1])
}
Drag options to blanks, or click blank then click option'
Ax - y
Bx * y
Cx / y
Dx + y
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication instead of addition.
Forgetting to return the result.
3fill in blank
hard

Fix the error in the function definition to correctly calculate the square of a number n.

R Programming
square <- function(n) {
  result <- n [1] n
  return(result)
}
Drag options to blanks, or click blank then click option'
A*
B+
C-
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using + instead of * for multiplication.
Using division or subtraction operators.
4fill in blank
hard

Fill both blanks to define a function named is_even that returns TRUE if a number num is even, otherwise FALSE.

R Programming
is_even <- function(num) {
  if (num [1] 2 == 0) {
    return([2])
  } else {
    return(FALSE)
  }
}
Drag options to blanks, or click blank then click option'
A%%
BTRUE
C==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using == instead of %% for modulo.
Returning FALSE when number is even.
5fill in blank
hard

Fill all three blanks to define a function named greet_person that takes a name and returns a greeting string like "Hello, Alice!".

R Programming
greet_person <- function([1]) {
  greeting <- paste([2], [3], sep = ", ")
  return(greeting)
}
Drag options to blanks, or click blank then click option'
Aname
B"Hello"
D"!"
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to include the parameter name.
Not using paste() correctly to join strings.