Init() Day 4

36
ACM init() Day 4: November 12, 2014

description

ACM Init() Day 4

Transcript of Init() Day 4

Page 1: Init() Day 4

ACM init()Day 4: November 12, 2014

Page 2: Init() Day 4

Any questions?• While Loops (Be careful about infinite loops!)

• Until Loops

• For Loops

• Next

• Arrays

• Simple Sorting

Page 3: Init() Day 4

Fizzbuzz Solution

Page 4: Init() Day 4

Improved Calculator

Let's go to koding.com to see the solution.

Page 5: Init() Day 4
Page 6: Init() Day 4

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

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

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

Method example

Let's go to koding and run it!

Page 10: Init() Day 4

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

Method examples

These are both valid methods.

Page 12: Init() Day 4

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

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

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

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

Using the square method

Try running this yourself!

Page 17: Init() Day 4

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

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

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

Answer

Page 21: Init() Day 4

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

ReturnTo return a variable use the following syntax:

def method_name(arg) #do something to arg return arg end

Page 23: Init() Day 4

Return example

Page 24: Init() Day 4

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

Answer

Page 26: Init() Day 4

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

Answer

Page 28: Init() Day 4

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

Block exampleHere is a block named block that will print "I'm a block!"

Page 30: Init() Day 4

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

Block example

Page 32: Init() Day 4

What will this print out?

Try it!

Page 33: Init() Day 4

Blocks can take in arguments too

What will this print?

Page 34: Init() Day 4

Answer

You are in the block 5 You are in the method test You are in the block 100

Page 35: Init() Day 4

What we did today• More about arrays

• Method

• Parameters

• Return Values

• Blocks

• How to use a block

• Parameters

Page 36: Init() Day 4

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.