An Overview of Golang Functions
In this blog post, we discuss Golang Functions. By the end of the class, you’ll be expected to master how to use Golang functions. But first, let’s start by defining what functions are and their importance in programming.
A function—also known as procedures or subroutines in some programming languages—is a section of code that’s independent of the main code that helps to map zero or more input arguments. Ideally, the main purpose of using a function in a code is to assist group program statements to perform specific tasks.
Why do we need functions in Golang programs?
Well, functions are very useful constructs in helping you to organize your code. If you don’t use functions, you may end up writing so many lines of codes that may not scale up well. One common function that we’ve been using so far in our earlier discussions is:
func main () {}
This is the core function of every Golang program that you’ll be creating. It’s the function that initializes execution of your code. It invokes other functions—both user defined and in-built functions—in your Golang code.
For you to learn how to create your functions, you need to first, understand how to declare functions.
All Golang functions must start with the keyword “func” which is followed by a function’s name. What follows after the function name is a list of arguments—parameters or inputs that the function will use. Here’s a syntax:
func function_name (list of arguments)
You’ll define the arguments by indicating their name type. Here’s an example:
func add(a float32, b float32)
The above function has two arguments namely: “an” and “b” which are floating point numbers. Collectively all the arguments and their return type are called function’s signature. In a nutshell, all Golang functions must begin with the keyword “func” followed by the function name and the function signature enclosed in brackets.
When two or more logical function arguments share the data type, you can leave the data type from all but the last argument. Here’s an example:
a float32, b float32
Can be shortened to:
a, b float32
How to implement functions in Golang
We implement functions by declaring any other variable(s) that may not have been declared in the function signature followed by program statements that will depend on what you want your function to do.
Here’s an example:
func add(a float32, b float32) float32 { return a + b }
The return statement causes the execution of a function to leave the current procedure or subroutine and resume in the main function. Sometime, functions can also be valued. That means that they can be passed around just like all the other values—that may be used as function parameters.
The return statement that has no arguments returns the named return values in Golang. This is referred to as “naked” return. The naked return statements should only be used with small functions. Otherwise they can make readability of longer functions to be difficult.
Functions in Golang can return a different number of results. Here’s an example:
func example1 (a, b string) (string, string) { return a, b }
Next up, we explore how to call functions in your main program.
Functions are called by their names in the main function “func main ( ).” For instance, here’s an example that shows how the function above “example1” can be called into the main function.
package main import "fmt" func example1 (a, b string) (string, string) { return a, b } func main() { c, d:= example1("Hallo my dear!!", "I have just returned from Iowa") fmt.Println(c, d) }
By following the rules of using functions that we’ve just learnt in this lesson—you’ll be on your way to becoming a professional in Golang!
GET YOUR FREE Golang EBOOK!