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

question about returning value of a function

Bob
Hi,

i'm not sure to understand how a value is returned from a function.
Look at this code below about a simple function that compares a value with
values 1 from 5.
I thought: if the passed value (x = test(?)) is not from 1 to 5, then the
return = -1. That's ok.
If not, the function will return value z, but at the end of the for/next
loop, the other return (return -1) will also be executed, no? The line
'return -1' is outside of the loop and will be executed in all cases?

Thanks
Bob
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
Dim x As Integer
x = test(6)
Response.Write(x)
End Sub
Function test(ByVal z As Integer) As Integer
Dim i As Integer
For i = 1 To 5
If z = i Then
Return z
End If
Next
Return -1
End Function
Dec 30 '06 #1
5 1180

For i = 1 To 5
If z = i Then
Return z
Exit Function '// ADD THIS LINE
End If
Next

"Bob" <.wrote in message news:u6****************@TK2MSFTNGP02.phx.gbl...
Hi,

i'm not sure to understand how a value is returned from a function.
Look at this code below about a simple function that compares a value with
values 1 from 5.
I thought: if the passed value (x = test(?)) is not from 1 to 5, then the
return = -1. That's ok.
If not, the function will return value z, but at the end of the for/next
loop, the other return (return -1) will also be executed, no? The line
'return -1' is outside of the loop and will be executed in all cases?

Thanks
Bob
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Dim x As Integer
x = test(6)
Response.Write(x)
End Sub
Function test(ByVal z As Integer) As Integer
Dim i As Integer
For i = 1 To 5
If z = i Then
Return z
End If
Next
Return -1
End Function

Dec 30 '06 #2
Bob
Thanks, but in the code i used as example, there was no EXIT function. So
why will the Return -1 not be executed if the comparaison is true?

"Just Me" <news.microsoft.comschreef in bericht
news:%2****************@TK2MSFTNGP06.phx.gbl...
>
For i = 1 To 5
If z = i Then
Return z
Exit Function '// ADD THIS LINE
End If
Next

"Bob" <.wrote in message news:u6****************@TK2MSFTNGP02.phx.gbl...
>Hi,

i'm not sure to understand how a value is returned from a function.
Look at this code below about a simple function that compares a value
with values 1 from 5.
I thought: if the passed value (x = test(?)) is not from 1 to 5, then the
return = -1. That's ok.
If not, the function will return value z, but at the end of the for/next
loop, the other return (return -1) will also be executed, no? The line
'return -1' is outside of the loop and will be executed in all cases?

Thanks
Bob
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Dim x As Integer
x = test(6)
Response.Write(x)
End Sub
Function test(ByVal z As Integer) As Integer
Dim i As Integer
For i = 1 To 5
If z = i Then
Return z
End If
Next
Return -1
End Function


Dec 30 '06 #3
"Just Me" <news.microsoft.comschrieb:
For i = 1 To 5
If z = i Then
Return z
Exit Function '// ADD THIS LINE
End If
Next
This line will never be executed because the method will be leaved when
'Return z' is executed.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Dec 30 '06 #4
Bob
Thanks

"Herfried K. Wagner [MVP]" <hi***************@gmx.atschreef in bericht
news:ug**************@TK2MSFTNGP03.phx.gbl...
"Just Me" <news.microsoft.comschrieb:
> For i = 1 To 5
If z = i Then
Return z
Exit Function '// ADD THIS LINE
End If
Next

This line will never be executed because the method will be leaved when
'Return z' is executed.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Dec 30 '06 #5
The return statement is an instruction to "return" along with the value to
return so yes you will get a -1 in your test sample. The line Return -1 is
outside the loop but no it won't be executed in all cases. If the value
passed is between 1 and 5 the earlier return statement will exit the
function.

That said (if you're developing good habits) you should reduce the number of
return statements (ideally to one) and it should generally be at or near the
end of the function. This can be accomplished by assigning a return
variable (as needed in the routine) and returning it as the routine exits.
"Bob" <.wrote in message news:u6****************@TK2MSFTNGP02.phx.gbl...
Hi,

i'm not sure to understand how a value is returned from a function.
Look at this code below about a simple function that compares a value with
values 1 from 5.
I thought: if the passed value (x = test(?)) is not from 1 to 5, then the
return = -1. That's ok.
If not, the function will return value z, but at the end of the for/next
loop, the other return (return -1) will also be executed, no? The line
'return -1' is outside of the loop and will be executed in all cases?

Thanks
Bob
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Dim x As Integer
x = test(6)
Response.Write(x)
End Sub
Function test(ByVal z As Integer) As Integer
Dim i As Integer
For i = 1 To 5
If z = i Then
Return z
End If
Next
Return -1
End Function

Dec 30 '06 #6

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

Similar topics

4
by: Asif | last post by:
Hi there, I have been trying to understand the behaviour of char (*pfn)(null) for a couple of days. can some body help me understand the behaviour of char (*pfn)(null) in Visual C++ environment?...
11
by: Daniel Wilcox | last post by:
I have a question, I have been given a piece of code that apparantly compiles under Visual C++ but I cannot get to compile under g++ 3.2. I have read the FAQ and delved into the Stroustrup book...
11
by: kazack | last post by:
I am under the the impression that a variable is what is stored in a memory address and a pointer is the memory address of the variable? I was looking to use a function that does not return a...
5
by: Sue | last post by:
After finishing up my first quarter JavaScript on 12/12/03, I decided to improve character checking on my project. In my project I only had to do very basic validation. Therefore, I only had one...
31
by: grahamo | last post by:
This came up in an interview I did a while ago and I wanted to know the correct answer. The setup is this; If I have a base class "food" and also two classes "meat" and "veg" that inherit from...
3
by: shaun | last post by:
This is going to seem very naïve, I'm afraid: I have the following: vector<string> DBXMLStructure::getFolderNames(){ const vector<string> & folderList(m_pdb->listFolders()); return folderList;...
1
by: john | last post by:
Relatively new to C coding, so any help would greatly be appreciated. I'm having problems try to return my string array from my parsing function. When I do a printf I am getting the correct value...
4
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's...
4
by: voidtwerp | last post by:
Hi, I hope this is not too OT but I would like clarification on how classes are held in memory. each object obviously has an individual copy of its data. I guess a class would only have one...
10
by: mdh | last post by:
Sorry in advance if this is really dumb, but I am trying to get my head around exactly what the declarator is. In the FAQ, 1.21, part of it says "C declarations ....come in 2 parts, a base type and...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.