-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added MD files for closure and recursion
Signed-off-by: Dave Augustus <[email protected]>
- Loading branch information
1 parent
6a6dcf5
commit 360c032
Showing
2 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,53 @@ | ||
In Go (Golang), a closure is a function that references variables declared outside of its own scope. This means it can access and assign values to these variables even after the scope in which they were declared has been destroyed1. | ||
|
||
Here’s an example to illustrate this concept: | ||
|
||
``` | ||
package main | ||
import "fmt" | ||
func newCounter() func() int { | ||
GFG := 0 | ||
return func() int { | ||
GFG += 1 | ||
return GFG | ||
} | ||
} | ||
func main() { | ||
counter := newCounter() | ||
fmt.Println(counter()) | ||
fmt.Println(counter()) | ||
} | ||
``` | ||
|
||
Here’s another example of a closure in Go: | ||
|
||
``` | ||
package main | ||
import "fmt" | ||
// outer function | ||
func greet() func() string { | ||
// variable defined outside the inner function | ||
name := "John" | ||
// return a nested anonymous function | ||
return func() string { | ||
name = "Hi " + name | ||
return name | ||
} | ||
} | ||
func main() { | ||
// call the outer function | ||
message := greet() | ||
// call the inner function | ||
fmt.Println(message()) | ||
} | ||
``` | ||
|
||
In this example, the greet function returns an anonymous function that references the name variable1. When we call greet, it returns this anonymous function, which we assign to message. Even though the greet function has finished executing (and so the name variable should be destroyed), we can still access name when we call message(). This is because the anonymous function acts as a closure, maintaining access to name even after greet has finished1. |
This file contains 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,31 @@ | ||
In Go (Golang), recursion is a programming concept where a function calls itself. This technique is used to solve problems that can be broken down into smaller, repetitive problems1. | ||
|
||
Here’s an example of a simple recursive function in Go: | ||
|
||
|
||
``` | ||
package main | ||
import "fmt" | ||
func countDown(number int) { | ||
if number >= 0 { | ||
fmt.Println(number) | ||
countDown(number - 1) | ||
} | ||
} | ||
func main() { | ||
countDown(3) | ||
} | ||
``` | ||
|
||
|
||
In this example, the countDown function calls itself, decrementing the number each time until number is no longer greater than or equal to 01. This is the base case that stops the recursion2. | ||
|
||
Recursion can be of different types2: | ||
|
||
Direct Recursion: The function calls itself directly2. | ||
Indirect Recursion: The function calls another function, and this function, in turn, calls the original function2. | ||
Tail Recursion: The recursive call is the last thing executed by the function2. | ||
Head Recursion: The recursive call is the first statement in the function2. | ||
Remember, it’s important to have a base case in a recursive function to prevent infinite recursion1. Without a base case, the function would keep calling itself indefinitely, which could lead to a stack overflow error3. |