A function argument is a value you give to a function when you call it. It tells the function what data to work with.
Click to reveal answer
beginner
How do you define a function with arguments in R?
You write the function name, then inside parentheses list the argument names. For example: myfun <- function(x, y) { x + y } defines a function with two arguments, x and y.
Click to reveal answer
intermediate
What happens if you call a function without providing all its arguments?
If the function has default values for some arguments, those defaults are used. Otherwise, R will give an error saying an argument is missing.
Click to reveal answer
intermediate
What is a default argument value in R functions?
A default argument value is a value assigned to an argument in the function definition. If you don’t provide that argument when calling the function, R uses the default.
Click to reveal answer
beginner
How can you pass arguments to a function by name in R?
You can specify the argument name when calling the function, like myfun(y = 5, x = 3). This lets you give arguments in any order.
Click to reveal answer
What will happen if you call sum(1, 2) in R?
AIt returns 2
BIt returns 3
CIt returns 1
DIt returns an error
✗ Incorrect
The sum function adds the numbers 1 and 2 and returns 3.
How do you set a default value for an argument in an R function?
ABy assigning a value in the function definition, like <code>function(x = 5)</code>
BBy calling the function with no arguments
CBy using the <code>default()</code> function
DBy writing <code>setdefault(x)</code>
✗ Incorrect
You assign a default value directly in the function definition.
What is the correct way to call a function with named arguments in R?
A<code>myfun(x = 2, y = 3)</code>
B<code>myfun(2, 3)</code>
C<code>myfun(, x = 2, y = 3)</code>
D<code>myfun(x:2, y:3)</code>
✗ Incorrect
Named arguments use the syntax name = value inside the function call.
If a function argument has a default value, what happens when you omit it in the call?
AAn error occurs
BThe function ignores the argument
CThe argument becomes NULL
DThe default value is used
✗ Incorrect
R uses the default value if you don’t provide that argument.
Which of these is NOT a valid way to pass arguments to a function in R?
ABy position, like <code>fun(1, 2)</code>
BBy name, like <code>fun(y = 2, x = 1)</code>
CBy using <code>fun(x:1, y:2)</code>
DBy mixing named and positional arguments
✗ Incorrect
The syntax x:1 is not valid for passing arguments in R.
Explain how function arguments work in R and how default values affect function calls.
Think about what happens when you call a function with fewer arguments than defined.
You got /4 concepts.
Describe how to use named arguments in R function calls and why they might be helpful.
Consider how naming arguments can make your code easier to read and less error-prone.
You got /4 concepts.
Practice
(1/5)
1. What is the purpose of function arguments in R? my_function <- function(x, y) { x + y }
easy
A. To create variables outside the function
B. To print output from the function
C. To send information into the function for processing
D. To stop the function from running
Solution
Step 1: Understand what arguments do
Arguments are values you give to a function so it can use them inside its code.
Step 2: Look at the example function
The function takes x and y as inputs and adds them, showing arguments are inputs.
Final Answer:
To send information into the function for processing -> Option C
Quick Check:
Function arguments = inputs [OK]
Hint: Arguments are inputs you give to functions [OK]
Common Mistakes:
Thinking arguments print output
Confusing arguments with variables outside
Believing arguments stop function execution
2. Which of the following is the correct way to define a function with two arguments a and b in R?
easy
A. my_func <- function {a, b} return(a + b)
B. my_func <- function(a, b) { return(a + b) }
C. my_func <- function[a, b] { return(a + b) }
D. my_func <- function(a; b) { return(a + b) }
Solution
Step 1: Recall R function syntax
Functions use parentheses () to list arguments separated by commas.
Step 2: Check each option
Only my_func <- function(a, b) { return(a + b) } uses parentheses and commas correctly; others use wrong brackets or separators.
Final Answer:
my_func <- function(a, b) { return(a + b) } -> Option B
Quick Check:
Function args use (a, b) [OK]
Hint: Use parentheses and commas for arguments [OK]
Common Mistakes:
Using square brackets instead of parentheses
Separating arguments with semicolons
Omitting parentheses around arguments
3. What will be the output of this R code?
add <- function(x, y = 5) {
x + y
}
add(3)
medium
A. 8
B. 3
C. 5
D. Error: missing argument y
Solution
Step 1: Understand default argument values
The function add has y defaulting to 5 if not provided.
Step 2: Call the function with one argument
Calling add(3) uses x=3 and default y=5, so result is 3 + 5 = 8.
Final Answer:
8 -> Option A
Quick Check:
3 + default 5 = 8 [OK]
Hint: Default arguments fill in missing values [OK]
Common Mistakes:
Expecting error when argument is missing
Ignoring default argument values
Confusing which argument has default
4. Identify the error in this R function definition:
multiply <- function(x, y) {
result <- x * z
return(result)
}
medium
A. Function should not use return statement
B. Function name cannot be multiply
C. Arguments must have default values
D. Variable z is not defined inside the function
Solution
Step 1: Check variables used inside function
The function uses z but only x and y are arguments; z is undefined.
Step 2: Understand variable scope
Variables inside functions must be defined or passed as arguments; z is missing.
Final Answer:
Variable z is not defined inside the function -> Option D
Quick Check:
Undefined variable = error [OK]
Hint: Use only defined arguments or variables inside functions [OK]
Common Mistakes:
Using variables not passed as arguments
Thinking return is optional error
Believing arguments must have defaults
5. You want to create a function greet that takes a name and an optional greeting message (default "Hello"). Which function definition is correct to achieve this?
In R, arguments with defaults must come after those without defaults.
Step 2: Check each option
greet <- function(name, message = "Hello") { paste(message, name) } correctly places message with default after name without default and uses paste correctly.