Lesson 1
Exploring Functions in Go
Introduction

Greetings, seeker of knowledge! Today, we're embarking on a journey to navigate the world of Go. We will focus particularly on Go functions — one of the key components in the programming universe.

Just as a recipe guides you through blending ingredients to create a delightful dish, functions in programming accept specific inputs or arguments, process them, and generate an output. For instance, consider a function in a meal-prep app. You input what ingredients you have, the app processes the input, and instantly, you receive a list of recipes you can cook. A striking illustration, isn't it?

Are you ready to start creating your own Go functions? Let's dive in!

Learning to Write Go Functions

So, how do we create a Go function? It's quite straightforward and involves several key components: the func keyword, the function's name, parentheses (), return types, and curly brackets {}. Here's an example:

Go
1func helloWorld() { 2 fmt.Println("Hello, World!") 3}

In this example, helloWorld is the name of the function. Within its body, enclosed by curly brackets {}, it performs an operation — printing the phrase "Hello, World!". We invoke or "call" this function using its name followed by parentheses. Let's give it a try:

Go
1func helloWorld() { 2 fmt.Println("Hello, World!") 3} 4 5// Call the function 6helloWorld() // Prints: "Hello, World!"

When the code above runs, it prints "Hello, World!" on your screen. Congratulations! You've just coded your very first Go function.

Empty Function Body in Go

Unlike Python, Go doesn't have a pass operator to define empty functions. Instead, you can use a TODO comment to signal an as-yet-undefined function in Go.

Go
1func notImplementedFunction() { 2 // TODO: Implement this. 3}

If you attempt to call this function now, it won't do anything because nothing has been defined within its body.

Functions with Arguments

Just as a recipe can vary based on the ingredients used, functions in Go become more versatile as we introduce different arguments. These inputs allow our Go functions to produce varied results.

Go
1func greet(message string) { 2 fmt.Println(message) 3} 4 5// Call the function with "Good Morning!" 6greet("Good Morning!") // Prints: "Good Morning!" 7// Call the function with "Good Evening!" 8greet("Good Evening!") // Prints: "Good Evening!"

Here, the greet function accepts a message argument of type string and prints it. When we use a specific argument, it prints exactly this string. It allows us to execute the same code snippet a bit differently with a short call.

Functions that Give Back: Return Values

Recipes don't just guide you; they also yield a dish ready to be savored. Similarly, functions can return outputs from inputs using the return keyword. Note that the function must specify the type of the returned value before the opening curly bracket.

Go
1func addNumbers(num1 int, num2 int) int { 2 result := num1 + num2 3 return result 4} 5 6// Call the function 7sum := addNumbers(3, 7) 8fmt.Println(sum) // Prints: 10

Like a meticulously followed recipe, the addNumbers function takes num1 and num2 as inputs, combines them, and then the return statement sends the sum back. This returned value is then assigned to the variable named sum, which we proudly display.

Concluding Our Journey

Congratulations! We've just journeyed across the Go universe, discovering the magic of Go functions, understanding their uses, and creating our own functions. The insights gleaned from this journey and the practical skills we acquired through learning and using Go functions lay the foundation for exploring more advanced Go functionalities that await us in future voyages.

Now, it's time to put this newfound knowledge into practice. Brace yourself because, up next, we have a multitude of experiments and exercises designed just for you. These are designed to test your skills and enhance your command over Go functions. So let's get coding, and may the creativity of culinary arts inspire your functions!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.