Creating Your Own Functions
Introduction
Yes, LiveCode comes with hundreds of commands, functions, and procedures. But, you can also create your own. It's like teaching LiveCode a new word to add to its "vocabulary." So, if LiveCode doesn't do something you want, you can create your own function to accomplish it. And, by creating a function you can easily reuse it over and over in your program.
Here's a simple example just to demonstrate the technique. The function merely adds 5 to whatever number the user enters and return the result. Here's the code to add to a button:
function AddSome num1
add 5 to num1
return num1
end AddSome
on mouseUp
ask "Give me a number:"
put AddSome(it) into field "message"
end mouseUp
Another Example: Calculating the Number of Consonants
Here's another example that is a little bit more useful. This example takes any word and returns the number consonants in that word. Here's the code to add to a button:
function addUpConsonants theWord
put "aeiou" into theVowels
put 0 into varCount
repeat for each char i in theWord
if i is not in theVowels then add 1 to varCount
end repeat
return varCount
end addUpConsonants
on mouseUp
ask "Give me a word:"
put addUpConsonants(it) into field "message"
end mouseUp