PROGRAM:
OUTPUT:
Result of 'sum1' function - 30
Result of 'sum2' function - 35
Result of 'sum3' function - 7 Status - true
Result of 'sum3' function - 0 Status - false
Result of 'sum3' function - 35
package main
import "fmt"
/* take two integer as arguments and return integer value */
func sum1(a int, b int) int {
return a + b
}
/* Compine variable and assign name for return variable */
func sum2(a, b int) (c int) {
c = a + b
return
}
/* Return multiple value from function */
func sum3(a, b int) (int, bool) {
if a == 0 || b == 0 {
return 0, false
}
c := a + b
return c, true
}
func main() {
out1 := sum1(10, 20)
fmt.Println("Result of 'sum1' function - ", out1)
out2 := sum2(15, 20)
fmt.Println("Result of 'sum2' function - ", out2)
out3, rc := sum3(5, 2)
fmt.Println("Result of 'sum3' function - ", out3, "Status - ", rc)
out3, rc = sum3(5, 0)
fmt.Println("Result of 'sum3' function - ", out3, "Status - ", rc)
/* ignore the return value using '_' */
out3, _ = sum3(25, 10)
fmt.Println("Result of 'sum3' function - ", out3)
}
OUTPUT:
Result of 'sum1' function - 30
Result of 'sum2' function - 35
Result of 'sum3' function - 7 Status - true
Result of 'sum3' function - 0 Status - false
Result of 'sum3' function - 35
No comments:
Post a Comment