iOS Swift Function External And Internal Parameter Name Example

When we define a swift function, we can provide function parameters to it, and each function parameter can have an external name and an internal name. External name is used by the caller to pass value to the parameter, internal name is used by the function body to get the parameter value. Below is a swift function example. This example is edited and executed in an Xcode playground file.

1. Create A Xcode Playground Project.

  1. Open Xcode, and click File —> New —> Playground menu item at Xcode top menu bar.
  2. Click the iOS tab, then select Blank template, click the Next button.
  3. Give the playground project a name and select a folder to save the playground file. Now you can edit and execute code in it.

2. Define Swift Function Example.

  1. Below is the normal swift function definition code. The parameter has three-part ( external_name internal_name:data_type ), for example ( x a:Int ).
  2. Then when you call the function from outside, you need to pass the parameter value using the external name ( x: 3 ). And in the function body, you can get the parameter value use the internal name ( a ).
  3. Please note the function parameter internal name is a constant variable, so you can not change it’s value directly.
    func multiple(x a:Int, y b:Int) -> Int
    {
        var ret:Int = a * b
        return ret
    }
    
    var c:Int = multiple(x: 3, y: 6)
    
    print(c)
  4. Below is above code output when execute.
    18
  5. Although you provide the function parameter external name and value when call it, but the function parameter’s order should not be changed, otherwise it will through an error. Below is an example.
    func multiple(x a:Int, y b:Int) -> Int
    {
        var ret:Int = a * b
        return ret
    }
    
    // Call function multiple with reverse ordered parameters.
    var c:Int = multiple(y: 6, x: 3)
    
    print(c)
  6. When you execute above code, it will throw below error.
    error: HelloWorldPlayground.playground:288:28: error: argument 'x' must precede argument 'y'
    var c:Int = multiple(y: 6, x: 3)
                         ~~~~~~^~~~
                         x: 3,

3. Suppress Function Parameter External Name.

  1. You can also suppress function parameter external name when the function parameter is not too much or easy to understand.
  2. To do this you should use _ to replace the function parameter external name like below.
    // Use _ to suppress the first parameter's external name.
    func multiple( _ a:Int, y b:Int) -> Int
    {
        var ret:Int = a * b
        return ret
    }
    
    // Do not need to specify the first parameter's external name.
    var c:Int = multiple(3, y: 6)
    
    print(c)
  3. Above code also print 18 in the console when you run it.

Reference

  1. How To Use Xcode Playground Project Example

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.