Chapter 42: Create/Call Function

Create/Call Function” in Go (or in almost any beginner tutorial), they want to understand two things at once:

  1. How to create (define / declare / write) a function
  2. How to call (invoke / use / execute) a function

These are the two sides of the same coin — you can’t really use functions without mastering both.

Let me explain everything like your personal Go teacher sitting next to you with laptop open — slowly, clearly, with many copy-paste examples, style notes, common patterns, mistakes to avoid, and real-world usage.

1. Creating (Defining) a Function – The Syntax

The most basic form:

Go

Key parts explained:

  • func keyword (always first)
  • functionName → starts with uppercase → exported (public, visible from other packages) → starts with lowercase → unexported (only usable inside this package)
  • Parameters: name first, type after (opposite of C++/Java)
  • Return type(s): after the parameter list
  • Multiple return values are very common (especially value, error)
  • Braces {} are mandatory (even for one line)

2. Calling (Invoking / Using) a Function

You call a function by writing its name + parentheses + arguments in the correct order.

Go

If it has multiple returns:

Go

3. Step-by-Step Examples – Let’s Build Them Together

Create a file functions.go and let’s add functions one by one.

Example 1 – No parameters, no return (void-like)

Go

Example 2 – Parameters + single return

Go

Short parameter syntax when types are the same:

Go

Example 3 – Multiple return values (the Go classic)

Go

Very important pattern: Most Go functions that can fail return (value, error) — check err != nil immediately.

Example 4 – Named return values (powerful & idiomatic)

Go

Named returns are auto-initialized to zero value — very useful when you have early returns.

Example 5 – Variadic function (accepts variable number of arguments)

Go

Rule: only one variadic parameter and it must be the last one.

6. Common Patterns You Will Use Every Day

Pattern 1: Error-returning functions

Go

Pattern 2: Helper functions

Go

Pattern 3: Functions returning functions (closures)

Go

7. Style & Best Practices (2025–2026 community standard)

  • Keep functions short (< 30–40 lines ideal)
  • One function = one clear responsibility
  • Use descriptive names (especially exported ones)
  • Return early on errors
  • Prefer multiple return values over panic / exceptions
  • Use named returns when there are 2+ return values
  • Pass slices/maps by value (cheap header copy)
  • Use pointers only when you need to modify or avoid large copy

8. Quick Practice – Try Writing These

  1. Function greetUser(name string, age int) that prints a personalized greeting
  2. Function max(a, b int) int that returns the larger number
  3. Function getArea(length, width float64) (area float64, err error)
  4. Variadic function joinStrings(sep string, parts …string) string

Which one was easiest / most fun to write?

Any part still confusing?

  • Why multiple returns instead of exceptions?
  • Difference between named vs unnamed returns?
  • When to use closures / functions as values?
  • Or ready for structs or methods next?

Keep writing and calling small functions — once you feel comfortable creating and using them, you’ll be able to build real, useful programs very quickly.

You’re progressing beautifully — keep asking! 💪🇮🇳🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *