Thursday, May 8, 2008

Subroutines

Subroutines and functions allow us to package similar instructions together. We've seen built-in subroutines before like:

Math.Sqrt()
Int()
Math.Round()
FormatNumber()
FormatCurrency()
FormatPercent()
str.IndexOf()
str.Length()
str.Substring()
...etc...


Each one of these subroutines/functions does something useful. We can use them to do a certain job, whether it is changing a number value in some way, formatting output or figuring out certain characteristics of strings.

We also learned that we can make our own functions and subroutines to do things that we decide. So let's say that we want to write a print subroutine that will just print out our report, and a calculate() function that will take in three of the expenses and add them together and return the answer. Take a look at the code:
Click for larger image.



You just have to make sure to write the function and subroutine code outside of the button1_click() subroutine and inside the Class Form1 block.

No comments: