Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // A function can take zero or more arguments.
- // In this example, add takes two parameters of type int.
- // Type comes after the variable name.
- package main
- import "fmt"
- func add(x int, y int) int {
- return x + y
- }
- // Func add can be shortened:
- func adds(x, y int) int {
- return x + y
- }
- // Multiple returns
- func swap(x, y string) (x, y string) {
- return x, y
- }
- // Return values may be named.
- // If so, they are treated as variables defined at the top of the function.
- func split(sum int) (x, y int) {
- x = sum * 4 / 9
- y = sum - x
- return
- }
- func main() {
- fmt.Println(add(42, 13))
- a, b := swap("Hello", "world")
- fmt.Println(a, b)
- fmt.Println(split(17))
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement