Topic Overview and Lesson Plan

Greetings, Space Explorer! Today, we're going to dive into the realm of calling functions in Go. By the end of this lesson, you will have become proficient in function calls, transitive function calls, and returning multiple values from functions in Go.

Recall the Function Calling in Go

Just as you use a remote to switch on your television, calling a function in Go executes a set of commands. It's akin to issuing instructions to your spaceship's computer. Let's use a familiar scenario in space travel as an example: welcoming a new crew member. Here's how that could look:

package main
import "fmt"

func greet(name string) {
    fmt.Printf("Hello %s, welcome aboard the spacecraft!\n", name)
}

func main() {
    greet("Astro Kid")  // Prints: "Hello Astro Kid, welcome aboard the spacecraft!"
}

In this example, we've instructed our spacecraft's computer to greet the newcomer, Astro Kid! Calling the greet function triggers the code block within that function.

Practical Explanation of Transitive Function Calling

In interstellar parlance, transitive function calling is tantamount to delegating spaceship tasks. You call one or multiple functions inside another function, assigning responsibility. Consider a scenario where we wish to fire up our spaceship's engine. Instead of handling the task directly, we delegate it to another function:

package main
import "fmt"

func power_up_engine(level int) {
    fmt.Printf("Powering up the engine to level %d\n", level)
}

func engine_start() {
    fmt.Println("The engine has started!")
}

func launch_spacecraft() {
    fmt.Printf("Prepare for launch. ")
    power_up_engine(3)
    engine_start()
}

func main() {
    launch_spacecraft()
    /*
        Prints:

        Prepare for launch. Powering up the engine to level 3
        The engine has started!
    */
}

In this example, as we launch the spacecraft, we call power_up_engine(3) to power up the engine of our spaceship. We then call engine_start() to handle the post-startup operations of the engine. Thus, one function can certainly call another to complete a task.

Understanding Functions with Multiple Return Values in Go

Go allows a function to return multiple results. Think of it as performing multiple checks simultaneously, such as monitoring the fuel status and oxygen levels before takeoff. Here's an example of our spaceship's computer in action, returning multiple outputs:

package main
import "fmt"

func spaceship_status() (string, string) {
    // Returning multiple values
    return "Fuel: Full", "Oxygen: Normal"
}

func main() {
    fuel_status, oxygen_status := spaceship_status()

    fmt.Println(fuel_status) // Prints: "Fuel: Full"
    fmt.Println(oxygen_status) // Prints: "Oxygen: Normal"
}

In this case, the spaceship_status function performs both a fuel check and an oxygen check, returning both results. Note that we specify two return types, one for each return value, using (string, string) syntax.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal