Parameters and Returns
A function can take more than one parameter. Separate them with commas:
package main
import "fmt"
func printTotal(price int, count int) {
fmt.Println("Total:", price*count)
}
func main() {
printTotal(5, 3)
printTotal(12, 2)
}
Output:
Total: 15
Total: 24
Arguments are matched to parameters by position. In printTotal(5, 3), the first argument 5 goes into price and the second argument 3 goes into count.
When parameters next to each other have the same type, you can write the type once. func printTotal(price, count int) means the same thing as func printTotal(price int, count int).
A call must give one argument for each parameter. With too few, Go stops at compile time:
package main
import "fmt"
func printTotal(price, count int) {
fmt.Println("Total:", price*count)
}
func main() {
printTotal(5)
}
Output:
# command-line-arguments
./main.go:10:13: not enough arguments in call to printTotal
have (number)
want (int, int)
have (number) is what the call gave: one number. want (int, int) is what the function needs: two int values.
Returning a value
printTotal prints its answer, and then the answer is gone. Often you want the answer back, so you can save it or use it in more math. To do that, write a return type after the parentheses, and send the value back with return:
package main
import "fmt"
func total(price, count int) int {
return price * count
}
func main() {
t := total(5, 3)
fmt.Println(t)
fmt.Println(total(12, 2) + 10)
}
Output:
15
34
The int after (price, count int) says that total gives back an int. The return line sends the value back to the code that called the function. So the call total(5, 3) works like the value 15. You can store it in a variable, print it, or use it in an expression, as the second Println does.
This is usually better than printing inside the function. The caller decides what to do with the result: print it, add to it, or compare it in an if.
return ends the function
When Go runs a return, the function stops right there. No line after it runs. This lets you handle a special case first and return early:
package main
import "fmt"
func grade(score int) string {
if score >= 50 {
return "pass"
}
return "fail"
}
func main() {
fmt.Println(grade(70))
fmt.Println(grade(30))
}
Output:
pass
fail
For 70, the if is true, so the function returns "pass" and the last line never runs. For 30, the if is false, so Go goes on to return "fail".
Go checks that a function with a return type always returns a value. Remove the last return "fail" and the program does not compile:
package main
import "fmt"
func grade(score int) string {
if score >= 50 {
return "pass"
}
}
func main() {
fmt.Println(grade(70))
}
Output:
# command-line-arguments
./main.go:9:1: missing return
Line 9 is the closing } of grade. For a score below 50, the function would reach that brace with no value to give back, so Go reports a missing return.
In the exercise, complete finalTotal: return 0 when the discount is bigger than the subtotal, and the subtotal minus the discount otherwise.