Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions concepts/functions/.meta/config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"blurb": "TODO: add blurb",
"authors": ["ErikSchierboom"],
"blurb": "Functions split a program up into logical units of computation. They optionally take arguments and return values.",
"authors": ["bobtfish"],
"contributors": []
}
92 changes: 72 additions & 20 deletions concepts/functions/about.md
Original file line number Diff line number Diff line change
@@ -1,40 +1,92 @@
# About

Functions in Go are considered [first class citizens][first-class-functions] making them very powerful. They can be used as:
A function allows you to group code into a reusable unit.
It consists of the `func` keyword, the name of the function, and a comma-separated list of zero or more parameters and types in round brackets.
All parameters must be explicitly typed, there is no type inference for parameters.
There are no default values for parameters, all function parameters are required.

- [public and private][public-vs-private] functions of a package
- attached to custom types also called a [method][methods] ([public and private][public-vs-private])
- assigned to variables
- passed into to functions as parameters and returned from functions (higher order functions)
- create custom types
- be used anonymously
- be used as closures
```go
import "fmt"

// No parameters
func PrintHello() {
fmt.Println("Hello")
}

// One parameter
func PrintHelloName(name string) {
fmt.Println("Hello " + name)
}
```

A function can have zero or more parameters. All parameters must be explicitly typed, there is no type inference for parameters. A function can also have multiple return values which must also be explicitly typed. Values are returned from functions using the [`return` keyword][return]. To allow a function, or a method to be [called by code in other packages][public-vs-private], the name of the function must start with a capital letter.
## Return Values

The function arguments are followed by zero or more return values which must also be explicitly typed.
Single return values are left bare, multiple return values are wrapped in parenthesis.
Values are returned to the calling code from functions using the [`return` keyword][return].
There can be multiple `return` statements in a function.
The execution of the function ends as soon as it hits one of those `return`s.
If multiple values are to be returned from a function, they are comma seperated.
More information about idiomatic use of [multiple return values][concept-multiple-return-values] can be found in the linked concept.

```go
package greeting
func Hello(name string) string {
return "Hello " + name
}

// Hello is a public function
func HelloAndGoodbye(name string) (string, string) {
return "Hello " + name, "Goodbye " + name
}
```

## Invoking Functions

Invoking a function is done by specifying the function name and passing arguments for each of the function's parameters in parenthesis.

```go
import "fmt"

// No parameters, no return value
func PrintHello() {
fmt.Println("Hello")
}
// Called like this:
PrintHello()

// One parameter, one return value
func Hello(name string) string {
return hello(name)
return "Hello " + name
}
// Called like this:
greeting := Hello("Dave")

// hello is a private function
func hello(name string) string {
return "Hello " + name
// Multiple parameters, multiple return values
func SumAndMultiply(a, b int) (int, int) {
return a+b, a*b
}
// Called like this:
aplusb, atimesb := SumAndMultiply(a, b)
```

Invoking a function from inside the same package is done by specifying the function name and passing arguments for each of the function's parameters.
## Named Return Values and Naked Return

Invoking a function from another package is done by specifying the package name as well as the function name.
As well as parameters, return values can optionally be named.
If named return values are used, a `return` statement without arguments will return those values, this is known as a 'naked' return.

```go
phrase := greeting.Hello("John")
func SumAndMultiplyThenMinus(a, b, c int) (sum, mult int) {
sum, mult = a+b, a*b
sum -= c
mult -= c
return
}
```

## Other types of functions

Functions in Go are considered [first class citizens][first-class-functions] making them very powerful.
There are a number of other concepts around functions like [concept:go/methods]() and anonymous functions which you will meet later in your journey.

[first-class-functinos]: https://golangbot.com/first-class-functions
[methods]: https://golang.org/ref/spec#Method_declarations
[return]: https://golang.org/ref/spec#Return_statements
[public-vs-private]: https://golang.org/ref/spec#Exported_identifiers
[concept-multiple-return-values]: /tracks/go/concepts/multiple-return-values
85 changes: 61 additions & 24 deletions concepts/functions/introduction.md
Original file line number Diff line number Diff line change
@@ -1,46 +1,83 @@
# Introduction

TODO: the content below is copied from the exercise introduction and probably needs rewriting to a proper concept introduction
A function allows you to group code into a reusable unit.
It consists of the `func` keyword, the name of the function, and a comma-separated list of zero or more parameters and types in round brackets.
All parameters must be explicitly typed, there is no type inference for parameters.
There are no default values for parameters, all function parameters are required.

## packages

## functions

## variables
```go
import "fmt"

Go is a statically-typed language, which means that everything has a type at compile-time. Assigning a value to a name is referred to as defining a variable. A variable can be defined either by explicitly specifying its type, or by assigning a value to have the Go compiler infer its type based on the assigned value.
// No parameters
func PrintHello() {
fmt.Println("Hello")
}

```go
var explicit int // Explicitly typed
implicit := 10 // Implicitly typed
// One parameter
func PrintHelloName(name string) {
fmt.Println("Hello " + name)
}
```

The value of a variable can be assigned using the `:=` and updated using the `=`. Once defined, a variable's type can never change.
## Return Values

The function arguments are followed by zero or more return values which must also be explicitly typed.
Single return values are left bare, multiple return values are wrapped in parenthesis.
Values are returned to the calling code from functions using the `return` keyword.
There can be multiple `return` statements in a function.
The execution of the function ends as soon as it hits one of those `return`s.
If multiple values are to be returned from a function, they are comma seperated.

```go
count := 1 // Assign initial value
count = 2 // Update to new value
func Hello(name string) string {
return "Hello " + name
}

