notexOPENOverview
Notex/Languages/Practice: defer and call by value
03.17LESSON 17 OF 18 · 1 MIN READ

Practice: defer and call by value

Always free to read

These exercises test your knowledge of defer statments and call by value in Go.

  1. Write a function called echo with an int parameter that returns an int. Use defer and a closure to print the return value.

  2. Write a function called mystery with an int parameter that returns an int. Inside mystery, there are three deferred closures. The first one assigns 10 to the return value, the second assigns 20 to the return value, and the third assigns 30 to the return value. Call this function from main and print out the result. Try to guess what will be printed out before you run the program.

  3. Write a function called ager that takes in a slice of person (with age and name fields) and has no return values. Your function should modify the slice by increasing everyone’s age by 1. Call this function from main and print out the result.

Progress is saved on this device.
KEEP EXPLORING

One note leads to another.

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