Function declaration in Go - Time & Space Complexity
When we declare a function, we want to know how its execution time changes as we use it with different inputs.
Here, we ask: does just declaring a function affect how long the program runs?
Analyze the time complexity of the following code snippet.
func greet(name string) string {
return "Hello, " + name
}
func main() {
message := greet("Alice")
println(message)
}
This code declares a simple function that returns a greeting message and then calls it once.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: A single function call with string concatenation.
- How many times: The function is called once; no loops or recursion.
Execution time grows very little because the function runs once and does a simple string join.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 1 call with small string work |
| 100 | Still 1 call, slightly more string work |
| 1000 | Still 1 call, more string joining but no loops |
Pattern observation: The time depends on the size of the input string but the function runs only once, so growth is very small.
Time Complexity: O(n)
This means the time grows linearly with the length of the input string, but only because of string joining, not because of repeated calls.
[X] Wrong: "Declaring a function makes the program slower as input grows."
[OK] Correct: Declaring a function itself does not run code or slow the program; only calling it does, and how often it runs matters more.
Understanding that function declaration alone does not affect time helps you focus on what really matters: how many times and how the function runs.
"What if the function greet was called inside a loop running n times? How would the time complexity change?"
Practice
Solution
Step 1: Recall Go function syntax
In Go, functions are declared using the keywordfunc.Step 2: Compare options
Only func usesfunc, which is correct for Go.Final Answer:
func -> Option AQuick Check:
Function declaration keyword = func [OK]
- Using 'function' like JavaScript
- Using 'def' like Python
- Using 'fun' which is invalid
add that takes two integers and returns an integer?Solution
Step 1: Understand Go function parameter syntax
In Go, parameters are declared asname type. Soa int, b intis correct.Step 2: Check return type and function keyword
Function must start withfuncand specify return typeint. func add(a int, b int) int {} matches this exactly.Final Answer:
func add(a int, b int) int {} -> Option DQuick Check:
Parameter syntax = name type [OK]
- Putting type before name in parameters
- Using 'function' keyword instead of 'func'
- Omitting return type when needed
package main
import "fmt"
func multiply(x int, y int) int {
return x * y
}
func main() {
fmt.Println(multiply(3, 4))
}Solution
Step 1: Understand function call and return
The functionmultiplytakes two integers and returns their product.Step 2: Calculate multiply(3, 4)
3 * 4 = 12, so the function returns 12, which is printed.Final Answer:
12 -> Option AQuick Check:
3 times 4 = 12 [OK]
- Adding instead of multiplying
- Concatenating numbers as strings
- Expecting syntax error due to function
func greet(name string) string {
return "Hello, " + name
Solution
Step 1: Check function syntax
The function starts withfunc, has parameter and return type correctly.Step 2: Look for missing parts
The function body is missing the closing brace}to end the function.Final Answer:
Missing closing brace '}' -> Option CQuick Check:
Every function must end with '}' [OK]
- Forgetting to close function body with '}'
- Confusing parameter types
- Omitting return type
filterEven that takes a slice of integers and returns a new slice containing only even numbers?Solution
Step 1: Check parameter and return types
The function must take a slice of integers[]intand return a slice of integers[]int. func filterEven(nums []int) []int { var evens []int for _, n := range nums { if n%2 == 0 { evens = append(evens, n) } } return evens } matches this.Step 2: Verify logic to filter even numbers
func filterEven(nums []int) []int { var evens []int for _, n := range nums { if n%2 == 0 { evens = append(evens, n) } } return evens } creates a new slice, loops over input, appends even numbers, and returns the new slice. This is correct.Final Answer:
func filterEven(nums []int) []int { var evens []int for _, n := range nums { if n%2 == 0 { evens = append(evens, n) } } return evens } -> Option BQuick Check:
Filter slice by appending evens [OK]
- Wrong parameter or return types
- Returning inside loop prematurely
- Printing instead of returning filtered slice
Ready to prove it?
Pass the test and this pattern moves unknown
