By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,959 Members | 1,136 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,959 IT Pros & Developers. It's quick & easy.

function with no return value works without problem

arunmib
100+
P: 104
Hi all,
Before posting my question I would like to state that "I am novice in VB, rather to say I am learning VB. My previous coding experience has been with C/C++". Ok, now to the question.

Expand|Select|Wrap|Line Numbers
  1. Public Function Add(ByVal iNum1 as Integer, ByVal iNum2 as Integer) As Integer
  2.  
  3. Dim iSum as Integer = 0
  4.  
  5. iSum = iNum1 + iNum2
  6.  
  7. End Sub
So, conceptually speaking there is no "return" statement in the function. So when I build my project there must be an error right. But everything builds fine, not even a warning and I am even able to run the code without any problem and i get the expected(correct) answer.

I mean how is this possible????


P.S : I am using VB.Net and tool is Visual Studio 2003.
Sep 20 '07 #1
Share this Question
Share on Google+
5 Replies


kadghar
Expert 100+
P: 1,295
Hi all,

I mean how is this possible????


P.S : I am using VB.Net and tool is Visual Studio 2003.
well that's possible, if you move variables that aren't byval or private inside a function, they'll change in the whole program.

the problem of not giving a return value is that you wont be able to do something like this:

a = sum(b)

you'll have to call the function as a procedure... and in that case, its better to create a procedure (sub) and call it.

In latter versions of VB (like vb express edition) you'll recieve a warning if you do this.
Sep 20 '07 #2

Expert 5K+
P: 8,434
I don't see anything wrong with the posted code, except that...
  • Since you don't set the return value, I would expect it to always return 0
  • End Sub isn't correct for a function, but perhaps VB2003 has a more relaxed attitude to that than VB6.

Is it possible that you are actually invoking a built-in Add function rather than your own code?
Sep 21 '07 #3

QVeen72
Expert 100+
P: 1,445
... So, conceptually speaking there is no "return" statement ...
Hi,

In VB6 and higher versions (.Net), you don't have to specify "Return" keyword (like you do in C++).
Yes, It dosen't throw any error.
In the above Function, Function Name should be populated with the Result (which has to be returned)

Add = iSum

In calling program, you can specify

txtSum.Text = Add(2, 8)


Regards
Veena
Sep 21 '07 #4

Expert 5K+
P: 8,434
... In VB6 and higher versions (.Net), you don't have to specify "Return" keyword (like you do in C++).
Just a historical note; there was no Return available in any of the VB versions up to and including version 6.



(Well... technically there is a Return statement, but it has a completely different meaning and should be ignored here.)
Sep 21 '07 #5

arunmib
100+
P: 104
thanks for the reply ppl :-)
Sep 22 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.