Function execution flow in Go - Time & Space Complexity
When we look at how a function runs, we want to know how long it takes as the input grows.
We ask: How does the work inside the function increase when input changes?
Analyze the time complexity of the following code snippet.
func sum(numbers []int) int {
total := 0
for _, num := range numbers {
total += num
}
return total
}
This function adds up all numbers in a list and returns the total.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Adding each number to total inside a loop.
- How many times: Once for every number in the list.
As the list gets bigger, the function does more additions, one for each number.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The work grows directly with the number of items.
Time Complexity: O(n)
This means the time to finish grows in a straight line with the input size.
[X] Wrong: "The function runs in the same time no matter how many numbers there are."
[OK] Correct: Because the function must add each number, more numbers mean more work.
Understanding how function steps grow with input helps you explain your code clearly and think about efficiency.
"What if we changed the function to sum only the first half of the list? How would the time complexity change?"
Practice
Solution
Step 1: Understand function call behavior
When a function is called, Go executes the code inside it in order, from the first line to the last.Step 2: Eliminate incorrect options
Options A, B, and C describe behaviors that are not how Go functions work when called.Final Answer:
The code inside the function runs from top to bottom. -> Option AQuick Check:
Function execution flow = top to bottom [OK]
- Thinking functions run only if no parameters
- Believing functions wait for user input automatically
- Assuming functions return immediately without running code
greet that returns a string in Go?Solution
Step 1: Check Go function syntax
In Go, functions start withfunc, followed by the name, parameters, and return type. func greet() string { return "Hello" } matches this.Step 2: Verify return statement and type
func greet() string { return "Hello" } returns a string and has the correct syntax. The version using 'function greet() { return "Hello" }' uses wrong keyword. The version 'func greet() { return "Hello" }' misses return type. The version using print instead of return is incorrect.Final Answer:
func greet() string { return "Hello" } -> Option CQuick Check:
Correct Go function syntax = func greet() string { return "Hello" } [OK]
- Using 'function' keyword instead of 'func'
- Omitting return type when function returns a value
- Using print instead of return for output
package main
import "fmt"
func add(a int, b int) int {
return a + b
}
func main() {
result := add(3, 4)
fmt.Println(result)
}Solution
Step 1: Understand function call and return
The functionaddtakes two integers and returns their sum. Callingadd(3, 4)returns 7.Step 2: Check output statement
fmt.Println(result)prints the returned value, which is 7.Final Answer:
7 -> Option DQuick Check:
3 + 4 = 7 [OK]
- Concatenating numbers as strings (34)
- Expecting zero without return
- Thinking code has syntax errors
func multiply(a int, b int) int {
product := a * b
}
func main() {
fmt.Println(multiply(2, 3))
}Solution
Step 1: Check function return type and body
The function declares it returns an int but does not have a return statement, causing a compile error.Step 2: Fix by adding return statement
Addingreturn productreturns the calculated value, matching the function signature.Final Answer:
Add a return statement: return product -> Option BQuick Check:
Function must return value if declared [OK]
- Forgetting return statement in non-void functions
- Changing function signature unnecessarily
- Changing operators without reason
package main
import "fmt"
func process(x int) int {
if x > 0 {
return x * 2
}
return 0
}
func main() {
values := []int{3, -1, 0, 5}
sum := 0
for _, v := range values {
sum += process(v)
}
fmt.Println(sum)
}Solution
Step 1: Analyze function process behavior
For each value, if positive, returns double the value; else returns 0.Step 2: Calculate sum over values slice
Values: 3 -> 6, -1 -> 0, 0 -> 0, 5 -> 10; sum = 6 + 0 + 0 + 10 = 16.Final Answer:
16 -> Option AQuick Check:
Sum of doubled positives = 16 [OK]
- Adding negative or zero values incorrectly
- Forgetting return 0 for non-positive
- Misreading loop or function logic
Ready to prove it?
Pass the test and this pattern moves unknown
