What if you could write a piece of code once and use it everywhere without copying it?
Why Function declaration in Go? - Purpose & Use Cases
Imagine you need to calculate the area of many rectangles in your program. Without functions, you would have to write the same calculation code over and over again everywhere you need it.
Writing the same code repeatedly is slow and tiring. It's easy to make mistakes or forget to update all copies if the calculation changes. This makes your program messy and hard to fix.
Function declaration lets you write the calculation once, give it a name, and then use that name whenever you need the calculation. This keeps your code clean, easy to read, and simple to update.
area := width * height // repeated everywhere
func area(width, height int) int {
return width * height
}
result := area(5, 10)Functions let you organize your code into reusable blocks, making programs easier to build, understand, and maintain.
Think of a coffee machine: you press a button (call a function) to get coffee instead of making it from scratch every time.
Functions save you from repeating code.
They make your program cleaner and less error-prone.
Functions help you organize and reuse your work easily.