Functions and Multiple Files


Why Functions?

Without functions, all code would be placed inside main().

Example:

func main() {
    // hundreds of lines of code
}

As a program grows, this becomes difficult to read and maintain.
Functions allow code to be divided into small, reusable units.


Defining a Function

func sayHello() {
    fmt.Println("Hello")
}

Calling the function:

sayHello()

Output:

Hello

Function Anatomy

func sayHello() {
    fmt.Println("Hello")
}

Components:

func

Keyword used to define a function.

sayHello

Function name.

()

Parameter list.

{}

Function body.


Function Parameters

Functions can receive input values.

func greet(name string) {
    fmt.Println("Hello", name)
}

Usage:

greet("Alice")
greet("Bob")

Output:

Hello Alice
Hello Bob

Parameter Syntax

Go places the type after the variable name.

func greet(name string)

Compared to C++:

void greet(string name)

This style is used consistently throughout Go.


Return Values

Functions can return data.

func add(a, b int) int {
    return a + b
}

Usage:

result := add(3, 5)
 
fmt.Println(result)

Output:

8

Multiple Parameters of Same Type

Instead of:

func add(a int, b int) int

Go allows:

func add(a, b int) int

This is the preferred style.


Example: Square Function

func square(x int) int {
    return x * x
}

Usage:

fmt.Println(square(5))

Output:

25

Multiple Source Files

A package can be split across multiple files.

Directory:

main.go
helper.go

main.go

package main
 
import "fmt"
 
func main() {
    fmt.Println(message())
}

helper.go

package main
 
func message() string {
    return "Hello from another file"
}

Output:

Hello from another file

Why Does This Work?

Both files belong to the same package:

package main

During compilation, Go combines all files belonging to the same package.

Conceptually:

Package main
 ├── main.go
 └── helper.go

Functions in one file can be called directly from another file in the same package.

No import is required.


Package vs Module

A common source of confusion.

Structure:

Module
 └── Package
      ├── File
      ├── File
      └── File

Example:

myproject/
    go.mod
 
    main.go
    helper.go

go.mod defines the module.

Files beginning with:

package main

belong to the same package.
Go builds all files in the package together.


Concepts Learned

Functions

func greet() {
}

Reusable blocks of code.


Parameters

func greet(name string)

Allow functions to receive data.


Return Values

func greet() string

Allow functions to return data.


Multiple Parameters

func add(a, b int) int

Shared type declaration.


Multiple Files

A package may span several source files.


Packages

package main

Files in the same package can access each other’s exported and unexported functions.


Session Summary

Topics covered:

  • Function definition
  • Function calls
  • Parameters
  • Return values
  • Multiple parameters
  • Splitting code across files
  • Package organization
  • Relationship between modules, packages, and files

Practical skills gained:

  • Creating reusable functions
  • Returning values from functions
  • Organizing code across multiple files
  • Understanding how Go builds packages

Key Takeaway

Functions are Go’s primary way to organize behavior.
Packages are Go’s primary way to organize code.

A typical Go program grows by:

Variables

Functions

Multiple Files

Packages

Structs

The next major topic is Structs, which provide a way to group related data together.
Go Learning - Session 4