init() day 4
DESCRIPTION
ACM Init() Day 4TRANSCRIPT
![Page 1: Init() Day 4](https://reader034.vdocuments.mx/reader034/viewer/2022042512/559427641a28abfb408b456b/html5/thumbnails/1.jpg)
ACM init()Day 4: November 12, 2014
![Page 2: Init() Day 4](https://reader034.vdocuments.mx/reader034/viewer/2022042512/559427641a28abfb408b456b/html5/thumbnails/2.jpg)
Any questions?• While Loops (Be careful about infinite loops!)
• Until Loops
• For Loops
• Next
• Arrays
• Simple Sorting
![Page 3: Init() Day 4](https://reader034.vdocuments.mx/reader034/viewer/2022042512/559427641a28abfb408b456b/html5/thumbnails/3.jpg)
Fizzbuzz Solution
![Page 4: Init() Day 4](https://reader034.vdocuments.mx/reader034/viewer/2022042512/559427641a28abfb408b456b/html5/thumbnails/4.jpg)
Improved Calculator
Let's go to koding.com to see the solution.
![Page 5: Init() Day 4](https://reader034.vdocuments.mx/reader034/viewer/2022042512/559427641a28abfb408b456b/html5/thumbnails/5.jpg)
![Page 6: Init() Day 4](https://reader034.vdocuments.mx/reader034/viewer/2022042512/559427641a28abfb408b456b/html5/thumbnails/6.jpg)
More about arraysRemember that an array is just a list of items. What if we want to iterate over each element of the array? Here's how to do it:
What will this do?
![Page 7: Init() Day 4](https://reader034.vdocuments.mx/reader034/viewer/2022042512/559427641a28abfb408b456b/html5/thumbnails/7.jpg)
How to iterate over an array
To go over every element of an array all you have to do is define an array, then use the following syntax:
![Page 8: Init() Day 4](https://reader034.vdocuments.mx/reader034/viewer/2022042512/559427641a28abfb408b456b/html5/thumbnails/8.jpg)
Methods• A method is a reusable section of code written to
perform a specific task in a program.
• Why use methods?
• If there is a mistake in your program it is much easier to debug if everything is separated out
• If you need to do the same thing over and over again a method can reduce repetition
![Page 9: Init() Day 4](https://reader034.vdocuments.mx/reader034/viewer/2022042512/559427641a28abfb408b456b/html5/thumbnails/9.jpg)
Method example
Let's go to koding and run it!
![Page 10: Init() Day 4](https://reader034.vdocuments.mx/reader034/viewer/2022042512/559427641a28abfb408b456b/html5/thumbnails/10.jpg)
How do we write a method?
A method has three parts: a header, a body, and an end. It looks like this:
def method_name the code you want to run end
![Page 11: Init() Day 4](https://reader034.vdocuments.mx/reader034/viewer/2022042512/559427641a28abfb408b456b/html5/thumbnails/11.jpg)
Method examples
These are both valid methods.
![Page 12: Init() Day 4](https://reader034.vdocuments.mx/reader034/viewer/2022042512/559427641a28abfb408b456b/html5/thumbnails/12.jpg)
How do we use a method?Calling a method is easy! All you have to do is type the
method's name. For example, if we wanted a method that tells us "Hi," we would do this:
Go to koding and try it! What happens if you type "says_hi" more than once?
![Page 13: Init() Day 4](https://reader034.vdocuments.mx/reader034/viewer/2022042512/559427641a28abfb408b456b/html5/thumbnails/13.jpg)
Methods with inputs
What if we want a method that takes in an argument? Let's say we want to compute the area of a square. Is there a way to
pass in the side length of the square to the method? !
Of course there is!
![Page 14: Init() Day 4](https://reader034.vdocuments.mx/reader034/viewer/2022042512/559427641a28abfb408b456b/html5/thumbnails/14.jpg)
Methods with inputsTo write a method that uses input use the following syntax:
def method_name(arg) the code you want to run with the arg end
![Page 15: Init() Day 4](https://reader034.vdocuments.mx/reader034/viewer/2022042512/559427641a28abfb408b456b/html5/thumbnails/15.jpg)
Square example
We want to compute the area of a square with side length n:
Now how would we use this?
![Page 16: Init() Day 4](https://reader034.vdocuments.mx/reader034/viewer/2022042512/559427641a28abfb408b456b/html5/thumbnails/16.jpg)
Using the square method
Try running this yourself!
![Page 17: Init() Day 4](https://reader034.vdocuments.mx/reader034/viewer/2022042512/559427641a28abfb408b456b/html5/thumbnails/17.jpg)
More method examplesWe can pass in strings too! Take a look:
In fact, we can pass in any type of variable to a method.
![Page 18: Init() Day 4](https://reader034.vdocuments.mx/reader034/viewer/2022042512/559427641a28abfb408b456b/html5/thumbnails/18.jpg)
Extra inputWe calculated the area of a square earlier. What if we want to
do a rectangle? We can pass in two arguments!
![Page 19: Init() Day 4](https://reader034.vdocuments.mx/reader034/viewer/2022042512/559427641a28abfb408b456b/html5/thumbnails/19.jpg)
Try this now!
Write a method called add that can add any two numbers together. Then try the method with some inputs!
![Page 20: Init() Day 4](https://reader034.vdocuments.mx/reader034/viewer/2022042512/559427641a28abfb408b456b/html5/thumbnails/20.jpg)
Answer
![Page 21: Init() Day 4](https://reader034.vdocuments.mx/reader034/viewer/2022042512/559427641a28abfb408b456b/html5/thumbnails/21.jpg)
Return
What if we don't want to print something out in a function? What if we just want to do something to a variable then keep
on using it? !
We can have our methods return the variable back to where it was called from. The variable can then be used again, but it
will have been modified by the method.
![Page 22: Init() Day 4](https://reader034.vdocuments.mx/reader034/viewer/2022042512/559427641a28abfb408b456b/html5/thumbnails/22.jpg)
ReturnTo return a variable use the following syntax:
def method_name(arg) #do something to arg return arg end
![Page 23: Init() Day 4](https://reader034.vdocuments.mx/reader034/viewer/2022042512/559427641a28abfb408b456b/html5/thumbnails/23.jpg)
Return example
![Page 24: Init() Day 4](https://reader034.vdocuments.mx/reader034/viewer/2022042512/559427641a28abfb408b456b/html5/thumbnails/24.jpg)
Try this now!
Write a by_three? method that takes a single integer parameter, number, and returns true if that number is evenly
divisible by three and false if not.
![Page 25: Init() Day 4](https://reader034.vdocuments.mx/reader034/viewer/2022042512/559427641a28abfb408b456b/html5/thumbnails/25.jpg)
Answer
![Page 26: Init() Day 4](https://reader034.vdocuments.mx/reader034/viewer/2022042512/559427641a28abfb408b456b/html5/thumbnails/26.jpg)
Try this now!
A greeter method that takes a single string parameter, name, and returns a string greeting that person. (Make sure to use
return and don't use print or puts.)
![Page 27: Init() Day 4](https://reader034.vdocuments.mx/reader034/viewer/2022042512/559427641a28abfb408b456b/html5/thumbnails/27.jpg)
Answer
![Page 28: Init() Day 4](https://reader034.vdocuments.mx/reader034/viewer/2022042512/559427641a28abfb408b456b/html5/thumbnails/28.jpg)
BlocksA block is very similar to a method. However, a block is alway
called from a method with the same name as the block. Here's what it looks like:
block_name { code you want to do }
![Page 29: Init() Day 4](https://reader034.vdocuments.mx/reader034/viewer/2022042512/559427641a28abfb408b456b/html5/thumbnails/29.jpg)
Block exampleHere is a block named block that will print "I'm a block!"
![Page 30: Init() Day 4](https://reader034.vdocuments.mx/reader034/viewer/2022042512/559427641a28abfb408b456b/html5/thumbnails/30.jpg)
How do we use a block?
A block must be called from a method with the same name. The block is called using the 'yield' keyword.
def name yield end !
name { code you want to do }
![Page 31: Init() Day 4](https://reader034.vdocuments.mx/reader034/viewer/2022042512/559427641a28abfb408b456b/html5/thumbnails/31.jpg)
Block example
![Page 32: Init() Day 4](https://reader034.vdocuments.mx/reader034/viewer/2022042512/559427641a28abfb408b456b/html5/thumbnails/32.jpg)
What will this print out?
Try it!
![Page 33: Init() Day 4](https://reader034.vdocuments.mx/reader034/viewer/2022042512/559427641a28abfb408b456b/html5/thumbnails/33.jpg)
Blocks can take in arguments too
What will this print?
![Page 34: Init() Day 4](https://reader034.vdocuments.mx/reader034/viewer/2022042512/559427641a28abfb408b456b/html5/thumbnails/34.jpg)
Answer
You are in the block 5 You are in the method test You are in the block 100
![Page 35: Init() Day 4](https://reader034.vdocuments.mx/reader034/viewer/2022042512/559427641a28abfb408b456b/html5/thumbnails/35.jpg)
What we did today• More about arrays
• Method
• Parameters
• Return Values
• Blocks
• How to use a block
• Parameters
![Page 36: Init() Day 4](https://reader034.vdocuments.mx/reader034/viewer/2022042512/559427641a28abfb408b456b/html5/thumbnails/36.jpg)
HomeworkWrite a program that can compute and print the area of different shapes. !Rules: !The program should be able to work by typing in area("shape_name", length1, length2) !There should be a different method for each shape. !At minimum, the program must compute the area of a square, a rectangle, and a triangle. Add other shapes if you can.