Introduction to Functions
This lesson discusses the introductory concepts of functions in Go before going into detail.
Functions are the basic building blocks of the Go code. They are very versatile, so Go can be said to have a lot of characteristics of a functional language. Functions are a kind of data because they are themselves values and have types. Let’s start this chapter by elaborating on the elementary function-description, which we discussed briefly in Chapter 2.
Basics of a function in Go
Every program consists of several functions. It is the basic code block. The order in which the functions are written in the program does not matter. However, for readability, it is better to start with main() and write the functions in a logical order (for example, the calling order).
Purpose of a function
The main purpose of functions is to break a large problem, which requires breaking many code lines into a number of smaller tasks. Also, the same task can be invoked several times, so a function promotes code reuse. In fact, a good program honors the Don’t Repeat Yourself principle, meaning that the code which performs a certain task may only appear once in the program.
Execution of a function
A function ends when it has executed its last statement before }, or when it executes a return statement, which can be with or without argument(s). These ...