Menu
Coddy logo textTech

Calling Functions

Part of the Fundamentals section of Coddy's GO journey — lesson 53 of 109.

Calling Functions

Let's learn how to call functions in Go. Calling a function means executing a function that has been declared so we can use its functionality.

First, let's create a simple greeting function and call it with different names:

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

func main() {
    greet("Alex")
    greet("Taylor")
}

After executing this code, we'll see two greeting messages on the screen:

Hello, Alex
Hello, Taylor

Now, let's call a function that returns a value and store that result in a variable:

func addThree(a, b, c int) int {
    return a + b + c
}

func main() {
    result := addThree(10, 20, 30)
    fmt.Println(result)
}

After executing this code, the function will add the three numbers together and return the sum. The variable result will store the value 60, and then we'll see this output on the screen:

60

That's how we call functions in Go - simply write the function name followed by parentheses containing any required arguments. If the function returns a value, we can capture it in a variable.

challenge icon

Challenge

Beginner

In this challenge, you'll practice calling functions in Go. We've defined three simple functions for you: sayHello, sayGoodbye, and sayThankYou. Your task is to call each of these functions in the correct order to produce the expected output.

Each function simply prints a greeting message. You just need to call them in the right sequence.

Cheat sheet

To call a function in Go, write the function name followed by parentheses containing any required arguments:

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

func main() {
    greet("Alex")
    greet("Taylor")
}

To call a function that returns a value and store the result:

func addThree(a, b, c int) int {
    return a + b + c
}

func main() {
    result := addThree(10, 20, 30)
    fmt.Println(result)
}

Try it yourself

package main

import "fmt"

// This function prints a hello message
func sayHello() {
    fmt.Println("Hello, friend!")
}

// This function prints a goodbye message
func sayGoodbye() {
    fmt.Println("Goodbye, friend!")
}

// This function prints a thank you message
func sayThankYou() {
    fmt.Println("Thank you, friend!")
}

func main() {
    // TODO: Call the sayHello function
    
    // TODO: Call the sayThankYou function
    
    // TODO: Call the sayGoodbye function
    
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals