notexOPENOverview
Notex/Languages/Practice: Function values and closures
03.13LESSON 13 OF 18 · 1 MIN READ

Practice: Function values and closures

Always free to read

These exercises test your knowledge of function values and closures.

  1. Assign an anonymous function to a variable that prints its int argument. Then call it inside a for i := range 5 loop.

  2. Write a function called makeCounter that takes in no parameters and returns a closure with the function signature func() int. The first call to the closure should return 1, and each subsequent call should return the next-largest integer. Demonstrate that these two separate counters are independent. 6.Write two functions, double(int) int, which returns two times the value of its parameter, and square(int) int, which returns the square of its parameter. Write a third function transform(nums []int, f func(int) int) []int. This function applies f to each element and returns a new slice. Call it twice, once with double as a parameter, and once with square.

Progress is saved on this device.
KEEP EXPLORING

One note leads to another.

Back to the library →
⌕
Search across the entire library ⌘ K