Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MS VB6 Step by Step - Page 284

Status
Not open for further replies.

JWJ

Technical User
Jun 27, 2000
15
0
0
US
Michael Halvorson, on page 284 of the referenced text, gives the following example of a procedure in a standard module to hold a function declaration.

To wit:

Function Rate (Hits, Attempts) As String
Percent = Hits / Attempts
Rate = Format(Percent, "0.0%")
End Function

On the next page, at the end of the fourth line, he states: "The Hits argument and Attempts argument are placeholders for the two variables that will be passed to the function during the function call."

Later on page 284 (step 13), he enters the following statment as the last line in a Command1_Click event procedure.

lblRate.Caption = Rate(Wins, Spins) which are public variables and populated in other program code.

I am thoroughly confused as to why he uses the Hits and Attempts argument in the Rate Function. Nowhere does he populate these variables in the illustration of the function procedure.

Any help would be greatly appreciated.


JWJ

James W. Jozwiak
jjozwiak1052@earthlink.net
 
I'm a bit confused of what you say you read in a book.

First of all:
Why passing public variables into a method (since they're public they already can be accessed there).
Second:
The variables in the function prototype aren't typed (they will be of the type variant now.
Last but certainly NOT least:
You should NOT use public variables, unless you ABSOLUTELY HAVE to.

Now to answer your question:
Let's consider that you're a decent programmer and do NOT use global variables. You have a command_click function in which you want to perform certain actions on two variables. By passing these two variables to another function, they can be used inside that function and can be operated on.

Unless the writer is trying to make a point in this chapter, I suggest you try another book, with better examples. After all, if you start learning, better start learning it the right way immediately (that's easier than trying to change a bad habbit you developed during the process).
Greetings,
Rick
 

They are passed into this function. Here is a bad example but...
[tt]
Dim I As Integer, A As Ingeger
A = 100
I = A
[/tt]
Somewhere in code I am setting a to a value I then pass that value to I. So in...
[tt]
Function Rate (Hits, Attempts) As String
Percent = Hits / Attempts
Rate = Format(Percent, "0.0%")
End Function
[/tt]
He is setting somewhere in code Wins and Spins. Then passing the value of these to the function (Hits, Attempts) where the function then does an operation on those values and returns a decimal number formatted as a string.

Does that help a little?

Good Luck

 
Function Rate (Hits, Attempts) As String
Percent = Hits / Attempts
Rate = Format(Percent, "0.0%")
End Function
This means when you call the function Hits is used within the function to represent the first argument and Attempts to represent the second. You don't need to declare them within the function (and indeed mustn't) because the are only references to whatever Rate is called with.

Suppose you had calls
lblRate.Caption = Rate(Wins, Spins)
and
lblRate.Caption = Rate(before, after) where before, after are also global variables.

Although they are different global variables, what happens to them within the function is the same. You could also have
lblRate.Caption = Rate(12, 24)
which would work equally well.

Looking at the code, Percent should actually be declared as something within the function, and format should be format$ as it produces a string. Also the first line should say what sort of variables are expected. As it is written you could call

lblRate.Caption = Rate("sinead", "quin")
which isn't going to work.

Also, if Attempts is 0 it's not going to work either, perhaps it should return 0 as returning infinity is tricky.

Function Rate (Hits as Long, Attempts as Long) As String
if Attempts = 0 then
Rate = "0"
else
Rate = Format$(Hits / Attempts, "0.0%")
endif
End Function

You don't need Percent at all. Of course you are ahead of me here. What if I want Rate(1.23,4.56) you ask. Then you could declare them 'As Variant'. Then you'd need to check for nulls...

 
LazyMe

Why passing public variables into a method (since they're public they already can be accessed there). -- Because not every invocation of the function may be acting on the same set of variables - that's why we have parameterized routines. It just so happens that in this case, the arguements happen to be two public variables. The next call to the same function may be with two local variables.

You should NOT use public variables, unless you ABSOLUTELY HAVE to. - Public variables should be used whenever public variables are called for. When the natural scope of an item is public, then so be it. It uses less memory, and is more efficient then incurring the overhead of including them on the call stack as parameters each and every time you pass then to another routine.

Whereas I agree that it generally a good idea to explicitly type all variables, I would caution you on classifying programmers as decent or not based on your own arbitrary standards,

I not sure that I would stick my neck out as you have to critique of a book based solely on the information provided in this thread. Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
JWJ - Although other contributors have already correctly answered the question, perhaps explaining it another way may make it easier to understand.

Many function have what are known as parameters. These are aliases for values that are determined at the time the function is called. When the function is called, it is called with arguments. What essentially happens is that each function parameter assumes the value of the corresponding argument at the time the function is called.

Here is the function, and the function contains two parameters, the first being Hits, and the second being Attempts.
Code:
Function Rate (Hits, Attempts) As String
   Percent = Hits / Attempts
   Rate = Format(Percent, "0.0%")
End Function
When the function is called:
Code:
lblRate.Caption = Rate(Wins, Spins)
It is called with two arguments - the first being Wins, and the second being Spins.

When the function is entered, the first parameters (Hits) assumes the value of the first argument (Wins), and the second parameter (Attempts) assumes thes value fo the second argument (Spins).

The next time I call the function
Code:
diffLabel.Caption = Rate(Heads, Flips)
Hits assumes the values of Heads, and Attempts assumes the value of Flips.

So in general terms, the nth parameter of the routine assumes the value of the nth argument from the call.

That being said, you can have optional parameters, and there is pass by value and pass by reference, but we'll save these for a later discussion. Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
CajunCenturion

>Because not every invocation of the function may be acting on the same set of variables - that's why we have parameterized routines. It just so happens that in this case, the arguements happen to be two public variables. The next call to the same function may be with two local variables.

Totally true. Maybe I should have made my point differently (or rather; probably) The point is, since this seems to be some sort of dummy book (like one we all started with), it can be VERY confusing using such techniques while trying to explain what methods are for and how they operate. Suppose you're reading the same book, don't know anything etc. and you see something like explained by JWJ, wouldn't you be as confused as he is-> why doing that? This seems to have lead to his question (I guess).



>It uses less memory, and is more efficient then incurring the overhead of including them on the call stack as parameters each and every time you pass then to another routine.

This however, I do not completely agree with. First of all allocating one variable publically uses the same amount of memory as allocating it in the scope of a function, although it might be (don't know for sure if this is true in VB), that the public one will be allocated on the heap, whereas the function scoped one on the stack frame. Point is 4 bytes remain 4 bytes. Besides that, passing them to another function, will always result in pushing them on the stack and popping them from the stack in the destination function. It makes no difference whether the variable is scoped publically or locally.

Greetings,
Rick
 
Hi,
I believe that the default input parameter referance type is Byref (personally, I allways specify). Therefore, it could be argued that leaving them as variants in the function relies simply on the calling parameters datatypes - no in memory conversion necessary.

Personally, I would have typed them as it is a good habbit to get into (double covers most basis I think). It stops programmers inadvertanly sending an 'invalid' type that would fail in the equation. We should all get out of the habbit of leaving things (or even specifing things) as variant, as it is no longer valid in VB.Net! (with the obvious exception of asp VBS where we have no choice!).

However, as there is no validation at all in this fn, then I would assume that further on, the author may cover validating the function (div by zero, datatype discrimination etc) without actually taking the snippet within the books context, we can only sneer at seeming obvious omissions/errors.

The useage of Global, form public and private variables is something that comes with experiance. Basically, if the variable is required in lots of forms without changing state (i.e. it carries it value - changeable or otherwise - through to each module) then make it global to save memory and processor time and to keep things simple. If it is used in all subs in a form, say, and needs to maintain state then a Form level public. Otherwise, pretty much, make it local and pass it if necessary.
If you are using it in a function, pass it. You may decide later to prote the function to a class, and you will not have to recode so much.

Wolf.
 
In reference to Dr. Halvorson's book, I would not necessarily make any assumptions because, unless I had the book in front of me, I wouldn't know the context in which things were being stated. We all know how misleading things can be when things are taken out of context.

With respect to the public variable discussion, the appropriate thing to do would be start a new thread for further discussion.
Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
as with regard to the book, it comes free with the vb6 deluxe learning edition. "quick and easy self-paced training in ms vb 6" (the first vb book i ever read)

the code is mearly an example of passing values to a function, it is not intended(i dont think) to make you a "decent programmer"

the book that accompanies it "VB 6 Programmers guide" begins to introduce the concept of becoming a "decent programmer" and explains itself very well.

hope this clears a few things up!
 
On a different level.... How do you pass a parameter to an EXE file that was created with VB? i.e. Testprogram.exe parm1 and then use parm1 in the program ?

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top