Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning Microsoft Visual Basic 2008 ISBN: 978-0-470-19134-7
This is the forum to discuss the Wrox book Beginning Microsoft Visual Basic 2008 by Thearon Willis, Bryan Newsome; ISBN: 9780470191347
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Microsoft Visual Basic 2008 ISBN: 978-0-470-19134-7 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 13th, 2009, 12:05 PM
Registered User
 
Join Date: Feb 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default More Chapter 3 Method questions.

First a little background -
My first programming course punched cards for SYS370 in FORTRAN, and years (and years) ago, I did some TurboC. (in fact, for you Borland/CodeGear folks, Allan Bauer used to be my roomate and co-worker, but I could never get my brain around Pascal..) However, my career never really required me to do much
programming. I'm finding now that it's becomming more of a requirement to stay competative.

*SO*

Working on the Building a Method Try it Out on P.79. Worked ok as the book instructed, but I got the fancy idea of why not make the messagebox a sub as well, and why not make dblArea a public var? (cobbwebs getting brushed away in the back of my brain..).

So, I came up with this with AreaMessageBox() as a sub as well.

PublicClass Form1
'declare dblArea as public var
Dim dblArea AsDouble
PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'calculate the area of a circle with radius of 100
dblArea = CalculateAreaFromRadius(100)
'display the results
AreaMessageBox(dblArea)
EndSub
PrivateSub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'Calculate the area of a circle with radius of 25
dblArea = CalculateAreaFromRadius(25)
'display the results
AreaMessageBox(dblArea)
EndSub
PrivateSub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
'calculate area of circle with radius of 250
dblArea = CalculateAreaFromRadius(250)
'display the results
AreaMessageBox(dblArea)
EndSub
'Calculate area from radius - find the area of a circle
PrivateFunction CalculateAreaFromRadius(ByVal radius AsDouble) AsDouble
'declare variables
Dim dblRadiusSquared AsDouble
Dim dblResult AsDouble
'square the radius
dblRadiusSquared = radius * radius
'multiply by pi
dblResult = dblRadiusSquared * Math.PI
'return the result
Return dblResult
EndFunction
'Area Messagebox sub
PrivateFunction AreaMessageBox(ByRef dblArea AsDouble)
'display message box for the area calucations for each button click
MessageBox.Show("The area of the circle is " & dblArea.ToString, "Circle Area")
Return (dblArea)
EndFunction
End
Class

I pass dblArea to AreaMessageBox, and everything works. But WHY do I need to pass dblArea back as part of the return statement? I really don't need it. How can I get it to pass back a null or T/F of sub completion?

Thanks!
Allen
Reply With Quote
  #2 (permalink)  
Old February 16th, 2009, 02:07 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Based on the code you have presented here AreaMessageBox should be a method and not a function since the value that is returned is never used to evaluate anything or used otherwise.

Now my statement is based soley on the code you have presented here and since I have not read the book in question, I would exercise caution in changing this to a method if this sample is something the author builds on later in the book where AreaMessageBox needs to return a value for some reason.

Outside of that, I see no reason why this shouldn't be a method.

hth.
-Doug
__________________
===============================================
Doug Parsons
Wrox online library: Wrox Books 24 x 7
Did someone here help you? Click on their post!
"Easy is the path to wisdom for those not blinded by themselves."
===============================================
Reply With Quote
  #3 (permalink)  
Old February 16th, 2009, 11:39 PM
Registered User
 
Join Date: Feb 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by dparsons View Post
Based on the code you have presented here AreaMessageBox should be a method and not a function since the value that is returned is never used to evaluate anything or used otherwise.

Now my statement is based soley on the code you have presented here and since I have not read the book in question, I would exercise caution in changing this to a method if this sample is something the author builds on later in the book where AreaMessageBox needs to return a value for some reason.

Outside of that, I see no reason why this shouldn't be a method.

hth.
-Doug
So, I guess I don't get the difference between a method and a function. In my mind, they are both subroutines, so I don't see the distinction. In C, there is no distinction. Maybe I need to read some more of the book, or you (or anybody else) and explain?

I went and pulled out my old C book ("The Waite Group's TurboC Programming for the PC Revised Edition, Copywrite 1989) to confirm that. I'm trying to NOT do this as I'm afraid that it will only confuse me more!

Thanks!
Allen
Reply With Quote
  #4 (permalink)  
Old February 17th, 2009, 12:14 AM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Hi Allen!

As far as the difference between a Subroutine and Function go it is really quite simple. A sub routine simply DOES something where as a Function RETURNS something. So you write a sub when you need to do something but arent expecting a return value (e.g. a method to save info to a database) and you would use a function when you need to do some further processing with the return value (when you are returning data from a database for example).

Does that make sense?

hth.
-Doug
__________________
===============================================
Doug Parsons
Wrox online library: Wrox Books 24 x 7
Did someone here help you? Click on their post!
"Easy is the path to wisdom for those not blinded by themselves."
===============================================
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 1 - Two questions mikener BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 6 March 17th, 2008 03:59 PM
Two Chapter 3 Questions psychonet BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 6 April 13th, 2007 11:39 PM
Comments and Questions for Chapter 2 nguyendh BOOK: Ivor Horton's Beginning Visual C++ 2005 0 June 24th, 2006 07:16 PM



All times are GMT -4. The time now is 03:35 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.