Problem overview
You are given two integers, a and b, and the function must return their sum. For a = 4 and b = 9 the answer is 13.
The task is deliberately tiny. Its purpose is to show how a coding challenge works: you get a function with parameters, the parameters are the input, and you return the answer.
Reading the function
The starter code gives you the function signature:
func sum(a int, b int) int {
// Write the first step below:
}
func sumdeclares a function namedsum.(a int, b int)lists two parameters. Each one has a name followed by its type, and both areint. When the lesson's tests run, they callsumwith different pairs of numbers and the parameters hold those values.- The
intafter the parameter list is the result type. It tells Go that the function must hand back an integer.
Go is strict about this. A function with a result type has to end with a return that supplies a value of that type. With the body left empty, the code does not compile and reports missing return.
How to approach any challenge
Every challenge in the course follows this pattern, so it helps to build a routine. Read the statement and the example until you can say the task in one sentence. Look at the parameters, since they are your input, and at the result type, since it tells you what to produce. Work the example by hand: here, 4 plus 9 is 13. Only then write code, and run it against the example before you submit.
Step by step
There is only one step: inside sum, return the sum of a and b. Add the parameters with the + operator and put return in front. The return keyword hands the value back to whoever called the function and ends the call.
The solution in Go
func sum(a int, b int) int {
// Write the first step below:
return a + b
}
A few details about these lines:
a + bis an expression, soreturn a + breturns the computed value without needing a variable to hold it. You could writetotal := a + band thenreturn total, with the same result.- Go does not convert between number types on its own. The sum of two
intvalues is anint, which matches the declared result. Adding anintto afloat64would be a compile error. - Go also allows
func sum(a, b int) int, where parameters of the same type share one type name. The lesson uses the longer form, and both mean the same thing. - On this platform an
inthas 64 bits. If the true sum is beyond the range of anint, the result silently wraps around instead of raising an error. That is far from the sizes in this challenge.
Complexity
The function does one addition, whatever the values are, so it takes constant time, O(1), and constant extra space.
Edge cases and testing
Try a few inputs in the editor above, calling the function with your own values:
sum(4, 9)gives13, the lesson's example.sum(-3, 3)gives0. Negative numbers need no special handling, because+works on them directly.sum(0, 0)gives0.
The most common beginner mistakes are leaving the function body empty, which gives missing return, and writing a + b without return, which Go rejects with an error saying the expression is not used. A result only reaches the caller through return.