Function arguments in R Programming - Time & Space Complexity
When we use functions with arguments, it is important to see how the time to run the function changes as the input changes.
We want to know: how does the work inside the function grow when the input size grows?
Analyze the time complexity of the following code snippet.
my_sum <- function(x) {
total <- 0
for (i in seq_along(x)) {
total <- total + x[i]
}
return(total)
}
values <- 1:100
my_sum(values)
This function adds up all numbers in a vector given as an argument.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that goes through each element of the input vector.
- How many times: Once for each item in the input vector.
As the input vector gets bigger, the function does more additions, one for each item.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The work grows directly with the size of the input vector.
Time Complexity: O(n)
This means the time to finish grows in a straight line as the input vector gets bigger.
[X] Wrong: "The function runs in the same time no matter how big the input is."
[OK] Correct: Because the function adds each number one by one, more numbers mean more work and more time.
Understanding how function arguments affect time helps you explain how your code handles different input sizes clearly and confidently.
"What if the function used two loops, each going through the input vector? How would the time complexity change?"
Practice
my_function <- function(x, y) { x + y }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 takesxandyas inputs and adds them, showing arguments are inputs.Final Answer:
To send information into the function for processing -> Option CQuick Check:
Function arguments = inputs [OK]
- Thinking arguments print output
- Confusing arguments with variables outside
- Believing arguments stop function execution
a and b in R?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 BQuick Check:
Function args use (a, b) [OK]
- Using square brackets instead of parentheses
- Separating arguments with semicolons
- Omitting parentheses around arguments
add <- function(x, y = 5) {
x + y
}
add(3)Solution
Step 1: Understand default argument values
The functionaddhasydefaulting to 5 if not provided.Step 2: Call the function with one argument
Callingadd(3)usesx=3and defaulty=5, so result is 3 + 5 = 8.Final Answer:
8 -> Option AQuick Check:
3 + default 5 = 8 [OK]
- Expecting error when argument is missing
- Ignoring default argument values
- Confusing which argument has default
multiply <- function(x, y) {
result <- x * z
return(result)
}Solution
Step 1: Check variables used inside function
The function useszbut onlyxandyare arguments;zis undefined.Step 2: Understand variable scope
Variables inside functions must be defined or passed as arguments;zis missing.Final Answer:
Variablezis not defined inside the function -> Option DQuick Check:
Undefined variable = error [OK]
- Using variables not passed as arguments
- Thinking return is optional error
- Believing arguments must have defaults
greet that takes a name and an optional greeting message (default "Hello"). Which function definition is correct to achieve this?Solution
Step 1: Identify default argument placement
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 placesmessagewith default afternamewithout default and uses paste correctly.Final Answer:
greet <- function(name, message = "Hello") { paste(message, name) } -> Option AQuick Check:
Default args last = correct [OK]
- Placing default arguments before required ones
- Forgetting default values
- Mixing argument order in paste function
Ready to prove it?
Pass the test and this pattern moves unknown
