Chevron Left Icon
Learn Go: Functions
Lesson 1 of 2
Chevron Right Icon
  1. 1
    In programming, a function is a block of code designed to be reused. As programmers, we want to find solutions to problems, but we also don’t want to do additional work when it’s not necessary. Let…
  2. 2
    As we saw before, we can bundle code in a function and call that function when we need the code inside it to run. We’ll be going over function syntax starting with a simple function definition: fu…
  3. 3
    A function definition creates something called a scope. Scope is a concept that refers to where the values and functions are defined and where they can be accessed. For instance, when a variable …
  4. 4
    While variables and their values are scoped to their functions, we do have built-in ways of passing information out of their native functions and into another namespace. Let’s describe the way that…
  5. 5
    We know that functions can return information, but we can also provide functions with information using parameters. Function parameters are variables that are used within the function to use in s…
  6. 6
    Functions are great for code reuse, this means that when you find yourself repeating the same pattern over and over, it might be a good idea to try and abstract it into a function. When you abstr…
  7. 7
    Functions can return multiple values to communicate more than a single value inside a function to the outside of a function. Check out the example below: func GPA(midtermGrade float32, finalGrade …
  8. 8
    We can delay a function call to the end of the current scope by using the defer keyword. defer tells Go to run a function, but at the end of the current function. This is useful for logging, file w…
  9. 9
    Congrats! This lesson covered how to: Define a function. Understand scope rules. Return a value from a function. Add parameters to our function. Reuse code with functions. Return multi…
  1. 1
    Go is a pass-by-value language. In other words, we’re passing functions the value of an argument. In a technical sense, when we’re calling a function with an argument, the Go compiler is stri…
  2. 2
    Picture being in class and taking part in a lesson. When we hear an important detail, we write it down in our notebook for reference later. This same idea of storing important information somewhere…
  3. 3
    In the previous exercise we went over addresses, now let’s learn how to store them. In Go, pointers do that job for us. Pointers are variables that specifically store addresses. We even set the …
  4. 4
    We know that addresses are where values are stored and pointers allow us to keep track of addresses. But what if we want the address to store a different value? Well, we can actually use our pointe…
  5. 5
    Using our knowledge of addresses, pointers, and dereferencing, let’s return to our initial problem: How can we change the value of a variable when we’re in a different scope? Let’s take a look at t…
  6. 6
    Congrats! You can now change variable values even when the variables are out of scope! The following concepts were covered in this lesson: Go is a pass-by-value language. An address is where …

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo