Variadic Functions in Go

GoGoBeginner
Practice Now

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

Introduction

In Go, a function that can take a variable number of arguments is called a variadic function. This challenge will test your understanding of how to use variadic functions in Go.


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-15448{{"`Variadic Functions in Go`"}} end

Variadic Functions

In this challenge, you need to implement a function named max that takes an arbitrary number of integers as arguments and returns the maximum value.

Requirements

  • The function max should take an arbitrary number of integers as arguments.
  • The function max should return the maximum value of the integers passed as arguments.

Example

$ go run variadic-functions.go
[1 2] 3
[1 2 3] 6
[1 2 3 4] 10

## Another key aspect of functions in Go is their ability
## to form closures, which we'll look at next.

Summary

In this challenge, you learned how to use variadic functions in Go. You implemented a function named max that takes an arbitrary number of integers as arguments and returns the maximum value.

Other Go Tutorials you may like