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 has an external name and an internal name. External name is used by 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 a 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 iOS tab, then select Blank template, click Next button.
    xcode playground project ios blank template
  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.

Below is the normal swift function definition code. The parameter has three part ( external_name internal_name:data_type ), for example ( x a:Int ). Then when you call the function from outside, you need to pass the parameter value use external name ( x: 3 ). And in the function body, you can get the parameter value use internal name ( a ).

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)

Below is above code output when execute.

18

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)

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.

You can also suppress function parameter external name when the function parameter is not too much or easy to understand. 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)

Above code also print 18 in the console when you run it.

READ :   How To Fix Warning Attempt To Present UIAlertController On ViewController Whose View Is Not In The Window Hierarchy

Reference

  1. How To Use Xcode Playground Project Example

Leave a Reply

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.