Chapter 43: Parameters Arguments
parameters vs arguments (also called: formal parameters vs actual arguments / parameters vs arguments)
This distinction trips up almost everyone coming from Python, JavaScript, Java or C++ at first — because Go is extremely explicit and strict about types and naming.
Let me explain it like we’re sitting together with VS Code open, writing small functions, calling them, and discussing every detail slowly and clearly.
1. The Two Sides of the Coin – Quick Definitions
| Term | Where it lives | Meaning | Also called | Example in code |
|---|---|---|---|---|
| Parameter | Inside the function definition | The placeholder name and type the function expects to receive | Formal parameter | func add(a int, b int) → a and b are parameters |
| Argument | Outside — when calling the function | The actual value you pass when you call the function | Actual argument / value | add(17, 25) → 17 and 25 are arguments |
Very simple sentence to remember forever:
The function defines parameters You pass arguments to fill those parameters
2. Visual Example – Side by Side
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ // These are PARAMETERS (placeholders + types) func add(a int, b int) int { return a + b } // When we call it ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ // These are ARGUMENTS (real values you supply) result := add(17, 25) // 17 goes into a, 25 goes into b |
So:
- a and b → parameters (they exist only inside the function definition)
- 17 and 25 → arguments (they are the concrete values you give when calling)
3. Different Ways Parameters Are Declared in Go
Go has very clean and expressive syntax for parameters.
Style 1 – Full type every time
|
0 1 2 3 4 5 6 7 8 9 |
func printInfo(name string, age int, height float64, isStudent bool) { fmt.Printf("Name: %s, Age: %d, Height: %.1f cm, Student: %t\n", name, age, height, isStudent) } |
Style 2 – Same-type parameters → list types once (very common)
|
0 1 2 3 4 5 6 7 8 |
func rectangleArea(length, width float64) float64 { return length * width } |
→ length float64, width float64 → shortened to length, width float64
Style 3 – No parameters
|
0 1 2 3 4 5 6 7 8 |
func sayHello() { fmt.Println("Namaste!") } |
Style 4 – Variadic parameters (…)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 |
func sumAll(numbers ...int) int { // numbers is a slice inside the function total := 0 for _, n := range numbers { total += n } return total } |
→ only last parameter can be variadic → inside function it becomes a slice []int
4. How Arguments Are Passed (Very Important in Go)
Go uses pass-by-value — but for slices/maps/channels it’s cheap because you copy only the header (pointer + len + cap).
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
func modifySlice(s []int) { s[0] = 999 // changes the original slice's backing array s = append(s, 777) // this creates new slice header – does NOT affect caller } func main() { numbers := []int{10, 20, 30} modifySlice(numbers) fmt.Println(numbers) // [999 20 30] – first element changed // but append did NOT add 777 to original } |
Key takeaway:
- Primitive types (int, string, bool, float64, struct, array) → full copy
- Slice / map / channel / function / interface → only descriptor/header copied (very cheap)
- If you want to modify the caller’s slice header (len/cap), use pointer: func modifySlice(s *[]int)
5. Named Parameters? (No – Go has none)
Go has no named arguments like Python (func(a=1, b=2)). Arguments must be passed in the exact order of parameters.
|
0 1 2 3 4 5 6 7 8 9 10 |
// No such thing in Go: divide(b=8, a=100) // compile error // Correct order only divide(100, 8) |
6. Real-World Patterns You Will Use Every Day
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
// Pattern 1: Classic error-returning function func fetchUser(id int) (*User, error) { // ... return user, nil } // Pattern 2: Variadic + slice spread func printAll(items ...string) { for _, item := range items { fmt.Println(item) } } fruits := []string{"apple", "banana"} printAll("orange", fruits...) // spread slice |
7. Your Quick Practice – Try Writing These
- Function greet(name string, age int) that prints personalized message
- Function max(a, b int) int – returns larger number
- Variadic function join(separator string, words …string) string → like strings.Join but you write it yourself
- Function swap(x, y *int) that swaps two integers using pointers
Which one was easiest / most fun?
Any part still confusing?
- Why pass-by-value + cheap slice header is actually good?
- Why no named parameters / keyword arguments in Go?
- Variadic vs slice parameter – when to choose which?
- Or ready for return types / multiple returns in more depth next?
Keep writing and calling small functions — this is the moment where you go from “learning syntax” to “building real logic”.
You’re doing really great — keep asking! 💪🇮🇳🚀
