By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
Every program you have written so far lives in one place: func main. That is fine while a program is ten lines long. It stops being fine at a hundred. A function is a named block of statements. You write it once, give it a name that says what it does, and call that name whenever you want the block to run.
Functions are the first real tool for keeping code readable. They split a long story into chapters with titles. When something breaks, you read the chapter that owns the behaviour instead of rereading the whole file.
Here is a complete program with one function of your own. welcome prints two lines. main prints nothing itself: it calls welcome, and the two lines appear.
package main
import "fmt"
func welcome() {
fmt.Println("Welcome to Penny!")
fmt.Println("This function has its own block.")
}
func main() {
welcome()
}Notice the two shapes. func welcome() { ... } is a declaration: it names a block but does not run it. welcome() inside main is a call: the parentheses mean run it now. It is the same distinction you already use with fmt.Println. The name refers to the function; the parentheses run it.
Every function you write has the same pieces. Read them in order once and the shape stops being mysterious.
| Part | In this program | What it does |
|---|---|---|
func | func | Marks the start of a function declaration. |
| Name | welcome | Lowercase means it belongs to this package. Name it after the action. |
| Parameters | () | The inputs it accepts. Empty for now; step 17 adds them. |
| Return type | none | The value it sends back. Nothing yet; step 17 adds that too. |
| Body | { ... } | The statements that run on every call, top to bottom. |
When main reaches welcome(), control leaves main, runs the body of welcome, then comes back to the line after the call. Nothing after the call runs until the function returns. Step through the diagram to follow that trip.
Nothing is handed back from this function: it has no return value. Step 17 changes that, and the picture gains a result arrow.
A function body is ordinary statements, so a function can call another function. That lets you build one small piece and reuse it. The banner below is printed twice by calling the same function twice.
package main
import "fmt"
func banner() {
fmt.Println("==================")
fmt.Println(" PENNY LEDGER")
fmt.Println("==================")
}
func main() {
banner()
fmt.Println("1. add 2. list 3. total")
banner()
}If the design of the banner changes, one edit updates both places. That is the payoff of a function: one definition, many uses. You will feel it most when the same three lines appear in five places and a bug hides in one of them.
You can call a function that is declared below main, or below the function that calls it. Go reads the whole file and the whole package before running anything, so the declaration does not need to appear first.
package main
import "fmt"
func main() {
reportHeader()
}
func reportHeader() {
line()
fmt.Println("Penny report")
line()
}
func line() {
fmt.Println("----------------")
}Execution still starts at main. Go looks for a function named main with no parameters and no return value, runs it, and when main ends the program ends. Everything else runs only because some statement called it.
Write a function called farewell that prints two lines: Goodbye from Penny! and Come back soon.. Call it from main after welcome(), then call it a second time. Before you run the program, write down the exact order of the four lines you expect. Run it and compare. If you want a second exercise, add a function called separator that prints a line of dashes and call it from farewell instead of printing the dashes there.
Key takeaways
A function names and groups statements so the same work can run many times.
func welcome() { ... } declares a function; welcome() calls it.
Declaring a function runs nothing; the body runs only when something calls it.
main is an ordinary function that Go calls to start the program, and it can call other functions.
When a call finishes, control returns to the statement after it.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.