Key Concepts

Review core concepts you need to learn to master this subject

Go Pass by Value Parameter

When a Go function parameter is passed by value, it means only a copy of the value is accessed and manipulated inside the function. The original value of the variable that is passed as an argument to the function remains intact.

func makeMeOlder(age int) { age += 5 } func main() { myAge := 10 makeMeOlder(myAge) fmt.Println(myAge) // myAge is still 10 }
Learn Go: Functions
Lesson 1 of 2
  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. We’ve referenced scope before in our [conditionals exercise about scoped short declarations](…
  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 also have the ability to return multiple values. Check out the example below: func GPA(midtermGrade float32, finalGrade float32) (string, float32) { averageGrade := (midtermGrade + fin…
  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…

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