Programa Golang que usa func con dos argumentos

Las funciones pueden usar ciertos parámetros para recibir valores y realizar la tarea requerida en consecuencia. La sintaxis de una función con dos argumentos es:

func function_name( [parameter_1 parameter_2] ) [return_types]
{
   body of the function
}

Ejemplo 1: Sin ningún tipo de retorno

// Golang program that uses func with
// two arguments without any return type
  
package main
  
import "fmt"
  
// Function accepting two arguments
// arguments defined as int type
func add(x int, y int) {
    var sum int = x + y
    fmt.Println(sum) // Prints the sum
}
  
// main function
func main() {
  
    add(10, 20) // Passing arguments
}

Producción:

30

Ejemplo 2: con tipo de retorno

// Golang program that uses func with
// two arguments with any return type
package main
  
import "fmt"
  
// Function accepting two arguments
// arguments and return defined
// as int type
func add(x int, y int) int {
    var z int = x + y
    return z
}
  
// main function
func main() {
  
    // Passing arguments and receiving
    // the returned value in sum
    var sum = add(10, 20)
    // Prints the sum
    fmt.Println(sum)
}

Producción:

30

Publicación traducida automáticamente

Artículo escrito por prakhar7 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *