This site is retired and is no longer updated since November 2021. Please visit our new website at https://www.teamscode.org for up-to-date information!
Calling Functions in Swift

Last lesson, we looked at the concept of functions. Functions are useful only when you know how to use them. To use a function, you call it. Initializers are a type of function. Below is the Car class(just for reference):

   class Car{
   //All of the variables below are the properties.
       var nameOfCar:String //Name of the car
       var color:String //Color of the car
       var fuelCapacity:Double // Fuel capacity
       var currentSpeed:Double
       var maxSpeed:Double // Max speed
       var allWheelDrive:Bool // Is it an all wheel drive?
       init(curSpeed:Double, speed:Double, name:String, fuelCap:Double, AWD:Bool){
           currentSpeed=curSpeed
           maxSpeed=speed
           nameOfCar=name
           fuelCapacity=fuelCap
           allWheelDrive=AWD
       }
       func drive(speed:Double, fuel:Int)->Double{
           var fuelRemaining=fuelCapacity - fuel
           currentSpeed=speed
           return fuelRemaining
       }
   }

Initializers

Initializers are functions that return an object of a class. The function init(curSpeed:Double, speed:Double, name:String, fuelCap:Double, AWD:Bool) in the Car class. Before learning how to call an initializer, let’s learn to write one.

First off, initializers are just functions that return objects of classes and to write one, you need only the init keyword. You do not need a return type, a return statement or a func keyword because the init keyword tells the compiler that you are about to write an initializer and it knows that initializers return objects of the Car type (in this case), and that they are functions. Here is the header(first line) of an initializer:

   init(curSpeed:Double, speed:Double, name:String, fuelCap:Double, AWD:Bool)

You might have noticed that it has parameters(inputs). The main use of these parameters is to get the specific values for the Car object you want to create. Because only if you create an object, you can drive it and use it in many ways. Just having the blueprint for a Car is not enough if you want to drive it. Inside the scope of the initializer(Scope=the code between the curly braces of an initializer), the inputs are assigned to each property of the Class. For example, the input called name gets the name of the car and in the scope of the initializer, we assign it to the property of the Car class called nameOfCar. Assigning inputs to their respective property is not the only thing you can do in an initializer. You can also get many other totally unrelated inputs from the programmer who is creating the object and not even use those inputs. Or you can use those inputs to calculate some value you are going to use in the future. What I mean is assigning the value of the parameters to the value of properties is not the only thing you can do with initializers. You can use them just like you use any other method. Now, let’s move on to how to call an initializar. Most of the time, when you are calling an initializer, you will want to assign the object it returns, to a variable. The below code is what you write if you want to create a variable named prius of the Car type(Classes give the blueprint for objects that can be created from them and when the objects are created, they are of the class’s type) that will hold an object of the Car class, this is how you do it:

   var prius:Car=Car(curSpeed:23.99, speed:12.334, name:"PRIUS", fuelCap:30.50, AWD:true)

The part of that code which says Car(curSpeed:23.99, speed:12.334, name:"PRIUS", fuelCap:30.50, AWD:true) is how you call the initializer. You just replace the init keyword in the initializer with the name of the class and fill up the values for the parameters while calling the initializer.

Calling Functions outside the class

Calling a function from within the class in which it is defined is different from calling it in code that is written outside the curly braces of the class. Let’s use the code we see in the Car class.The drive function, belongs to the Car class. Assume we are writing code outside of the Car class. To call the drive function in this situation, we need an object. Let’s create the object.

   var hondaAccord:Car=Car(curSpeed:23.99, speed:12.334, name:"HONDA ACCORD", fuelCap:30.50, AWD:true)

Now, let’s drive the Honda Accord we have created.

   hondaAccord.drive(speed:90.322, fuel:0.9)

drive is a function that “drives” a Car object. To call the function, you first type the name of the object you want to drive. In this case the name is hondaAccord. Then type a dot or a full-stop or whatever you call “.”. Then type the name of the function you want the object to call. In this case it is drive. After that, in parantheses(just like it is defined in the Car class), type the names and the values of the parameters. The order matters. So, to know the right order, look at how you wrote it in the function definition of the drive method in the Car class. That’s all!

Calling Functions inside the class Calling a function from within the class in which it is defined is easier than calling it from outside. To call it within the class, you don’t need an object. This is because the class is just a blueprint. And you don’t need a Car to write instruction about how a Car drives. Let’s see this in the form of code. Let’s say we want a function that makes the car drive like crazy. Using the drive function will make it easier to write how to drive crazy because driving like crazy, is a kind of driving(Driving like crazy definition=Driving with a speed of 100000 miles/s and using a fuel amount of 1500 gallons). This is what we do to define in the blueprint of a car how to drive like a crazy person(Please note that this code needs to be in the scope of the Car class):

       func crazyDrive()->Double{
           return drive(speed:100000, fuel:1500)
       }

We call the drive method just like we called it outside the class in the previous topic but without the the name of the object and the “.”. The return statement does something very interesting here. When you do something like that in Swift, this is what happens: 1)The drive function is called. 2)The Double value returned by the drive function is passed on to the crazyDrive function and it returns the Double value.

So, now we have most of the basics of object oriented programming. In the next lesson we will be learning about COLLECTION TYPES!!!

Written by Valliappan Valliappan

Notice any mistakes? Please email us at learn@teamscode.com so that we can fix any inaccuracies.