Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Declaring a function consists of providing a function name, names and types of any parameters, and one or more return values.
- Declaring a function
-
func
keyword - Function name
- Parentheses
- Code block in curly braces that gets run when function is called
-
package main
import "fmt"
func main() {
myFunction()
}
func myFunction() {
fmt.Println("Running myFunction")
}
-
Naming rules
- Same naming rules as variables, types, etc.
- Must start with letter
- Capital letter means exported from the current package, lower case means unexported
- Convention is to use camelCase for multiple words
Can declare parameters:
package main
import "fmt"
func main() {
square(3)
add(2, 4)
subtract(10, 3)
}
func square(number int) {
fmt.Println(number * number)
}
func add(a float64, b float64) {
fmt.Println(a + b)
}
func subtract(a, b float64) {
fmt.Println(a - b)
}
Parameters are type enforced.
square("hello")
fails at compile time.-
Return values
- Must specify return value type.
- Can name return values - sometimes useful as documentation.
- Can use bare returns if return values are named.
package main
import "fmt"
func main() {
fmt.Println(square(3))
fmt.Println(add(2, 4))
fmt.Println(subtract(10, 3))
}
func square(number int) int {
return number * number
}
func add(a float64, b float64) (sum float64) {
return a + b
}
func subtract(a, b float64) (difference float64) {
difference = a - b
return
}
- Compile error if last statement of function is not a
return
, because that line of code will never be reached.
Go makes it easy to declare
your own functions.
0:00
You do so using the func key word followed
by the name you want the function to have.
0:03
So we'll name this function myFunction.
0:09
Then you include a pair of parenthesis
0:13
followed by a code block in curly braces
that gets run when the function is called.
0:16
So on this function, myFunction,
we'll just make a call to the print
0:21
line function and we'll pass up
the string Running myFunction.
0:27
Once you've declared a function you can
call it by typing the function name,
0:36
myFunction, followed by
a pair of parentheses again.
0:41
So, save this and
run it down here in the console.
0:44
The main function gets called when the
program is run and then it makes a call
0:48
to myFunction, which then prints
out the string Running myFunction.
0:53
You can see that down here on the output.
0:57
The rules for naming functions
are the same as they are for variables.
1:00
A function name has to
start with a letter.
1:03
A capital letter means the function is
exported from the current package, whereas
1:06
lowercase means it's unexported, that is,
not visible outside the current package.
1:09
And again, there's a convention that
if a function name has multiple words,
1:14
then all words after the first
should begin with a capital letter.
1:18
If we try to refer to function
names from outside the package,
1:22
we're only allowed to refer to functions
that have been exported, that is,
1:25
those whose names begin
with a capital letter.
1:28
If we try to refer to
a non-exported function name,
1:31
we'll get a compile error saying
cannot refer to unexported name.
1:34
If you want your functions
to accept parameters,
1:39
you can declare those within the
parentheses when declaring the function.
1:42
A parameter is just like any other
variable, you need to give it a name and
1:46
you need to declare a type for it.
1:49
If you need a function to
accept multiple parameters,
1:52
you can separate those with commas.
1:54
If you have multiple
parameters of the same type,
1:57
you can just list the type
after the last parameter.
1:59
So up here we make calls to
the functions we've declared and
2:03
pass arguments into each
of those parameters.
2:06
Let's try running this real quick.
2:09
So here we call the square function which
2:11
multiplies the number by itself
then prints the value out.
2:14
So 3 times 3 is 9.
2:18
Here we call the add function and
we pass it arguments of 2 and 4,
2:20
which it adds together and
prints the result out.
2:25
So 2 plus 4 is 6.
2:27
And here we call subtract.
2:28
We pass up the arguments 10 and
3, it subtracts 3 from 10,
2:30
gives us the result, 7.
2:34
And since we declare the types
of all the parameters,
2:36
that allows Go to enforce the use
of the correct arguments.
2:38
So for example, if we were to try to
pass a string to the square function and
2:42
then try to run this, we'll get an error.
2:46
Cannot use type string as type int as
an argument to the square function.
2:48
There's certain features for
2:53
method declarations that you may
be used to in other languages, but
2:54
the Go omits in order to keep method
call simple, clear, and fast.
2:58
Go doesn't allow default values for
parameters,
3:03
it doesn't allow you to name parameters,
and it doesn't allow method overloading.
3:06
That means declaring multiple methods with
the same name but different parameters.
3:10
That means that when you call
a method you're always going to
3:15
use the same number of
arguments in the same place.
3:18
The goal is to make calling
methods simple, clear, and fast.
3:22
And of course,
as in most other programming languages,
3:26
functions can return values.
3:29
So let's suppose that we wanted
to update all these functions to
3:32
return a value instead of
simply printing a value out.
3:35
We'll set up our square
function to return an int,
3:38
by specifying the int type here
following the parentheses.
3:42
And then instead of taking
the number times itself and
3:46
printing that, we're going to return
that value using the return keyword.
3:49
You can set up a variable
to hold the return value,
3:57
which is also known as
naming the return value.
4:00
Sometimes that's useful as documentation.
4:02
So if we want to do that,
we place parentheses here
4:05
after the parentheses that accept
the parameters for the function.
4:08
And we provide a name for
the return value.
4:13
So we'll call this one sum, and of course
its type is going to be a float64,
4:16
since we're adding two
float64 numbers together.
4:21
And then as before instead of printing
the value out, we'll just return it.
4:26
If you've named a return value, then you
can use what's called a bare return.
4:31
So let's name the return value for
the subtract function to be difference,
4:36
and it's going to have a type of float64.
4:41
That sets up a variable
that we can assign to and
4:47
then that value will
be used as our return.
4:49
So we can say difference = a minus b.
4:52
And then, that allows us use to use a bare
return here at the end of the function.
5:00
So we can just use the return
key word all by itself.
5:04
And that will, since we've set
difference up as a named return value
5:08
that's the value that will be returned.
5:12
So let's save this.
5:14
And up here in the main function now
that we're returning values instead,
5:16
we'll need to print them out up here.
5:21
So we'll say format.printline.
5:22
We'll print the return value of square.
5:27
We'd better change that back to
an acceptable argument type.
5:30
So we'll say square(3).
5:34
And then fmt.Println(add(2, 4)).
5:37
And fmt.Println(subtract(10, 3)).
5:42
Subtract 3 from 10.
5:45
Save that, run it, and
there are our results.
5:47
The square of 3 and 3 is 9.
5:52
The result of the adding 2 and 4 is 6.
5:54
And the result of
subtracting 3 from 10 is 7.
5:57
The values were returned from
each of the function calls
6:01
up here in the main function and
we printed each one of them.
6:04
If you were to add a statement after
the return statement within a function,
6:08
that line of code would never be breached.
6:12
Go protects you from this sort of
situation by giving you a compile error.
6:14
So let's suppose that we were
to add a call to fmt.Println(
6:18
down here after the return statement
within the square function.
6:23
Let's try running that, and
you see we get a compile error.
6:31
Missing return at end of function.
6:34
So if a function is
supposed to return a value,
6:36
you need to ensure that the last line
of that function is a return statement.
6:38
You need to sign up for Treehouse in order to download course files.
Sign up