What if your functions could hand you answers like a helpful friend every time?
Why Return values in Go? - Purpose & Use Cases
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.
Manually tracking and sharing results is slow and confusing. You might forget the number or mix it up, causing mistakes and extra work.
Return values let a function send back results automatically. This way, you get the exact answer every time without extra steps or confusion.
func bakeCookies() {
fmt.Println("Baked 12 cookies")
}
// No way to get the number backfunc bakeCookies() int {
return 12
}
// Now you can use the number anywhereReturn values make your code clear and reusable by passing results directly where needed.
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.
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.