KinderSwift 7: Introduction to Functions

This session we will learn about functions by making colors with UIColor.

Transcript

Welcome to lesson 7 of KinderSwift.

In our last lesson we used this

Double (2) 

to convert a Int into a Double and this

Int (2.5) 

to convert a Double into a Int. Both of these are functions.

Functions take the data within the parentheses and change it into something else.

Most of Swift is made up of using or creating functions. There are math functions such as this one:

sqrt(100.0)

Which calculates the square root of 100 or this that calculates ten to the power of 3

pow(10.0,3.0)

Here we have more than two numbers in the parentheses separated by commas. Each of these is a parameter. A function may have any number of parameters.

Functions are not just for numbers. For example this:

UIColor(white: 0.5, alpha: 1.0)

Is a function with two parameters. Often you will see identifiers for the parameters, in this case white and alpha. This function makes different colors between black and white.

Here is a function with four parameters.

UIColor(red: 1.0, green: 0.8, blue: 0.2, alpha: 1.0)

This function has a range. The parameters can be from 0 to 1.0. We will need to find documentation to tell us this though. Often you can get to the documentation by clicking the mouse button while hitting option while on a keyword. You can get the most information by clicking the Class reference link.

frame07

Functions may have other expressions, even functions or variables inside them. Often colors get described as values of Red green and blue in a range from 0 to 255. We might have a color described by. (255, 90, 00). We need to divide by 255 to get a range of 0 to 1. We can write that like this:

UIColor(red: 255.0/255.0, green: 90.0/255.0, blue: 0, alpha: 1.0)

Parameters do have type. We can guess that the UIColor function has the type Double,but we would be wrong. It has a new type for us CGFloat. CGFloat is a floating point number like Double, but used for computer graphics.

Let’s try a variable. Type this:

var myRed = 255.0/255.0
UIColor(red: myRed, green: 0.8, blue: 0.0, alpha: 1.0)

you get an error.

myRed is the wrong type. It is a Double not a CGFloat. We have to declare it CGFloat to work here. Compilers like Swift are very fussy about this. CGFloat and Double might look the same to us, but not to Swift.

Add a type to myRed,

var myRed:CGFloat = 255.0/255.0

and everything is okay.

How can we tell what type to use? One way is we watch autocomplete type the statement for us: Try it, Type this

UIColor(

We get this:

frame08

You’ll see that the parameters give their type, just like we assigned types with var and let. UIColor uses CGFloats.

You will also notice something else. there are a lot of functions named UIColor, but with very different parameters. Most of these you will never use, but there a few you might. We’ll discuss which ones in a few lessons when we start making user interfaces.

There are so many functions for UIColor because is UIColor is a type. You might have noticed we broke the rule about a lowercase letter for an identifier in a lot of these functions such as Int() or Double(), though sqrt() and pow() are both lowercase.

Ones with capital letters are all special functions called Initializers. They set the initial value of a type. Usually we assign them like this:

var myColor = UIColor(red: 1.0, green: 0.8, blue: 0.2, alpha: 1.0)

We can then use myColor later in our program. Like Int or Double sometime we use them once to cast a type without assigning them.

For example I can cast myBlue which is a Double into a CGFloat if I need to in my UIColor function like this:

var myRed:CGFloat = 255.0/255.0
var myBlue = 200.0/255.0
UIColor(red: myRed, green: 0.8, blue: CGFloat(myBlue), alpha: 1.0)

Another name for a type is a Class. While there really is no difference between a class and a Type in Swift, the general rule is classes are more complex types. UIColor is a class which has its own functions and variables. Classes will be our focus for many of our upcoming lessons, but to get your feet wet, I’ll introduce another type of function: A class method.

UIColor has a lot of class methods, many without parameters. Instead it has a blank parentheses pair. You use class methods by stating its type then a dot, then the function. for example there is this:

UIColor.blackColor()

and this

UIColor.greenColor()

There are many more of these. Before the next lesson play around with some of the UIColor Class methods and the Red:green:blue:alpha: initializer. You can scroll for them in autocomplete or you can use this list:

The Apple System Colors with a color wheel of RGB primary and secondaries.
The Apple System Colors with a color wheel of RGB primary and secondaries.

Next time we’ll make our own functions.

One thought on “KinderSwift 7: Introduction to Functions”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s