Init() Day 4
-
Upload
ucla-association-of-computing-machinery -
Category
Education
-
view
176 -
download
1
description
Transcript of Init() Day 4
ACM init()Day 4: November 12, 2014
Any questions?• While Loops (Be careful about infinite loops!)
• Until Loops
• For Loops
• Next
• Arrays
• Simple Sorting
Fizzbuzz Solution
Improved Calculator
Let's go to koding.com to see the solution.
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?
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:
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
Method example
Let's go to koding and run it!
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
Method examples
These are both valid methods.
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?
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!
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
Square example
We want to compute the area of a square with side length n:
Now how would we use this?
Using the square method
Try running this yourself!
More method examplesWe can pass in strings too! Take a look:
In fact, we can pass in any type of variable to a method.
Extra inputWe calculated the area of a square earlier. What if we want to
do a rectangle? We can pass in two arguments!
Try this now!
Write a method called add that can add any two numbers together. Then try the method with some inputs!
Answer
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.
ReturnTo return a variable use the following syntax:
def method_name(arg) #do something to arg return arg end
Return example
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.
Answer
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.)
Answer
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 }
Block exampleHere is a block named block that will print "I'm a block!"
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 }
Block example
What will this print out?
Try it!
Blocks can take in arguments too
What will this print?
Answer
You are in the block 5 You are in the method test You are in the block 100
What we did today• More about arrays
• Method
• Parameters
• Return Values
• Blocks
• How to use a block
• Parameters
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.