Procedure with result block is very handy when you need to do something multiple times. This will save you time as you don’t need to repeat the same procedure many times. Also it’s hard to keep track of multiple global variables. This will help you keep your blocks small.
We will make an app that is going to look like this-
It takes an input in a text box and notifies if the input is an even number. Also it gives the square of the number. We will be using procedures with result to accomplish these two operations.
Let’s take a look at the blocks first-
We only have one global variable named result. We initialized it to false just to show you that you can use the same variable to store different things like numbers, texts, booleans, etc.
IsEven procedure checks if a given value is an even number. Since it takes a number to validate, when you need to call it, you have to pass a value. Whenever you need to use a global variable, always initialize it to a default value. Here we initialized result to “NO“. Then we check if the given input represented by a local variable named x is actually a number followed by simple math operation to check if the remainder of dividing the given input by 2 is 0 since we know an even number is divisible by 2. If both are true, we set the value of result to “YES“. Lastly, it returns whatever value variable result holds. If the given value is not a number or an odd number, it will return the default value that we set. Remember the default value we set was “NO“. Note that in design view you can set NumbersOnly property checked if you want the users to only enter numbers. But an empty box is not a number either even if you set that property.
GetSquare procedure returns the square of a given number. We first set a default value for result which is “Not a number!”. Then we check if the given input is a number, if it is, we set the value of result to the square of the given number. Lastly we return result.
When you tap IsEvenBtn, it simply calls the IsEven procedure with the text input and notifies the user with whatever value IsEven procedure returns.
When you tap SquareBtn, it simply calls the GetSquare procedure with the text input and notifies the user with whatever value GetSquare procedure returns.
As always you can download the app for this tutorial ProcedureResults.