Transcript
In our last lesson we learned about functions. Swift, and most modern programming languages are nothing more than writing groups of your own functions. In this lesson, we will write our own functions.
In our last lesson you may remember this function
UIColor(white: alpha:)
Which made varying colors of gray. Let’s start by making our own custom function like it, but for various shades of red. Make a new playground, and in the playground type this:
func myColorRed(myRed:CGFloat) -> UIColor{ return UIColor(red:myRed, green: 0.0, blue: 0.0, alpha: 1.0) }
Let’s explain the parts of a function. Like we started variables with var
or constants with let
, we start functions with func
. This tells Swift we are writing a function. We next give an identifier for the function, which will be its name. Generally, the identifier starts with a lower case letter.
Immediately after the identifier we have a parameter list surrounded by parentheses. If we have more than one parameter, we separate them with commas. Note each parameter is like an explicit assignment with var.
var myRed:CGFloat = 0
Instead of assigning any value though, the function will get a value when we use the function and place a value int he parameter.
We then write an arrow made from a dash and greater than symbol, followed by the type. This tells us the type of the value returned from the function. This function returns a value of type UIColor
.
We then have curly braces surrounding some code. This is known as a code block. You will find that we use the curly braces a lot to make code blocks in Swift.
Inside the code block, usually the last line of the code, you will find return and a value. In this case we use the UIColor
initializer to return a red color.
Let’s try this. Type these in
myColorRed(1.0) myColorRed(0.5) myColorRed(0.86) myColorRed(0.92) myColorRed(0.66)
And we get different values of red.
Let’s try a more complicated example. Type this:
func pizzaArea(z:Double) -> Double { // area of a pizza: pi * z * z = a let pi = 3.14159265358 return pi * z * z }
This finds the area of a circular pizza using z
as a radius of circle. z
is a Double
and we return a Double
. We set a constant of pi
, and then return pi
times z
squared.
Try this function out.
pizzaArea(3.0) pizzaArea(6.0) pizzaArea(12.5)
Most of the time we do not measure a pizza’s radius but it’s diameter. We say 14 inch diameter or 35cm diameter, not a 7 inch or 17.5 cm radius.
Some functions do not return a value, they do something else, such as print the results. There is a special function to print out results to the console, called println
. You place a value or values separated by commas in the parameter and print line will print them to the console.
Let’s combine both the diameter problem and an example of a println
function into one function. Add this to the playground
func printPizzaArea(diameter:Double){ let radius:Double = diameter / 2.0 let area = pizzaArea(radius) println (radius,area) }
You will notice the arrow is missing. If we do not return a value, we do not add it to the function declaration. We do have a parameter diameter of Double.
In the code block, we find the radius by dividing the diameter by two. We find the area by using our already defined area function. Our third line prints the radius and area to the console.
For a fourteen inch pizza we can type this:
printPizzaArea(14.0)
and it works, but we see nothing. You need to open the console click here to open it
and you will see a new window on the playground with our answer.
Now try a pizza at 35 cm
printPizzaArea(35.0)
We get an answer. Our answer does not look very user-friendly though. In our next lesson we’ll look at Strings and formatting.
Challenge Work
If you’d like an assignment until next time here’s two things to try:
Make a blueColor
function which returns a blue color as a UIColor
from an Int
value from 1 to 255.
Make a function to calculate the volume of a deep dish pizza and print the radius, area and height to the console. . Use a diameter and height parameter and the area function we already defined. A hint is the volume is the height time the area.
I’ll post the answers in another video
Leave a Reply