Go Functions Fundamentals

GoGoBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this challenge, we will learn about functions in Go. We will see how to define functions, how to pass arguments to them, and how to return values from them.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL go(("`Go`")) -.-> go/FunctionsandControlFlowGroup(["`Functions and Control Flow`"]) go/FunctionsandControlFlowGroup -.-> go/functions("`Functions`") subgraph Lab Skills go/functions -.-> lab-15397{{"`Go Functions Fundamentals`"}} end

Functions

In the given code, we have two functions plus and plusPlus. The plus function takes two integers as arguments and returns their sum. The plusPlus function takes three integers as arguments and returns their sum. Your task is to complete the plusPlus function by adding the third integer to the sum of the first two integers.

Requirements

  • The plus function should take two integers as arguments and return their sum as an integer.
  • The plusPlus function should take three integers as arguments and return their sum as an integer.
  • The plusPlus function should use the plus function to calculate the sum of the first two integers.

Example

$ go run functions.go
1+2 = 3
1+2+3 = 6

## There are several other features to Go functions. One is
## multiple return values, which we'll look at next.

Summary

In this challenge, we learned about functions in Go. We saw how to define functions, how to pass arguments to them, and how to return values from them. We also saw how to use one function inside another function.

Other Go Tutorials you may like