0Pricing
Swift Academy · Lesson

Parameters

Parameters

Parameters is a free Swift Academy lesson on CoddyKit — lesson 1 of 1. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 1 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Introduction

Hello,

In this section, we will learn about the useful features of parameters in Swift language.

The first we will learn are variadic parameters.

A variadic parameter is a parameter that can take 0 or more values ​​of the same type. It can be used when we cannot predict exactly how many parameters it will take.

example

A function can have a maximum of one variadic parameter. 

Let's do an example.

func arithmeticMean(_ numbers: Double...) -> Double {
    var total: Double = 0
    for number in numbers {
        total += number
    }
    return total / Double(numbers.count)
}

var result = arithmeticMean(1, 2, 3, 4, 5)
print(result)
// returns 3.0, which is the arithmetic mean of these five numbers
result = arithmeticMean(3, 8.25, 18.75)
print(result)
// returns 10.0, which is the arithmetic mean of these three numbers

Warning

Variadic parameter actually creates a constant as a Constant array.

in out parameter

Function parameters are fixed by default.

Trying to change the parameter of a function inside the function body will result in a compile-time error.

If we want to write a function and want to change the value of the parameter of that function and we want this change to remain permanently even after the function is completed, we need to use an in-out parameter.

example

When calling the function, the argument is passed to the name of the variable with the & sign. This means that the variable can be changed by the function.

In-out parameters cannot have a default value. Variadic parameters cannot be marked as inout.

/* 
In the example below, the value of someInt 
is assigned to anotherInt and the value 
of anotherInt to someInt.
*/
// Note that these two types have variables.
func swapTwoInts(_ a: inout Int, _ b: inout Int) {
    let temporaryA = a
    a = b
    b = temporaryA
}

var someInt = 3
var anotherInt = 107

// Note that the corresponding arguments 
//are passed ampersant while calling the function.
swapTwoInts(&someInt, &anotherInt)


print("someInt is now \(someInt), and anotherInt is now \(anotherInt)")
// Prints "someInt is now 107, and anotherInt is now 3"

Congratulations

Congratulations 🎉

In this lesson, we have learned parameter features on Swift.

See you in the next lesson.

Parameters — illustration 6

Frequently asked questions

Is the “Parameters” lesson free?

Yes — the full text of “Parameters” is free to read here on the web, and the Swift Academy course includes 1 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.

What will I learn in “Parameters”?

Parameters You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 1, so you can start here or from the beginning and move at your own pace.

How long does the “Parameters” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Swift Academy lesson?

Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

← Back to Swift Academy