473,396 Members | 1,858 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,396 software developers and data experts.

returning void from a Function


Hi Everyone.

Please help.

Is there a way to return void from a Function?

That is, what are the ramifications of writing something like the
following...
Private Function DoSomething()

''''''''''' code

Return Nothing

End Function
Is there any advantage to doing this?

What if there is no return type stated and the Return is left off entirely?
What happens then? Are there any implications?

(What I want to do here is to both explicitly state that there is no return
value and at the same time to use a Function rather than a Sub.)

Please advise.

Thank you.

--Mark.
Nov 19 '05 #1
4 78519
If you return Nothing, that will simply be returning a null reference, as
opposed to a reference to an object. There is nothing wrong with that. You
just need to make sure that whoever is calling this function, knows to check
that the return value is not Nothing before trying to call a method or a
property on the object.

"Mark Kamoski" <mk******@yahoo.com> wrote in message
news:Ob**************@TK2MSFTNGP11.phx.gbl...

Hi Everyone.

Please help.

Is there a way to return void from a Function?

That is, what are the ramifications of writing something like the
following...
Private Function DoSomething()

''''''''''' code

Return Nothing

End Function
Is there any advantage to doing this?

What if there is no return type stated and the Return is left off entirely? What happens then? Are there any implications?

(What I want to do here is to both explicitly state that there is no return value and at the same time to use a Function rather than a Sub.)

Please advise.

Thank you.

--Mark.

Nov 19 '05 #2
Mark,
Void is a C#, C++, Java term, it is not available in VB.NET. Void is no
where near Nothing. Nothing is the same as C#, C++, Java's null.

As Mattias suggested start using Option Strict On at the top of your source
files, you will find that your function needs a return type, your's has a
return type of Object.

To have a 'void' return type you need to use a Sub.

' make Option Strict On compatible
Private Function DoSomething() As Nothing

''''''''''' code

Return Nothing

End Function
' a Void Function
Private Sub DoSomething()
Return
End Sub

Yes, coming from those other languages, its a little odd having two keywords
to define a function, however that's the rules. ;-)

Hope this helps
Jay

"Mark Kamoski" <mk******@yahoo.com> wrote in message
news:Ob**************@TK2MSFTNGP11.phx.gbl...
Hi Everyone.

Please help.

Is there a way to return void from a Function?

That is, what are the ramifications of writing something like the
following...
Private Function DoSomething()

''''''''''' code

Return Nothing

End Function
Is there any advantage to doing this?

What if there is no return type stated and the Return is left off entirely? What happens then? Are there any implications?

(What I want to do here is to both explicitly state that there is no return value and at the same time to use a Function rather than a Sub.)

Please advise.

Thank you.

--Mark.

Nov 19 '05 #3
I think you've answered your own question in your previous post, make a
Sub!!

in C, when you declare a method as returning a void, you are essentially
doing the same in VB when you make a Sub:

Public Sub MyFunc(Param1 As Integer)
....
End Sub

void MyFunc( int Param1 )
{
...;
}

--
Happy to help,
-- Tom Spink
(th**********@ntlworld.com)

" There's no place like 127.0.0.1 "

Please respond to the newsgroup,
so all can benefit.
One Day,
"Mark Kamoski" <mk******@yahoo.com> wrote in message
news:Ob**************@TK2MSFTNGP11.phx.gbl...

Hi Everyone.

Please help.

Is there a way to return void from a Function?

That is, what are the ramifications of writing something like the
following...
Private Function DoSomething()

''''''''''' code

Return Nothing

End Function
Is there any advantage to doing this?

What if there is no return type stated and the Return is left off entirely? What happens then? Are there any implications?

(What I want to do here is to both explicitly state that there is no return value and at the same time to use a Function rather than a Sub.)

Please advise.

Thank you.

--Mark.

Nov 19 '05 #4
A function must specify a return type.
If you don't know what "type" you will be returning you need to return an
"object" and then do a late binding to it to read the return value as
whatever you are doing with it.
I get a sneaking suspicion that you might be better off redesigning your
idea using a "Sub" and passing your parameters as referenced objects. You
will be able to set flags as well stating what the result of your Sub was.
The trouble is that a function can only have one return value. Of course
you can pass parameters by reference to it and change their values from
within the function as you see fit, but this would be confusing code.
Functions should return values and not change the value of parameters.
"Mark Kamoski" <mk******@yahoo.com> wrote in message
news:Ob**************@TK2MSFTNGP11.phx.gbl...

Hi Everyone.

Please help.

Is there a way to return void from a Function?

That is, what are the ramifications of writing something like the
following...
Private Function DoSomething()

''''''''''' code

Return Nothing

End Function
Is there any advantage to doing this?

What if there is no return type stated and the Return is left off entirely? What happens then? Are there any implications?

(What I want to do here is to both explicitly state that there is no return value and at the same time to use a Function rather than a Sub.)

Please advise.

Thank you.

--Mark.

Nov 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jochen Zeischka | last post by:
I'm puzzled. When compiling this: template<class ValRes, class Val1, class Val2> Veld<ValRes>& mult(Veld<ValRes>& res, const Veld<Val1>& v1, const Veld<Val2>& v2) { // something return res; }...
14
by: deanbrown3d | last post by:
Hi there! Suppose I have a function void ShowMessage(string s); (that has a void return type.) In my other function F, also with a void return type, can I do this?
6
by: keepyourstupidspam | last post by:
Hi, I want to pass a function pointer that is a class member. This is the fn I want to pass the function pointer into: int Scheduler::Add(const unsigned long timeout, void* pFunction, void*...
5
by: Giannis Papadopoulos | last post by:
I have the following code #include <stdio.h> void a(void) { printf("a called.\n"); } int b(void) { printf("b called.\n");
8
by: Thomas Barth | last post by:
Hi could someone explain to me why the compiler outputs a warning that the control reaches the end of a non void function? int main(void) { ... pthread_t thrCallOp, thrHangUp;...
14
by: Protoman | last post by:
How would you write a function returning a function pointer and why would you need to do this? Is it: int(*)(int&) fn(int& arg); Thanks!!!
3
by: giladt | last post by:
Hi, I'm a novice programmer and I've come across the following problem: I am trying to get strlen for a cosntant char array but I get back a void no matter what I try. The code looks like...
6
by: Daniel Rudy | last post by:
Hello Everyone, How do I return early from a void function? The following code does not compile. strata:/home/dr2867/c/modules 735 $$$ ->./compile unpifi.c unpifi gcc -W -Wall -Wshadow...
3
by: socondc22 | last post by:
Im stuck at this point. The way i was trying to do this was running if statement around my void function and then a else if statement around a different void function but it didnt work... right now...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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,...

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.