Learn Go: Functions
Learn how to write Go functions in this action-packed course!
StartKey Concepts
Review core concepts you need to learn to master this subject
Go Pass by Value Parameter
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
- 1In 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…
- 2As 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…
- 4While 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…
- 5We 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…
- 6Functions 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…
- 7Functions also have to ability to return multiple values. Check out the example below: func GPA(midtermGrade float32, finalGrade float32) (string, float32) { averageGrade := (midtermGrade + fina…
- 8We 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…
What you'll create
Portfolio projects that showcase your new skills
How you'll master it
Stress-test your knowledge with quizzes that help commit syntax to memory