By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
welcome always prints the same lines, which is a thin kind of reuse. The real power arrives when a function accepts parameters: named inputs whose values the caller chooses. The function becomes a machine. Give it a number, get an answer for that number.
Penny stores money in integer cents, so a function that turns 1250 into $12.50 is a good first machine. The caller supplies 1250; the function returns the readable string.
The line between func and the opening brace is called the signature. In func formatCents(cents int) string, the signature says: the name is formatCents, there is one parameter named cents of type int, and the function returns a string.
Go puts the parameter name first and its type second. Two parameters of the same type share one type: func add(a, b int) int declares both a and b as int. Read a signature from left to right and it tells you everything you need in order to call the function.
package main
import "fmt"
func formatCents(cents int) string {
dollars := cents / 100
remainder := cents % 100
return fmt.Sprintf("$%d.%02d", dollars, remainder)
}
func main() {
fmt.Println(formatCents(1250))
fmt.Println(formatCents(99))
fmt.Println(formatCents(5))
fmt.Println(formatCents(100000))
}Inside the body, cents behaves like a variable that already has a value. dollars and remainder are ordinary local variables. The %02d verb from step 8 pads the cents part to two digits, so five cents prints as $0.05, not $0.5.
return does two things at once: it ends the function immediately and hands the value after it back to the caller. The caller can print that value, store it in a variable, or pass it to another function.
package main
import "fmt"
func formatCents(cents int) string {
return fmt.Sprintf("$%d.%02d", cents/100, cents%100)
}
func tax(cents int) int {
return cents * 8 / 100
}
func main() {
amount := 1250
due := amount + tax(amount)
fmt.Println("Amount:", formatCents(amount))
fmt.Println("Tax: ", formatCents(tax(amount)))
fmt.Println("Total: ", formatCents(due))
}Read the output with the code: tax(1250) returns 100, which is one dollar in cents. formatCents turns each amount into dollars and cents only at the moment of printing. Keep the arithmetic in cents and format last; that is the habit Penny uses throughout the course.
The diagram shows the trip for tax(amount). The value in the caller is copied into the parameter, the body computes with that copy, and a new value comes back to the caller.
Real input is not always clean. A function that multiplies a negative number spreads the mistake. The fix is a guard clause: a small if at the top of the function that handles the bad case and returns immediately.
After the guard, the rest of the body can assume the input is valid. You read the happy path without extra indentation, and you cannot reach the calculating code with bad data.
package main
import "fmt"
func formatCents(cents int) string {
return fmt.Sprintf("$%d.%02d", cents/100, cents%100)
}
func lateFee(daysLate int) int {
if daysLate <= 0 {
return 0
}
if daysLate > 30 {
return 1500
}
return daysLate * 25
}
func main() {
fmt.Println("0 days late:", formatCents(lateFee(0)))
fmt.Println("3 days late:", formatCents(lateFee(3)))
fmt.Println("45 days late:", formatCents(lateFee(45)))
}lateFee(0) returns 0 at the first guard. lateFee(45) passes the first guard, fails the second one, and returns the flat 1500. Only lateFee(3) reaches the final line. Because return is an exit, nothing after it in the same block runs.
Copy the last program and change the late fee rule: over 14 days costs a flat $10.00 (1000 cents), and each earlier day costs 30 cents instead of 25. Predict the three amounts before you look at the output. Then add func discount(cents, percent int) int that returns 0 when the percent is not positive or the amount is not positive, and otherwise subtracts cents * percent / 100 from cents. Call it with 1250, 10 and expect 1125, then call it with 1250, 0 and confirm the guard returns 0.
Key takeaways
A parameter is a typed input. The caller's argument is copied into it.
func name(param type) resultType is the signature; in Go the name comes before the type.
return hands a value back and ends the function immediately.
A guard clause handles invalid input at the top, so the rest of the body can assume the good case.
Do Penny's money arithmetic in integer cents and call formatCents only when printing.
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.