Step 1 of 5 · Reading · ~3 min
Learn
Functions
Functions
A function gives a piece of work a name, a list of inputs and a result, so the rest of the program can ask for it without knowing how it is done. By the end of this lesson you can read and write a Go signature, say exactly what a function receives, and keep returning a value separate from printing one.
The shape of a declaration
Read it left to right: the keyword func, the name, the parameters in parentheses, then the result type — the type of the value the function hands back. Types come after names (word string, never string word), and the result type sits outside the parentheses, just before the brace.
A call is an expression: it is the value that came back, so you can print it or store it.
Parameters that share a type can share one annotation: func clamp(lo, hi, v int) int declares three independent int parameters. Leave the result type off entirely and the function hands nothing back:
Every path must return
Promise a result type and the compiler holds you to it on every branch:
Adding return "not positive" after the if fixes it. Go catches this at build time; there is no silent empty value at runtime.
Parameters are copies
Go passes arguments by value — the function gets its own copy:
The cure is not a trick, it is a result: have the function hand the new number back and assign it at the call site. (Changing a caller's variable on purpose needs a pointer — the & you have been giving fmt.Scan — and gets its own lesson later.)
Returning is not printing
A function can hand a value back, or it can print it, and the two are not interchangeable. Do both, and the value shows up twice:
<go>
<go>
Output is compared character for character, so the extra line fails the test even though the number or word on it is right. Pick one side: the function computes and returns, the caller prints.
Two results at once (a preview)
Result types go in parentheses, returned values are separated by commas. Go uses this shape everywhere to hand back a value and a status; the next lesson is about exactly that.
Common mistakes
- Printing inside the function and again in
main: every input produces its answer twice, on two lines, and exact comparison rejects the whole output. - Reaching for
math.Pow. It speaksfloat64, and large results print in scientific notation:fmt.Println(math.Pow(123456, 2))prints1.5241383936e+10, not15241383936. Multiplying twointvalues keeps the result anint. - Adding where the task multiplies.
x + xagrees with x-times-x only at 0 and 2, so a single check on an input like 2 hides the bug; at 3 it yields 6 where 9 is wanted. - Special-casing negative input. A negative times a negative is already positive; flipping the sign by hand turns the answer for -5 into -25.
Your exercise
The starter declares square with a placeholder result, and a main that already reads the number and prints whatever square hands back. Write the body so the value returned is the argument multiplied by itself. The likeliest slip is printing inside square too, which makes every answer arrive twice.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…