Skip to main content

Command Palette

Search for a command to run...

Return a function from a function

Published
1 min readView as Markdown

When you want a function to return a String, it's pretty obvious:

func whatIsForLunch() -> String {
    return "Something delicious"
}

If you want to return a function, then the return type looks more like this: () -> Void. The key bits that trip me up:

  • You need { } to assign the function to a variable or constant.
  • You need () to use the assigned function
func howToMakeACake() -> (() -> Void) {
   // return some function
}

var cakeRecipe: () -> Void 
cakeRecipe = { howToMakeACake() }  // you need the { }

if timeForCake {
    cakeRecipe()  // you need the () to run the function
}
8 views

More from this blog

CoderLyn's developer journey

136 posts

I'm an iOS developer, helping millennials with their finances through US neo-bank, Empower.

Before making the switch to development, I was a strategist and GM in the tech space.