473,406 Members | 2,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Function procedures

I'm creating an application where a user creates a cable bill. The application calculates the bill based on whether the customer is a residential or business customer. I've finished the interface and all of the calculations. However, the assignment is to use one or more sub or function procedures. I have several sub procedures but I don't have any function procedures and I'm not sure how to include one in my program without messing up what I've created. Any advice would be greatly appreciated. Thanks! Here's my code:

'Project Name: Tammy Seilheimer
'Project Purpose: This project calculates a customer's bill for cable services
'Created By: Tammy Seilheimer

Expand|Select|Wrap|Line Numbers
  1. Option Explicit On
  2.  
  3. Public Class Form1
  4.  
  5.     Private Sub residentialButton_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles residentialbutton.CheckedChanged
  6.         connectionstextbox.Visible = False
  7.         connectionsquantitytextbox.Visible = False
  8.         TextBox4.Visible = False
  9.         Label7.Visible = False
  10.         Label8.Visible = False
  11.  
  12.     End Sub
  13.  
  14.     Private Sub businessbutton_checkedchanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles businessbutton.CheckedChanged
  15.         connectionstextbox.Visible = True
  16.         connectionsquantitytextbox.Visible = True
  17.         TextBox4.Visible = True
  18.         Label7.Visible = True
  19.         Label8.Visible = True
  20.     End Sub
  21.  
  22.  
  23.     Private Sub calculatebutton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calculatebutton.Click
  24.  
  25.         If residentialbutton.Checked Then
  26.             Dim channelprice As Decimal
  27.             Dim residentialnumberofchannels As Integer
  28.             Dim residentialchanneltotalprice As Decimal
  29.             Dim residentialamountdue As Decimal
  30.             Dim processingfee As Decimal
  31.             Dim basicfee As Decimal
  32.             Dim isconverted As Boolean
  33.             Dim isconverted2 As Boolean
  34.             Dim isconverted3 As Boolean
  35.             Dim isconverted4 As Boolean
  36.             isconverted = Integer.TryParse(channelpricetextbox.Text, channelprice)
  37.             isconverted2 = Integer.TryParse(processingtextbox.Text, processingfee)
  38.             isconverted3 = Integer.TryParse(basicfeetextbox.Text, basicfee)
  39.             isconverted4 = Integer.TryParse(premiumquantitytextbox.Text, residentialnumberofchannels)
  40.             'connectionstextbox.Visible = False
  41.             'connectionsquantitytextbox.Visible = False
  42.             ' TextBox4.Visible = False
  43.             'Label7.Visible = False
  44.             'Label8.Visible = False
  45.  
  46.  
  47.             If isconverted4 = True Then
  48.                 processingfee = 4.5
  49.                 basicfee = 30
  50.                 channelprice = 5
  51.                 premiumquantitytextbox.Text = residentialnumberofchannels
  52.                 channelpricetextbox.Text = channelprice.ToString("C2")
  53.                 residentialchanneltotalprice = (residentialnumberofchannels * channelprice)
  54.                 channeltextbox.Text = residentialchanneltotalprice.ToString("C2")
  55.                 processingtextbox.Text = processingfee.ToString("C2")
  56.                 basicfeetextbox.Text = basicfee.ToString("C2")
  57.                 residentialamountdue = (processingfee + basicfee + residentialchanneltotalprice)
  58.                 amountduetextbox.Text = residentialamountdue.ToString("C2")
  59.  
  60.             End If
  61.         End If
  62.  
  63.         If businessbutton.Checked Then
  64.  
  65.             Dim businessnumberofchannels As Integer
  66.             Dim businessnumberofconnections As Integer
  67.             Dim businesschanneltotalprice As Decimal
  68.             Dim businessconnectionstotalprice As Decimal
  69.             Dim businessamountdue As Decimal
  70.             Dim channelprice As Decimal
  71.             Dim isconverted5 As Boolean
  72.             Dim isconverted6 As Boolean
  73.             Dim processingfee As Decimal
  74.             Dim basicfee As Decimal
  75.             Dim connectionprice As Decimal
  76.             isconverted5 = Integer.TryParse(connectionsquantitytextbox.Text, businessnumberofconnections)
  77.             isconverted6 = Integer.TryParse(premiumquantitytextbox.Text, businessnumberofchannels)
  78.  
  79.             If isconverted5 = True Then
  80.                 processingfee = 16.5
  81.                 basicfee = 80
  82.                 channelprice = 50
  83.                 connectionprice = 4
  84.                 businessconnectionstotalprice = businessnumberofconnections * 4
  85.                 businessnumberofconnections = connectionsquantitytextbox.Text
  86.                 channelpricetextbox.Text = channelprice.ToString("C2")
  87.                 connectionstextbox.Text = businessconnectionstotalprice.ToString("C2")
  88.                 TextBox4.Text = connectionprice.ToString("C2")
  89.                 processingtextbox.Text = processingfee.ToString("C2")
  90.                 basicfeetextbox.Text = basicfee.ToString("C2")
  91.                 businesschanneltotalprice = businessnumberofchannels * 50
  92.                 channeltextbox.Text = businesschanneltotalprice.ToString("C2")
  93.                 businessnumberofchannels = premiumquantitytextbox.Text
  94.                 businessamountdue = (businesschanneltotalprice + processingfee + basicfee + businessconnectionstotalprice)
  95.                 amountduetextbox.Text = businessamountdue.ToString("C2")
  96.  
  97.             End If
  98.         End If
  99.  
  100.  
  101.     End Sub
  102.  
  103.  
  104.     Private Sub exitbutton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Exitbutton.Click
  105.         Me.Close()
  106.     End Sub
  107.  
  108.     Private Sub clearbutton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clearbutton.Click
  109.         processingtextbox.Text = String.Empty
  110.         basicfeetextbox.Text = String.Empty
  111.         connectionsquantitytextbox.Text = String.Empty
  112.         connectionstextbox.Text = String.Empty
  113.         premiumquantitytextbox.Text = String.Empty
  114.         channelpricetextbox.Text = String.Empty
  115.         channeltextbox.Text = String.Empty
  116.         amountduetextbox.Text = String.Empty
  117.         TextBox4.Text = String.Empty
  118.     End Sub
  119.  
  120.  
  121.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  122.  
  123.     End Sub
  124. End Class
