Default argument values in R Programming - Time & Space Complexity
We want to see how using default values for function arguments affects how long the code takes to run.
Does having default values change the work done when the function runs?
Analyze the time complexity of the following code snippet.
my_sum <- function(x, y = 10) {
total <- 0
for (i in 1:x) {
total <- total + i + y
}
return(total)
}
result <- my_sum(5)
This function adds numbers from 1 to x, adding y each time. If y is not given, it uses 10.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that runs from 1 to x.
- How many times: Exactly x times, once for each number from 1 to x.
Each time we increase x, the loop runs more times, doing more additions.
| Input Size (x) | Approx. Operations |
|---|---|
| 10 | About 10 additions |
| 100 | About 100 additions |
| 1000 | About 1000 additions |
Pattern observation: The work grows directly with x. Double x, double the work.
Time Complexity: O(n)
This means the time to run grows in a straight line with the input size x.
[X] Wrong: "Using a default value makes the function faster or slower depending on the value."
[OK] Correct: The default value is just a number used inside the loop. It does not change how many times the loop runs, so it does not affect the time complexity.
Understanding how default arguments affect performance helps you explain your code clearly and shows you know what really matters for speed.
"What if the default argument was a function call that runs a loop itself? How would the time complexity change?"
Practice
Solution
Step 1: Understand default arguments in R
Default arguments provide a preset value if no value is given when calling the function.Step 2: Behavior when argument is missing
If the argument is not provided, R uses the default value automatically without error.Final Answer:
The function uses the default value specified in the function definition. -> Option CQuick Check:
Missing argument uses default value [OK]
- Thinking missing argument causes error
- Assuming function asks for input
- Believing function ignores missing argument silently
x set to 10?Solution
Step 1: Recall R function default argument syntax
In R, default values are assigned with=inside the function definition.Step 2: Check each option
Only myfunc <- function(x = 10) { return(x) } usesx = 10correctly. Others use invalid syntax like:,<-, or==.Final Answer:
myfunc <- function(x = 10) { return(x) } -> Option BQuick Check:
Default argument uses = in function definition [OK]
- Using <- or : instead of = for default values
- Using == which is a comparison, not assignment
- Placing default outside function parentheses
add <- function(a, b = 5) {
return(a + b)
}
add(3)Solution
Step 1: Identify default argument usage
The functionaddhasbdefaulting to 5 if not provided.Step 2: Call function with only
Sincea=3bis missing, it uses default 5, so result is 3 + 5 = 8.Final Answer:
8 -> Option AQuick Check:
3 + default 5 = 8 [OK]
- Expecting error for missing argument
- Ignoring default and using only provided argument
- Confusing output with just the first argument
multiply <- function(x, y = 2) {
x * y
}
multiply(4, )What is the problem with this code?
Solution
Step 1: Analyze function call syntax
The callmultiply(4, )has a comma but no second argument value, which is invalid syntax in R.Step 2: Understand R argument rules
Trailing commas without values cause syntax errors; you must omit the comma if skipping optional arguments.Final Answer:
Syntax error due to trailing comma without value. -> Option DQuick Check:
Trailing comma without value causes syntax error [OK]
- Assuming default used despite syntax error
- Thinking function returns result ignoring syntax
- Confusing missing argument with missing value
greet that prints a greeting message. It should take a name argument with default "Guest" and a punctuation argument with default "!". Which function definition correctly implements this?Solution
Step 1: Check default argument syntax
Defaults must be assigned inside the function definition using=and quoted strings for text.Step 2: Validate each option
greet <- function(name = "Guest", punctuation = "!") { paste0("Hello, ", name, punctuation) } correctly sets defaults with quotes and uses them in paste0. Other options misuse default assignment by placing it inside the function body, omitting defaults for optional arguments, or lacking quotes around strings (e.g.,name = Guest,punctuation = !).Final Answer:
greet <- function(name = "Guest", punctuation = "!") { paste0("Hello, ", name, punctuation) } -> Option AQuick Check:
Default arguments set with = and quoted strings [OK]
- Assigning defaults inside function body
- Forgetting quotes around string defaults
- Omitting default for all optional arguments
Ready to prove it?
Pass the test and this pattern moves unknown
