Function definition in R Programming - Time & Space Complexity
When we define a function, we want to know how long it takes to run when we use it with different inputs.
We ask: How does the time to run change as the input size grows?
Analyze the time complexity of the following code snippet.
my_function <- function(x) {
result <- x + 1
return(result)
}
output <- my_function(5)
print(output)
This code defines a simple function that adds 1 to a number and returns the result.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: A single addition operation inside the function.
- How many times: Exactly once each time the function is called.
The function does the same small task no matter what number you give it.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The work stays the same no matter how big the input number is.
Time Complexity: O(1)
This means the function takes the same short time to run no matter the input size.
[X] Wrong: "The function takes longer if the input number is bigger because the number is bigger."
[OK] Correct: The function just adds 1 once, so the size of the number does not change how long it takes.
Understanding how simple functions behave helps you explain your code clearly and shows you know how to think about efficiency.
"What if the function used a loop to add 1 multiple times based on the input? How would the time complexity change?"
Practice
greet in R?Solution
Step 1: Recognize R function syntax
In R, functions are defined by assigning a function() to a name using <-.Step 2: Match the correct syntax
greet <- function() { } uses the correct syntax: name <- function() { }.Final Answer:
greet <- function() { } -> Option AQuick Check:
Function definition = name <- function() [OK]
- Using def instead of function
- Writing function name() without <-
- Placing function keyword after name
add that takes two arguments x and y in R?Solution
Step 1: Check function definition format
In R, arguments go inside parentheses after function keyword, and the body is inside braces.Step 2: Verify return statement and assignment
add <- function(x, y) { return(x + y) } correctly assigns function with arguments and uses return() properly.Final Answer:
add <- function(x, y) { return(x + y) } -> Option CQuick Check:
Arguments inside function() and return() used [OK]
- Placing arguments outside function()
- Missing braces {} around function body
- Incorrect assignment syntax
multiply <- function(a, b) {
result <- a * b
return(result)
}
multiply(3, 4)Solution
Step 1: Understand function behavior
The function multiply takes two numbers and returns their product.Step 2: Calculate the product of 3 and 4
3 * 4 equals 12, so the function returns 12.Final Answer:
12 -> Option AQuick Check:
3 * 4 = 12 [OK]
- Adding instead of multiplying
- Concatenating numbers as strings
- Forgetting to return the result
sum_values <- function(x, y) total <- x + y return(total) }
Solution
Step 1: Check function body braces
The function body must be enclosed in braces { } immediately after function() declaration.Step 2: Locate missing brace
The code lacks an opening brace { after function(x, y), causing syntax error.Final Answer:
Missing opening brace { after function() -> Option BQuick Check:
Function body needs { } [OK]
- Omitting braces around function body
- Placing return outside function
- Using wrong brackets for arguments
filter_positive that takes a numeric vector and returns only the positive numbers. Which function definition correctly does this in R?Solution
Step 1: Understand filtering in R
To get positive numbers, select elements where vec > 0.Step 2: Check return statement
filter_positive <- function(vec) { return(vec[vec > 0]) } returns vec elements where condition vec > 0 is TRUE, correctly filtering positives.Final Answer:
filter_positive <- function(vec) { return(vec[vec > 0]) } -> Option DQuick Check:
Filter with vec[vec > 0] returns positives [OK]
- Using wrong comparison operator
- Not returning filtered vector
- Returning logical vector instead of values
Ready to prove it?
Pass the test and this pattern moves unknown
