Re: If If statement is false then continue program flow

Posted 11 November 2012 - 06:13 PM

Raminator, on 11 November 2012 - 04:37 PM, said:

Oops, sorry i forgot to put the code last time, this is how the function code is looking now:

Well as I said, it's close. The reason I turn on Option Strict and Option Explicit is so that problem areas will be pointed out to you as you type, and mostly, will not let you compile when there's a problem.

Your Function getsqrt() should have an "As" clause, so you should add As BigInteger after the ")".

Also, it would have shown you an error saying "Function 'getsqrt' doesn't return a value on all paths." What this means is that there is a possibility of returning a NULL reference. It won't always matter, but it's a good practice to do it right, for the times when it does matter, and you have to figure out why your program is failing, sometimes because of an obscure and difficult to find reason.

As for the Console, you will see, if you run the code using Visual Studio, the output will show up in the Output window.
I put the second button in there to stop the program, though when running from Visual Studio, you can stop it with the Debugger.

A few items of note:
There are two ways of returning a value from a function. One is to use Return(value), This is fine. Perfectly legal and good code. The disadvantage of it is when you have multiple Return statements in different parts of the function. It means that ther are more than one exit from the function. Personally (and you may differ in your opinion), I like to have only one exit from the function, and so, if I use Return(value) I set the value in all the places where a Return would also work, and then use the Return(value) at the end of the function.

Alternatively (and I like this way a lot), you can set the variable named the same as the function, to the return value, and when you fall out of the function, you return the value in that variable.

So up there, right after the Dim statements, I have getsqrt = 0, making it the default return value. As long as I never find a number that I'm looking for, all I need to do is finish the function, and the default value comes back.

You did well. I was glad to see your efforts to try suggestions and your willingness to learn.

Re: If If statement is false then continue program flow

Oops, sorry i forgot to put the code last time, this is how the function code is looking now:

Well as I said, it's close. The reason I turn on Option Strict and Option Explicit is so that problem areas will be pointed out to you as you type, and mostly, will not let you compile when there's a problem.

Your Function getsqrt() should have an "As" clause, so you should add As BigInteger after the ")".

Also, it would have shown you an error saying "Function 'getsqrt' doesn't return a value on all paths." What this means is that there is a possibility of returning a NULL reference. It won't always matter, but it's a good practice to do it right, for the times when it does matter, and you have to figure out why your program is failing, sometimes because of an obscure and difficult to find reason.

As for the Console, you will see, if you run the code using Visual Studio, the output will show up in the Output window.
I put the second button in there to stop the program, though when running from Visual Studio, you can stop it with the Debugger.

A few items of note:
There are two ways of returning a value from a function. One is to use Return(value), This is fine. Perfectly legal and good code. The disadvantage of it is when you have multiple Return statements in different parts of the function. It means that ther are more than one exit from the function. Personally (and you may differ in your opinion), I like to have only one exit from the function, and so, if I use Return(value) I set the value in all the places where a Return would also work, and then use the Return(value) at the end of the function.

Alternatively (and I like this way a lot), you can set the variable named the same as the function, to the return value, and when you fall out of the function, you return the value in that variable.

So up there, right after the Dim statements, I have getsqrt = 0, making it the default return value. As long as I never find a number that I'm looking for, all I need to do is finish the function, and the default value comes back.

You did well. I was glad to see your efforts to try suggestions and your willingness to learn.

So, i believe we are almost over with this project ( ) this is how the code is looking like now(i did a few changes to make it look a little better)