0
0
R Programmingprogramming~10 mins

Why functions organize code in R Programming - Test Your Understanding

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 a message.

R Programming
greet <- function() {
  print([1])
}
Drag options to blanks, or click blank then click option'
AHello, friend!
B"Hello, friend!"
Cprint("Hello, friend!")
Dmessage("Hello, friend!")
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around the message
Putting print inside the string
2fill in blank
medium

Complete the code to call the greet function.

R Programming
[1]()
Drag options to blanks, or click blank then click option'
Agreet
Bprint
Cmessage
Dsay
Attempts:
3 left
💡 Hint
Common Mistakes
Using print instead of the function name
Forgetting parentheses
3fill in blank
hard

Fix the error in the function definition to accept a name parameter.

R Programming
greet <- function([1]) {
  print(paste("Hello," , name))
}
Drag options to blanks, or click blank then click option'
Atext
Bmessage
Cinput
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different parameter name than inside the function
Forgetting to add a parameter
4fill in blank
hard

Fill both blanks to create a function that adds two numbers and returns the result.

R Programming
add_numbers <- function(a, b) {
  result <- a [1] b
  return([2])
}
Drag options to blanks, or click blank then click option'
A+
Bresult
C-
Da
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction instead of addition
Returning the wrong variable
5fill in blank
hard

Fill all three blanks to create a function that checks if a number is positive, negative, or zero.

R Programming
check_number <- function(num) {
  if (num [1] 0) {
    return("Positive")
  } else if (num [2] 0) {
    return("Negative")
  } else {
    return([3])
  }
}
Drag options to blanks, or click blank then click option'
A>
B<
C"Zero"
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong comparison operators
Returning a variable instead of a string for zero