Hi!
What is the difference between a function and a subroutine in vb dot net?
Regards,
Hi!
What is the difference between a function and a subroutine in vb dot net?
Regards,
A function returns a value whereas a subroutine does not. A function should not change the values of actual arguments whereas a subroutine could change them.
I think you've got it...
VBScript is considered "messy" anyhoo...
I often see functions used just like a subs...the functions just don't return anything and you don't store the return value in the call:
...some code...
foo barred, up ' call to a function like a sub...not trapping the return value
...some more code...
function foo(param1, param2)
' some code
end function
VB6 function calls vs. subroutine calls
In VB6, there is a difference between the syntax for a subroutine call and
a function call. The function call is a method with arguments in parentheses,
such as:
success = zip.UnzipMatching("unzipDir","*.jpg",1)
You could also call it as a subroutine, but you *must* leave off the parens:
zip.UnzipMatching "unzipDir", "*.jpg", 1
A function with one argument might seem confusing, because both seem to work:
zip.OpenZip ("test.zip")
and
zip.OpenZip "test.zip"
However, VB6 recognizes the function parameter list by the commas, so the ("test.zip") is just
an expression. You’ll notice that VB6 will insert a space between the method name and
the left paren. However, if you call it as a function, no space is inserted:
success = zip.OpenZip("test.zip")
A function accepts any number of parameters (possibly zero), does something with them, and returns a value. A subroutine is performs an action, but doesn't return a value.
There are two differences between a function and a subroutine: A)How they are invoked. B)How they are accessed.
A function call has the following syntax ::
function(arg1, arg2, ...)
where: function Is the name of the function. arg1, arg2, ... Are the arguments.
A subroutine call has the following syntax
::subroutine (arg1, arg2, ... {outputfield|'format'})
where: subroutine -->Is the name of the subroutine. arg1, arg2, ... Are the arguments. {outputfield|'format'} Is the name of the output field or its format.
In addition, on some platforms, the functions are available immediately; whereas, the subroutines are available in a special subroutine library that you must access.
Let's start with properties. Properties are characteristics of an object. Examples of objects? Textbox, label, form, RadioButtons, etc. Objects can also be defined through class definitions and are instantiated when called upon and used in a program.
Methods are tasks that are tied directly to an object. They occur when an action takes place upon that object. They do not exist without the presence of that object.
Sub Routines (otherwise known as Independent Sub Functions) are functions that are completely INDEPENDENT of OBJECTS. That means that they can be called from anywhere in the program. They do not have a "return value," though they can return information to the calling function via parameter passing by reference.
Functions are code blocks thay create change on some variable or object. They are not necessarily tied to an object (though all methods are functions) and usually have a "return value" that is caught and used somehow in the calling function. Most often, this return value is stored in a variable, though the function call itself can be used in a subsequent function call (example:
Dim sum as Integer = Add(getnumber() + 2) )
I hope that is a help to you -
Bookmarks