Lesson 8Using Functions
Functions are an important aspect of any programming language in that they allow us to create reusable blocks of code that can use a variety of input values. You've used functions in previous lessons, including the functions main
and Println
. In this lesson, you will take a closer look at functions and learn to create your own.
DEFINING A FUNCTION
A function is a block of organized, reusable code that uses one or more Go statements to complete a single, related action. You use functions to help with code reusability, increased readability, and redundancy checking (making sure that you do not use the same lines over and over within an application).
Go provides many built-in functions, such as the fmt.Println()
function that you've been using throughout this book; however, you are also allowed to create or define your own functions. A function that you define yourself in a Go program is known as a user-defined function.
As you progress through this lesson, here are a few key terms you should know:
- Functions are blocks of organized, reusable code.
- User-defined functions are functions defined by the user. These functions use the
def
keyword to define them. - Built-in ...
Get Job Ready Go now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.