Passing variable number of arguments in a function

Follow the latest of Whizdumb’s journey here.

Continuing with our learning of functions in go, lets now explore how a function can accept variable number of arguments in a function.

Lets modify calculateMinimumAndMaximum(x []int) (int,int) from previous post to accept a variable number of arguments rather than an array or slice.

Lets update the function signature and definition first.

func calculateMinimumAndMaximum(x ...int) (min int, maximum int) {
    minimum := -1
    max := 0    
    for _, v := range x {
        if minimum == -1 || v < minimum {
          minimum = v
        }
        if max < v {
            max = v
        }
    }
    return minimum, max 
}


Here we have defined a function which accepts a variable number of arguments of type int. The functions are called Variadic functions

Now we can call this function with a variable number of arguments, all of which should be of type int.

Here is how the whole thing looks like.

package main

import "fmt"

func main() {      
    minimum, maximum := calculateMinimumAndMaximum(48,96,86,68,
    57,82,63,70,
    37,34,83,27,
    19,97, 9,17,)
    fmt.Println("Minimum value is", minimum)
    fmt.Println("Maximum value is", maximum)   
}

func calculateMinimumAndMaximum(x ...int) (min int, maximum int) {
    minimum := -1
    max := 0    
    for _, v := range x {
        
        if minimum == -1 || v < minimum {
        
          minimum = v
        }
        
        if max < v {
         
            max = v
        }
    }
    return minimum, max 
}

~~ Whizdumb ~~

Advertisements

Returning multiple values from a function in Go

In the previous post we looked at how we can create and use functions in go.

Now lets give a look at how we can return multiple values from a function and use them.
We will modify the function in the previous post to return both minimum and maximum value in the array

package main

import "fmt"

func main() {    
 x := []int{
    48,96,86,68,
    57,82,63,70,
    37,34,83,27,
    19,97, 9,17,
}    
    minimum, maximum := calculateMinimumAndMaximum(x)
    fmt.Println("Minimum value is", minimum)
    fmt.Println("Maximum value is", maximum)   
}


func calculateMinimumAndMaximum(x []int) (int, int) {
    minimum := -1
    max := 0    
    for _, v := range x {
        
        if minimum == -1 || v < minimum {
        
          minimum = v
        }
        
        if max < v {
         
            max = v
        }
    }
    return minimum, max 
}

In the code above, we have a function calculateMinimumAndMaximum which returns two integer values, the minimum and maximum number in the array list.

Now, it is fairly evident in the above code that of the two values returned the first is minimum and the second is maximum. How did we know that? Because we can look at the last return statement of the function, which return minimum and maximum.

But if we do not have access to the source code but know only the method signature

  func calculateMinimumAndMaximum(x []int) (int,int)

how can we know what is the first value and what is the second value.

Go comes to rescue here! We can specify names for our return values in our method signature

so our signature

  func calculateMinimumAndMaximum(x []int) (int,int)

can be converted to

  func calculateMinimumAndMaximum(x []int) (min int, maximum int)

Now we know the first value is minimum and second value is maximum from signature itself.

Here is how the final code looks like now.


package main

import "fmt"

func main() {    
 x := []int{
    48,96,86,68,
    57,82,63,70,
    37,34,83,27,
    19,97, 9,17,
}    
    minimum, maximum := calculateMinimumAndMaximum(x)
    fmt.Println("Minimum value is", minimum)
    fmt.Println("Maximum value is", maximum)   
}


func calculateMinimumAndMaximum(x []int) (min int, maximum int) {
    minimum := -1
    max := 0    
    for _, v := range x {
        
        if minimum == -1 || v < minimum {
        
          minimum = v
        }
        
        if max < v {
         
            max = v
        }
    }
    return minimum, max 
}


Pretty Awesome!

~~ Whizdumb ~~

Working with functions in Golang

Follow the latest of Whizdumb’s journey here.

Functions are an integral part of any language, they help not just in building a functionality but also make the code cleaner, less buggy and easy to understand.

Lets explore a function in Go language, Here is a program which return the minimum of the given positive numbers

package main
import "fmt"
func main() {    
 x := []int{
    48,96,86,68,
    57,82,63,70,
    37,34,83,27,
    19,97, 9,17,
}    
    minimum := -1    
    for _, v := range x {
        
        if minimum == -1 || v < minimum {
        
          minimum = v
        }
    }
    fmt.Println(minimum)
}

We just used few functions here. main() is a function which gets invoked when we run this code. The main() like in C or Java forms the entry point from where the code starts executing.

We also used another function Println which is part of “fmt” package which we imported.

Now lets create our own function. Lets extract the logic to calculate the minimum out to another function and print it. Here is how it looks like

package main
import "fmt"

func main() {
    
 x := []int{
    48,96,86,68,
    57,82,63,70,
    37,34,83,27,
    19,97, 9,17,
}
  calculateMinimum(x)
}

func calculateMinimum(x []int) {
    minimum := -1
    for _, v := range x {
        
        if minimum == -1 || v < minimum {
        
          minimum = v
        }
    }
    fmt.Print(minimum)    
}

We have created a function called calculateMinimum() which accepts an array of integers and returns nothing.

Lets now modify it to return a value

package main
import "fmt"
func main() {
     x := []int{
    48,96,86,68,
    57,82,63,70,
    37,34,83,27,
    19,97, 9,17,
}
    minimum := calculateMinimum(x)
    fmt.Println("Minimum value is", minimum)
}

func calculateMinimum(x []int) int {
    minimum := -1
    for _, v := range x {
       if minimum == -1 || v < minimum {   
          minimum = v
        }
    }
    return minimum    
}

Here, notice the change in signature of the function from func calculateMinimum(x []int) to func calculateMinimum(x []int) int the extra int in the end tells the compiler that we are expecting an int value to be returned from this function.

We are assigning the returned value to minimum variable in the main function and using it to print the minimum value.

In the next blog we will see how we can return multiple values from a function.

Sd:
~~ Whizdumb ~~