Documentation or explanation for keyword 'in' as used in function arguments

I am learning Swift and trying to find information about what the keyword 'in' does when used in a function argument as in these examples:

func fillEllipse(in rect: CGRect)
func drawSomething(in rect: CGRect, in context: CGContext, 
                   with colorSpace: CGColorSpace?)

From the context, I gather that it essentially means put the ellipse in a rectangle, draw something in a rectangle, in a context, etc. but what is actually happening when 'in' is used like this? Is it related to closures, or something else?

In Xcode, option-click on 'in' doesn't provide any information, and I haven't been able to find any explanations of this usage on the internet.

This is not a keyword, this is a so-called argument label, which is intended to be used to make function declarations and calls read more naturally. It has no "impact" (as in "what is actually happening"); whoever writes a function chooses how to name it and its argument labels and parameter names.

As an example, suppose we are calling the second function you mentioned. Without argument labels, the call site would look like this:

drawSomething(rect, ctx, myColorSpace)

— however with labels, it clarifies the usage of arguments:

drawSomething(in: rect, in: ctx, with: myColorSpace)

(although I personally would rename the second label into into:, because one usually says that they draw into a context).


I've been reading about anonymous functions so I guess I just started thinking that it was related. Thanks!

It is related — in a way — anonymous functions can't have argument labels because the latter are a part of the function's name.

For example, init(contentsOfFile:encoding:) is a full name of one of the initializers for String, and the contentsOfFile and encoding labels are rightful parts of its name. This way you can have multiple functions with the same identifier (that is, init), which then differ in their argument labels, making the overall name unique.

So because anonymous functions do not have a name, they don't have both the identifier and argument labels.

1 Like

Maybe this should have been my original question. Here is an example from a book I'm trying to understand. The topic is transforming a function into an anonymous function. What is 'in' in this situation?

Named function:

func whatToDoLater(finished:Bool) { 
    print("finished: \(finished)")

Anonymous function:

   (finished:Bool) -> () in 
   print("finished: \(finished)")

Oh, this in is a separator between the function type and the function body, and it is a keyword in this case. The general "grammar" for anonymous functions (which we more often call "closures" in Swift) is

{ /*parameters and type*/ in /*function body*/ }

— in your example, (finished:Bool) -> () is the function type along with a parameter name and print("finished: \(finished)") is the body, they are separated by the in keyword. You may think of this keyword as simply a substitute for the opening curly brace { in normal function declarations.

Bottomline: there simply had to be something as a separator there, and the keyword in was chosen for this purpose. There is a post on this further.