quick vb.net question

jae

Golden Member
Jul 31, 2001
1,034
0
76
www.facebook.com
I, for the life me, can not grasp the point of usings sub-procedure or functions. Maybe its because trying to study sick right now, but i just cant see what it would be used for. My professor told us that the book had a really horrid example of sub-procedure using SelectColor; but never gave a better example. So can someone here quickly give a better explanation of when one would be used.

*I understand that sub-procedure values cant be accessed outside the procedure and functions can.

Thanks
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
You use them to help organize the code, and break up chunks of logic into separate entities.

As far as a sub procedure vs a function, a sub procedure does not return a value while a function does. It's just a naming thing, there really isn't that much of a difference.

You would use a function when you want to write a routine for say Squareroot, which would return the sqrt of the value passed in via parameters. Whereas for a sub procedure you could write a routine that queries a specified url with the specified parameters using the GET method for simulating a form submission. You don't really care about the return value, because any errors would be reported using Exceptions.
 

PhatoseAlpha

Platinum Member
Apr 10, 2005
2,131
21
81
Functions and procedures are the basis of all vb.net. I'm actually fairly certain you can't do anything that isn't a function or a procedure.

As noted, they modularize and abstract, letting you write the code to do something once and use it as many times as you need. An example sub would be a subroutine that blanked a window and redrew it according to certain parameters. An example function would be something that returned a value (say, price) for a particular object.


It's called abstraction, and it's absolutely vital. Cut and pasting may seem just as effective at first.....but then the first time you have to make a change to your code in seventeen different places because you did that, you realize why we don't do that. Functions and Sub serve to keep frequently used bits of code in a single place so you only have to fix them in one place when they invariably end up bugged.
 

jae

Golden Member
Jul 31, 2001
1,034
0
76
www.facebook.com
thanks guys that really helped especially the last paragraph alpha. the book had me thinkin in a way where i would just use a handle instead. but this really cleared things up for me