you still have not fixed the main sub routine
and the test function is being called outwide the loop
we will enevtually get rid of the junk
and end up with a cleaner piece of code
the problem her that too many people are making odd ( unfortunately not helpful comments)
the word arg is in the brackets of the declaration of the function isprime()
so it is a value passed to the function and used within it to do what ever task you have specified!
when we call the function isprime(arg) we use the variable that we want to test
in your case the variable is the X thing from the loop
we want to test x to see if it is a prime
the test is exhaustive as the definition of a prime is a value that can be devided by itsself and 1 only!
so lets look at you atempt at ISPRIME()
Code:
Public Function isprime(ByVal arg) As Boolean
Dim n As Integer ' why have you introduced n here - do you think it is the upperlimit
' if we needed it I would have suggested passing more than one argument
Dim ReturnValue As Boolean = True ' what is the purpose of this declaration - is it to allow us to pass the answer back?
' answers (returned values) are returned in the name of the function
' so when we are ready the result will be isprime=<something>
For F = 2 To n ' ok so now your cooking you are about to test from 2 to n the upper limit
' whoops this function was never passed the upper limit
' why would you pass 50 to test and see if lets say 9 was a prime number?
If n Mod F = 0 Then ' right a test , good thinking, but what are you testing?
' i see its that fictitious upperlimit again ( aargh!)
ReturnValue = False 'yup thats the right output, but in the wrong place
'we don't have a function called returnvalue
'its called ISPRIME
Exit For 'and yes cancel any more test once devisable it cannot be prime
End If
Next
Return ReturnValue 'this is not need as you will have already filled the variable with the result
'the function is waiting to end
End Function
my comments are true for the vast number of languages that support function, true some of them use return <something> to send back the result, but vb does not ( although i'm not sure about .net)
anyway...
good attempt now lets fix it
Code:
Public Function isprime(ByVal arg) As Boolean
isprime = True ' the default state, all numbers are prime unless they are not
' no need for "if else"
For F = 2 To arg-1 ' arg is the value you want to test we know it devides itself
' can anything smaller devide it
If arg Mod F = 0 Then ' if a lesser value can devide it
isprime = False 'set the return value to false (this arg is not a prime)
Exit for ' you might be able to "exit function" here as well i cant test iam on wrong machine!
End If
Next ' get the next smaller value to test arg with
' the isprime return value was set true to start and false when arg was devisable
End Function
hope that makes more sense
now for the final bit cleaning up the main sub
Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim n As Integer 'variable for the lower value
Dim m As Integer 'variable for the upper value
n = InputBox("Enter lower limit") ' ok lets getvthe first value
m = InputBox("Enter Upper limit") ' ok lets get the next value
' good thats 2 value done, but no tests yet
' the actual output of the inboxes will be strings but the typing of n and m will cast them to integers
If n < 2 Then
msgbox("the lower limit is too small - start again")
Exit Sub
End If
If m > 50 Then
msgbox("the upperlimit is too big - start again")
Exit Sub
End If
If n Mod 2 = 0 Then n = n + 1 'this ensures your lowerlimit is odd to start from
'replacement loop
' Remember, we only bother to check odd numbers
For x = n To m Step 2 ' this for does from an odd lowerlimit then +2,+4,+... up to the last odd number before the upperlimit
If isprime(x) Then List.additem(x) 'if the result of a new function called isprime is true then store the x that was tested in the listbox
Next
' loop some more
End Sub
'put isprime() function definition here
End Class
now is that crystal clear now
you may have to fix syntax
i am writting this in text only
dont have a mcchine here to try anything on
sorry
hope this has helped
please rate this and my other posts if the have helped
ask even if there is the tiniest bit you don't understand
nice to see your a girly programmer
sorry if the he hurt but its english for you dont know the gender
here to talk