Nov 3 '07 #1
3 1201
debasisdas
8,127 Expert 4TB
Functions are created mainly to return a value. If you want only a sub routine and have nothing to return then procedure is better.
Nov 4 '07 #2
balabaster
797 Expert 512MB
Functions are created mainly to return a value. If you want only a sub routine and have nothing to return then procedure is better.
Something that always astounds me with computer courses (at least in my experience) is that they never really teach code reuse and efficiency of code structure...it's like they put a bunch of concepts out there and tell the students to get on with it. My university course was the exact same way all those years ago - and I've had a lot of on the job learning to do since then.

Obviously you're aware that a sub(routine) (which equates to a procedure) is just a code block that doesn't need to return any values. A function is similar except it returns values.
Expand|Select|Wrap|Line Numbers
  1.  
  2. Sub MyDemoSub(ByVal Param1 As String, ByVal Param2 As String)
  3. 'Do something that requires knowing Param1 and Param2 to
  4. 'achieve the objective. 
  5. End Sub
  6.  
  7. Function MyDemoFunction(ByVal Param1 As String, ByVal Param2 As String) As Boolean
  8. 'Do something that requires knowing Param1 and Param2 to
  9. 'achieve the objective and return some value.
  10. Return Param1 = Param2
  11. End Function
  12.  
As you can see, the demo function just compares the values passed into parameter 1 and parameter 2 and if they are the same, the function returns the value true. Obviously your subs and functions can have as few or as many parameters as you need - but the purpose of them is to allow code re-use...the ability to run through a procedure with a single line of code.
Expand|Select|Wrap|Line Numbers
  1. Dim MyValue1 As String = "Hello" 
  2. Dim MyValue2 As String = "World"
  3. 'We want to see if the values in MyValue1 and MyValue2 are the same
  4. Dim MyBoolean As Boolean = MyDemoFunction(MyValue1, MyValue2)
  5. 'At this point MyBoolean = False
  6.  
Now for the purpose of this demo, the code example is actually longer than the code it would've taken to do:
Dim MyBool As Boolean = (MyValue1 = MyValue2)
But you can easily see that any procedure as complex as needed could be contained within the bounds of the function and any data types could be passed as parameters and any data types could be returned.

It would be fairly simple to include a few functions - I'll start you off with one very basic one - you would probably want something a little more complex to make your code more usable but this will give you an idea of what you're looking for:
Expand|Select|Wrap|Line Numbers
  1. Function ResidentialTotal(ByVal NumberOfChannels As Integer, ByVal ChannelPrice As Decimal) As Decimal
  2.   Return NumberOfChannels * ChannelPrice 
  3. End Function
  4.  
You would call this from your main procedure in the following manner:
Expand|Select|Wrap|Line Numbers
  1. residentialchanneltotalprice = ResidentialTotal(residentialnumberofchannels, channelprice)
  2.  
