Swift - Function with Parameters

Introduction

A function can optionally define one or more named typed inputs.

The following function takes in one single typed input parameter :

func myFunction ( num : Int ) {
     print(num)
}

The num parameter is used internally within the function, and its data type is Int.

To call this function, call its name and pass in an integer value, like this:

myFunction (5)
//or

var num = 5
myFunction (num)

The following function takes in two input parameters, both of type Int :

func myFunction( num1: Int, num2: Int ) {
     print(num1, num2)
}

To call this function, pass it two integer values as the argument:

myFunction (5, 6)

Related Topic