You are on page 1of 7

01

FUNCTIONS
Ruwaya saif
12.51CAI
02

Page 1
• I made a function called introduceMyself that prints "My name is
Ruwaya and I'm a student” the called the function to display the
message.

• I made a function called magicEightBall and inside it I declared a


constant called randomNum = arc4random_uniform(UInt32(5)).
Then, i used a switch statement so if the randomNum was 0,1,2,3 a
message will be displayed , a different message will be displayed
for each number. Then I called the function a couple of times to see
the output.
03

Page 2
• A assigned a variable called steps to 0, then made a function
called incrementSteps and in it I added 1 to steps then printed
it. I called the function a couple of times to see the output.

• I made a constant called goal and made it = to 10000. then, I


made a function called progressUpdateand inside it I used an if
statement so if steps was less than 10% of goal then "You're off
to a good start.” will be displayed. Else if steps is less than half of
goal then "You're almost halfway there!” will be printed. Else if
steps is less than 90% of goal then "You're over halfway there!"
will be printed. Else if steps is less than goal then "You're almost
there!" will be printed. Else "You beat your goal!" will be printed.
Then I called the function to see the output.
04
PAGE 3
• I made a function called introduction with two string parameter,
name and home, and one integer parameter, age. Then inside
the function("\(name), \(age), is from \(home)") will be printed.
Then I called the function twice and put a name, home and an
age for each one.

• I made a function called almostAddition with two integer


parameters, first and second. I assigned a result = first + second
– 2 then used print result so when the function is called the
result will be printed. Then I called the function to see the
output.

• I made a function called multiply with 2 integers parameter, first


and second. Inside the function I put a used print first multiplied
by second, so when the function is called the multiplication will
be printed. Then I called the function and gave first the value of
2 and I gave second the value of 2.
PAGE 4
• I made a function called progressUpdate with 2 integer
parameters, steps and goal. Inside the function I wrote an if-
else-if statement , so if steps was less than 10% of goal then
"You're off to a good start.” will be displayed. Else if steps is less
than half of goal then "You're almost halfway there!” will be
printed. Else if steps is less than 90% of goal then "You're over
halfway there!" will be printed. Else if steps is less than goal then
"You're almost there!" will be printed. Else "You beat your goal!"
will be printed. I called the function 3 times with different
number for goal and steps each time.

• I made a function called pacing that had 4 double parameters,


currentDistance, totalDistance, currentTime and goalTime.
Then, I assigned a constant called currentPace to currentTime
divided by (currentDistance divided by totalDistance). Then I
used an if statement where if currentPace was less than
goalTime then print "Keep it up!” else "You've got to push it just
a bit harder!” will be printed. Then, I called the function and
gave the parameters that I made numbers.and no one knows
you.
08
PAGE 5
• I made a function called greeting with a string parameter, name.
then inside I returned the message "Hi, \(name)! How are you?”.
Then I called the function and gave it the name: Ruwaya.

• I made a function called multiplyArg with 2 integer parameters,


num and num2 the inside a put a return num times num2 plus
2.then I called the function and gave num and num2 the value
of 2.
PAGE 6
• I made a function called calculatePace with three dluble
parameters, currentDistance, totalDistance, currentTime. In the
function I returned currentDistance divided by (totalDistance
divided by currentTime). I printed the function and gave it thse
values (currentDistance: 30.0, totalDistance: 100.0,currentTime:
25.0).

• I made a function called pacing with 4 parameters,


currentDistance, totalDistance,currentTime, goalTime. Then, I
assigned a constant called currentPace to
calculatePace(currentDistance: currentDistance,
totalDistance:totalDistance, currentTime: currentTime) then I
used an if statement so if currentPace was less than or equal to
goalTime then “Keep it up! Will be returned. Else "You've got to
push it just a bit harder!” will be printed. I called the function
and gave the parameters random values.

You might also like