Face it, we need them. Procedures save us from writing the same group of statements over and over. It's a search for re-usability and it is the major reason why object oriented programming was developed. The approach in today's lesson is the use of procs - often short for procedures or 'subroutines' in other languages.
Brief ExplanationA procedure(or proc) is a group of related statements that is used to accomplish a task. Once you define a proc you can execute(or call) it whenever you need to. Most importantly, procedures provide a way to divide a complex program into smaller, more do-able tasks and takes off some of the load for programmers. Personally, without procedures, or procs, or functions, or what ever you wanna call them, serious programming would be just about impossible.
Built-in ProceduresDM provides you with built-in procedures that are already incorporated into the language. The concept of a built-in procedure is so that it is already there and you don't have to make it yourself. Below is an example of a built-in procedure that rounds a number to the nearest 100th.
mob/verb/Round()
var number_prompt = input("Enter a number you would like to round") as num
// Prompting the user(you) to enter a number you would like to round.
// Note: input() is a built-in procedure, also.
if(!number_prompt)
src << "You need to enter a number."; return
// If the user didn't specify a number do nothing..
src << "When rounded, [number_prompt] is equal to: [round(number_prompt, 100)]."
// Notice the round(), that is a built-in procedure that is declared inside of a variable following [] closed brackets.
We defined two built-in procedures in that example: input() and round(). We used input() to prompt the user to enter a number and we used round() to round the number the user entered.
Below is another example of another built-in proc called rand(), which is short for random.
mob/verb/Randomize()
src << "The numbers are.... [rand(1, 100)]"
That example was more simple. Each time you press the verb Randomize it gives you random numbers from 1 to 100. Nice, huh?
Self-Made ProcsNow, on to a more complex way of procedures. These are procedures that are made by you. They only carry out what YOU tell them to do. Of course, you just can't write something like... 'get the average of two numbers'.. no.. you have to do it in a systematic way, a way that the compiler can understand. In the example below, I will make a procedure that returns the value of two numbers, 'x', and 'y'.
proc/average(x, y)
return(x + y) / 2 ; // Returns the value of 'x' + 'y' divided(/) by 2.
mob/verb/getAverage()
src << average(10.3,15.7) // Outputs the average(x,y) procedure(shown above) to the user.
That is an example of a procedure made by a user, designed to do a specific task. That's all, folks.
Wrap Up: In this tutorial we looked at identifying and creating built-in and self-made procedures. Next week's tutorial will be identifying and using different procedure types.