0
0
Goprogramming~3 mins

Why Return values in Go? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if your functions could hand you answers like a helpful friend every time?

The Scenario

Imagine you are baking cookies and want to share how many you made with your friend. Without a clear way to tell them the exact number, you have to keep repeating or writing it down manually every time.

The Problem

Manually tracking and sharing results is slow and confusing. You might forget the number or mix it up, causing mistakes and extra work.

The Solution

Return values let a function send back results automatically. This way, you get the exact answer every time without extra steps or confusion.

Before vs After
Before
func bakeCookies() {
    fmt.Println("Baked 12 cookies")
}
// No way to get the number back
After
func bakeCookies() int {
    return 12
}
// Now you can use the number anywhere
What It Enables

Return values make your code clear and reusable by passing results directly where needed.

Real Life Example

When calculating a bill, a function can return the total amount so you can print it, save it, or use it for discounts without repeating calculations.

Key Takeaways

Return values send results from functions back to the caller.

They avoid manual tracking and reduce errors.

They make code easier to use and understand.