-
-
Notifications
You must be signed in to change notification settings - Fork 673
Functions concept #1720
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Functions concept #1720
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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": [] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. | ||
bobtfish marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ```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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 `*/`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.