I started you off with such a simple one because as this is an assignment you should really be learning the benefit of this type of code structure so you should really get to grips with using it. Using that example directly in your code probably won't earn you the best marks because it uses more code than your original - it just demonstrates the concept.
Nov 4 '07 #3
Thanks, that helped a lot.


Something that always astounds me with computer courses (at least in my experience) is that they never really teach code reuse and efficiency of code structure...it's like they put a bunch of concepts out there and tell the students to get on with it. My university course was the exact same way all those years ago - and I've had a lot of on the job learning to do since then.

Obviously you're aware that a sub(routine) (which equates to a procedure) is just a code block that doesn't need to return any values. A function is similar except it returns values.
Expand|Select|Wrap|Line Numbers
  1.  
  2. Sub MyDemoSub(ByVal Param1 As String, ByVal Param2 As String)
  3. 'Do something that requires knowing Param1 and Param2 to
  4. 'achieve the objective. 
  5. End Sub
  6.  
  7. Function MyDemoFunction(ByVal Param1 As String, ByVal Param2 As String) As Boolean
  8. 'Do something that requires knowing Param1 and Param2 to
  9. 'achieve the objective and return some value.
  10. Return Param1 = Param2
  11. End Function
  12.  
As you can see, the demo function just compares the values passed into parameter 1 and parameter 2 and if they are the same, the function returns the value true. Obviously your subs and functions can have as few or as many parameters as you need - but the purpose of them is to allow code re-use...the ability to run through a procedure with a single line of code.
Expand|Select|Wrap|Line Numbers
  1. Dim MyValue1 As String = "Hello" 
  2. Dim MyValue2 As String = "World"
  3. 'We want to see if the values in MyValue1 and MyValue2 are the same
  4. Dim MyBoolean As Boolean = MyDemoFunction(MyValue1, MyValue2)
  5. 'At this point MyBoolean = False
  6.  
Now for the purpose of this demo, the code example is actually longer than the code it would've taken to do:
Dim MyBool As Boolean = (MyValue1 = MyValue2)
But you can easily see that any procedure as complex as needed could be contained within the bounds of the function and any data types could be passed as parameters and any data types could be returned.

It would be fairly simple to include a few functions - I'll start you off with one very basic one - you would probably want something a little more complex to make your code more usable but this will give you an idea of what you're looking for:
Expand|Select|Wrap|Line Numbers
  1. Function ResidentialTotal(ByVal NumberOfChannels As Integer, ByVal ChannelPrice As Decimal) As Decimal
  2.   Return NumberOfChannels * ChannelPrice 
  3. End Function
  4.  
You would call this from your main procedure in the following manner:
Expand|Select|Wrap|Line Numbers
  1. residentialchanneltotalprice = ResidentialTotal(residentialnumberofchannels, channelprice)
  2.  
I started you off with such a simple one because as this is an assignment you should really be learning the benefit of this type of code structure so you should really get to grips with using it. Using that example directly in your code probably won't earn you the best marks because it uses more code than your original - it just demonstrates the concept.
Nov 4 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

35
by: wired | last post by:
Hi, I've just taught myself C++, so I haven't learnt much about style or the like from any single source, and I'm quite styleless as a result. But at the same time, I really want nice code and I...
3
by: Mariusz | last post by:
I want to write function to call another function which name is parameter to first function. Other parameters should be passed to called function. If I call it function('f1',10) it should call...
6
by: gustav04 | last post by:
hi all i have a question: what is the difference between a c-function and an c++ class method (both do exactly the same thing). lets say, i have a function called print2std() and a class...
3
by: Praveen | last post by:
Hi All, I have some procedures which written in Oracle. Now the requirement has come to convert those procedures into db2. I used IBM provided MTK tool to convert the procedures. The tool has...
8
by: WindAndWaves | last post by:
Hi everyone Why do you use the word public in a function, when the function is fully accessible without the word public in front of it. I read the help, but did not really get it. Cheers ...
8
by: Randy Harris | last post by:
I've been working with Access for many years (early days of 2.0). Since leaving 2.0, one of my greatest frustrations with the product is the difficulty of locating procedures within a module. ...
14
by: digital | last post by:
hello anyone... pls explain me , how different between function and procedure for C/C++ and Pascal. Thankx......
3
by: chreo | last post by:
I have user-defined function in MSSQL which returns Table (with 10 columns) (sorry for Polish names) CREATE FUNCTION PACZKI_Z_AKCJI (@AKCJA_ID int) RETURNS TABLE RETURN SELECT TOP 100...
0
by: hastha23 | last post by:
Dear Friends, My oracle Version is 10g. I calling a function from sql select same time function body contain DML statement,that time is possible call function from sql? and again one, ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.