Methods and Functions are blocks of code that you create to perform particular actions. For example, if you wanted to know how many characters were in a particular TextArea, you could write a method to do the calculations. As we will see, you could also write a function to do the same thing but in a slightly different way. In the following we will examine both methods as a means of illustrating the difference between methods and functions.
Creating a Method:
To add a method select a window and then click the Insert icon in the menubar. Select “New Method… ” from the Insert menu. Enter a name for the method and, if desired, any parameters that the method will use. In this example we will be passing the CountCharacters method a string of characters. The method will then count the characters in the string we have passed to it.
Parameters must be separated by commas.
In the code editor enter the code for CountCharacters method.
TextField1.text = str(len(theWords))
The code uses the “len” function to get the number of characters in “theWords”. The “str” function is used to convert the number to a string so it can be displayed in EditField1.
The code in PushButton1 uses our CountCharacters method.
It passes the text in TextArea1 to CountCharacters as a parameter.
CountCharacters then executes its codeplacing the results in TextField1
On the next page we will see how to accomplish the same thing using a Function instead of a Method.