// Compiler error when assigning different type
// count = false
func HelloAndGoodbye(name string) (string, string) {
return "Hello " + name, "Goodbye " + name
}
```

A function can have zero or more parameters. All parameters must be explicitly typed, there is no type inference for parameters. A function can also have multiple return values which must also be explicitly typed. Values are returned from functions using the `return` keyword. To allow a function to be called by code in other packages, the name of the function must start with a capital letter.
## Invoking Functions

Invoking a function is done by specifying the function name and passing arguments for each of the function's parameters in parenthesis.

```go
package greeting
import "fmt"

// Hello is a public function
// No parameters, no return value
func PrintHello() {
fmt.Println("Hello")
}
// Called like this:
PrintHello()

// One parameter, one return value
func Hello(name string) string {
return hello(name)
return "Hello " + name
}
// Called like this:
greeting := Hello("Dave")

// hello is a private function
func hello(name string) string {
return "Hello " + name
// Multiple parameters, multiple return values
func SumAndMultiply(a, b int) (int, int) {
return a+b, a*b
}
// Called like this:
aplusb, atimesb := SumAndMultiply(a, b)
```

Invoking a function is done by specifying the function name and passing arguments for each of the function's parameters.
## Named Return Values and Naked Return

As well as parameters, return values can optionally be named.
If named return values are used, a `return` statement without arguments will return those values, this is known as a 'naked' return.

```go
func SumAndMultiplyThenMinus(a, b, c int) (sum, mult int) {
sum, mult = a+b, a*b
sum -= c
mult -= c
return
}
```

Go supports two types of comments. Single line comments are preceded by `//` and multiline comments are inserted between `/*` and `*/`.
29 changes: 6 additions & 23 deletions concepts/functions/links.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,4 @@
[
{
"url": "https://golang.org/ref/spec#Assignments",
"description": "assignment"
},
{
"url": "https://golang.org/ref/spec#Assignments",
"description": "assignment"
},
{
"url": "https://golang.org/ref/spec#Operators",
"description": "operators"
},
{
"url": "https://golang.org/ref/spec#Exported_identifiers",
"description": "public-vs-private"
},
{
"url": "https://golang.org/ref/spec#Method_declarations",
"description": "methods"
},
{
"url": "https://golang.org/ref/spec#Exported_identifiers",
"description": "public-vs-private"
Expand All @@ -28,8 +8,11 @@
"description": "return"
},
{
"url": "https://golang.org/ref/spec#Exported_identifiers",
"description": "public-vs-private"
"url": "https://tour.golang.org/basics/4",
"description": "A tour of Go: functions"
},
{ "url": "https://golang.org/ref/spec#Comments", "description": "comments" }
{
"url": "https://gobyexample.com/functions",
"description": "Go by Example: Functions"
}
]
21 changes: 21 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,22 @@
],
"status": "beta"
},
{
"name": "Lasagna Master",
"slug": "lasagna-master",
"uuid": "41167bbf-c2e6-4946-83d8-0ea108e5ce8d",
"concepts": [
"functions"
],
"prerequisites": [
"conditionals-if",
"numbers",
"strings",
"comparison",
"slices"
],
"status": "beta"
},
{
"name": "Need For Speed",
"slug": "need-for-speed",
Expand Down Expand Up @@ -1851,6 +1867,11 @@
"slug": "interfaces",
"uuid": "a492060c-11e7-448e-bdbd-6a907cec9493"
},
{
"name": "Functions",
"slug": "functions",
"uuid": "e737f47b-5d1a-423a-8d6a-cd201a6e4e44"
},
{
"name": "Iteration",
"slug": "iteration",
Expand Down
24 changes: 24 additions & 0 deletions exercises/concept/lasagna-master/.docs/hints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Hints

## 1. Estimate the preparation time

- Use the `len()` keyword to determine the number of layers (length of the layers array).

## 2. Compute the amounts of noodles and sauce needed

- First, set up two variables to track the amount of noodles and sauce.
- Use a for loop to iterate through the layers.
- If you encounter a `'noodles'` or `'sauce'` layer in your loop, increase the amount stored in the respective variable accordingly.

## 3. Add the secret ingredient

- Revisit [slices][concept-slices] to find out how to retrieve an element from an slice and how to add something the end of a slice.
- The index of the last element in an array `a` is `len(a) - 1`.

## 4. Scale the recipe

- First make a new slice of the same size as the input slice
- Use a [for range loop][concept-range-iteration] to iterate through the input slice and generate the output slice

[concept-conditonals-if]: /tracks/go/concepts/conditionals-if
[concept-slices]: /tracks/go/concepts/slices
Loading