Learning to program (v) – adding a subroutine

Our program as it stands works quite well.

print("How many trees? ")
n = parse(chomp(readline()))

for i=1:n
   
   println("Enter the circumference of the tree (inches): ")
   c = parse(chomp(readline()))

   println("Tree Species: ")
   println("(1) Silver Maple")
   println("(2) White Birch")
   println("(3) Black Walnut")
   println("(4) Red Oak")
   println("(5) White Oak")
   println("(6) American Elm")
   print("Choose a tree: ")
   t = parse(chomp(readline()))

   if (t == 1)
      g = 3.0
   elseif (t == 2)
      g = 5.0
   elseif (t == 3)
      g = 4.5
   elseif (t == 4)
      g = 4.0
   elseif (t == 5)
      g = 5.0
   elseif (t == 6)
      g = 4.0
   end

   d = c / pi
   age = d * g
   println("The tree is ", round(age,2), " years old")

end

The most unreadable part is still the three lines that read in info: parse(chomp(readline())). As this is used three times, it might be nice if we put it in its own “subroutine”, which is a way of creating a piece of a program which can be used anywhere. These are more often called functions. By doing this we are able to “hide” code. Here is what a simple function would look like in Julia:

function readNum()
   c = parse(chomp(readline()))
   return c
end

It uses the pair function-end, to define the function. It is given a name, in this case readNum, and because it doesn’t take any inputs there is nothing in between the parentheses. The line of code that reads the number from the user is placed inside the function, and passed back to the program using the function using the return statement.

function readNum()
   c = parse(chomp(readline()))
   return c
end

print("How many trees? ")
n = readNum()

for i=1:n

   print("Enter the circumference of the tree (inches): ")
   c = readNum()

   println("Tree Species: ")
   println("(1) Silver Maple")
   println("(2) White Birch")
   println("(3) Black Walnut")
   println("(4) Red Oak")
   println("(5) White Oak")
   println("(6) American Elm")
   print("Choose a tree (?): ")
   t = readNum()

   if (t == 1)
      g = 3.0
   elseif (t == 2)
      g = 5.0
   elseif (t == 3)
      g = 4.5
   elseif (t == 4)
      g = 4.0
   elseif (t == 5)
      g = 5.0
   elseif (t == 6)
      g = 4.0
   end

   d = c / pi
   age = d * g
   println("The tree is ", round(age,2), " years old")

end

This is a very simple example of a function – some will argue there was little point, as one line of code in each instance was replaced with “one line of code”. But were there more lines repeated, then it could be quite beneficial.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s