EZ

Eduzan

Learning Hub

Eduzan
Eduzan / Go Programming Language

Functions & Methods

Computer Science / Go Programming Language tutorial chapter - Published 2025-12-16 - Go Programming Language

In Go, functions are segments of code designed to carry out specific tasks. They can be reused throughout the program to optimize memory usage, enhance code clarity, and save time. Functions may return a value to the caller or not, depending on their implementation.

Syntax:

func function_name(Parameter-list)(Return_type) {
    // function body...
}

Example:

package main

import "fmt"

// add() takes two integers and returns their sum
func add(x, y int) int {
    return x + y
}

func main() {
    sum := add(8, 12)
    fmt.Printf("Sum: %d", sum)
}

Output:

Sum: 20

Function Declaration

In Go, functions are defined using the func keyword, followed by the function name, a parameter list, and an optional return type.

Syntax:

func function_name(Parameter-list)(Return_type) {
    // function body...
}

Example:

func add(x, y int) int {
    return x + y
}

Output:

marks are 500 or more

Explanation:

  • func: Used to declare a function.
  • function_name: The name of the function, e.g., add.
  • Parameter-listx, y int are the parameters with their types.
  • Return_typeint specifies the return type.

Function Calling

To execute a function, use its name followed by any required arguments in parentheses. For instance, add(8, 12) invokes the function with the arguments 8 and 12.

Example:

sum := add(8, 12)
fmt.Printf("The sum is: %d", sum)

Output:

// Go program to demonstrate
// the use of nested if statements
package main

import "fmt"

func main() {

   // Declare two variables
   var a int = 250
   var b int = 500

   // Check the first condition
   if a < 300 {

      // If condition1 is true,
      // check the nested condition
      if b < 600 {

         // Executes if both conditions are true
         fmt.Printf("a is less than 300 and b is less than 600\n")
      }
   }
}

Function Arguments

Go allows two methods for passing arguments to functions: Call by Value and Call by Reference. By default, Go employs call by value, where argument values are copied, ensuring that modifications inside the function do not affect the original variables.

1. Call by Value: In this approach, the argument values are passed as copies to the function. Any changes made to these values inside the function remain local to the function.

Example:

package main

import "fmt"

func add(x, y int) int {
    x = x + 5 // modifying x within the function
    return x + y
}

func main() {
    a := 10
    b := 20
    fmt.Printf("Before: a = %d, b = %d\n", a, b)
    total := add(a, b)
    fmt.Printf("Sum: %d\n", total)
    fmt.Printf("After: a = %d, b = %d\n", a, b)
}

Output:

Before: a = 10, b = 20
Sum: 35
After: a = 10, b = 20

2. Call by Reference: In this method, pointers are passed to the function, allowing modifications made inside the function to affect the original variables.

Example:

package main

import "fmt"

func add(x, y *int) int {
    *x = *x + 5 // modifying x via its memory address
    return *x + *y
}

func main() {
    a := 10
    b := 20
    fmt.Printf("Before: a = %d, b = %d\n", a, b)
    total := add(&a, &b)
    fmt.Printf("Sum: %d\n", total)
    fmt.Printf("After: a = %d, b = %d\n", a, b)
}

Output:

value is 150
End of lesson.