0
0
R Programmingprogramming~10 mins

Default argument values 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 with a default argument value.

R Programming
greet <- function(name = [1]) {
  paste("Hello", name)
}
Drag options to blanks, or click blank then click option'
A"World"
BWorld
Cname
D"name"
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put quotes around the default string value.
Using a variable name without quotes as default.
2fill in blank
medium

Complete the code to call the function without providing the argument, so it uses the default value.

R Programming
greet([1])
Drag options to blanks, or click blank then click option'
A"Alice"
B(no argument)
Cname = "Alice"
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Passing an empty string instead of no argument.
Trying to pass the argument name without a value.
3fill in blank
hard

Fix the error in the function definition by completing the default argument value correctly.

R Programming
multiply <- function(x, y = [1]) {
  x * y
}
Drag options to blanks, or click blank then click option'
A2
B"2"
Cy
Dx
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes around numbers for default numeric arguments.
Using variable names as default values.
4fill in blank
hard

Fill both blanks to create a function with two default arguments and return their sum.

R Programming
add <- function(a = [1], b = [2]) {
  a + b
}
Drag options to blanks, or click blank then click option'
A1
B0
C2
D"0"
Attempts:
3 left
💡 Hint
Common Mistakes
Using strings instead of numbers for default values.
Using the same value for both defaults.
5fill in blank
hard

Fill all three blanks to define a function with default arguments and call it with one argument.

R Programming
power <- function(base = [1], exponent = [2]) {
  base ^ exponent
}

result <- power([3])
result
Drag options to blanks, or click blank then click option'
A2
B3
C5
Dbase = 5
Attempts:
3 left
💡 Hint
Common Mistakes
Passing argument with name when only value is needed.
Mixing up base and exponent values.