← Back to Go Course | Chapter 4: Functions | Lesson 1 of 7

Function Basics

A function is a labeled recipe you write once, so you can ask for it by name whenever you need that job done again.
Syntax
go
func name(param type) returnType {
    // body
    return value
}

Declaring a Function

A function is declared with the func keyword, a name, a parenthesized parameter list (each with a name and type), and an optional return type. The body runs whenever the function is called elsewhere in the program.

Example: Declaring a Function

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the fmt package
import "fmt"

// Define the function square
func square(n int) int {
	// Send n * n back to the caller
	return n * n
}

// main is where the program starts running
func main() {
	// Print the values followed by a newline
	fmt.Println(square(6))
}

Multiple Parameters

Functions can take several parameters, each declared with its own name and type, or grouped together when consecutive parameters share the same type to reduce repetition.

Note: Consecutive parameters sharing a type can omit the type on all but the last, e.g. 'func add(a, b int) int'.

Example: Multiple Parameters

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the fmt package
import "fmt"

// Define the function rectangleArea
func rectangleArea(width, height float64) float64 {
	// Send width * height back to the caller
	return width * height
}

// main is where the program starts running
func main() {
	// Print the values followed by a newline
	fmt.Println(rectangleArea(4.5, 3.0))
}

Functions with No Return Value

A function that doesn't need to hand back a result simply omits the return type, and inside its body a bare return (or reaching the end of the function) exits it.

Example: Functions with No Return Value

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the fmt package
import "fmt"

// Define the function logMessage
func logMessage(msg string) {
	// Print the values followed by a newline
	fmt.Println("[LOG]", msg)
}

// main is where the program starts running
func main() {
	logMessage("server started")
}

Every Path Must Return

If a function's signature declares a return type, the Go compiler requires that every possible execution path through the function ends in a return statement -- an if without a matching else that also returns won't compile.

Example: Every Path Must Return

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the fmt package
import "fmt"

// Define the function sign
func sign(n int) string {
	// Check whether n > 0
	if n > 0 {
		// Send "positive" back to the caller
		return "positive"
	} else if n < 0 {
		// Send "negative" back to the caller
		return "negative"
	}
	return "zero"
}

// main is where the program starts running
func main() {
	// Print the values followed by a newline
	fmt.Println(sign(-5))
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting that Go requires the parameter type after each name, and that consecutive same-typed parameters can share one type annotation.
  2. Trying to call a function before it's declared in the file -- unnecessary in Go, since order of top-level declarations doesn't matter, unlike some scripting languages.
  3. Not returning a value on every code path when the function signature declares a return type, which is a compile error.
Chapter Summary
  • Functions are declared with 'func name(params) returnType { }'.
  • Parameters of the same type can share a single type annotation.
  • Every code path in a function with a declared return type must return a value.
  • Top-level function order in a file doesn't matter -- Go resolves all declarations before running.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.