ID:119411
 
Keywords: tutorial

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 Explanation

A 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 Procedures

DM 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.

Example 1
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.

Example 2
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 Procs

Now, 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'.

Example 3
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.

shadow on code is an eye sore. Going to see if I can fix that...
Haha, yeah.
text-shadows are just gone now.
I thought you were gonna wait to post this one until tomorrow, but oh well, it's here already. =P
I don't understand the last example. You say "I will make a procedure that returns the value of two numbers, 'a', and 'b'.", but they're actually called "x" and "y", but the proc ignores the values you pass it and uses 10.3 and 15.7 instead, but then you don't pass it any values.
Yeah, typo, forgot to change it, I actually wrote this stuff first before I copied it into the post.
Who's the audience for this? I think if you are going to make these often, give them a level of newbie, intermediate, advanced.
Ganing wrote:
Who's the audience for this? I think if you are going to make these often, give them a level of newbie, intermediate, advanced.

Yeah.. it was mostly intended for beginners but yeah you're right, will do.
mob/varb/Rasengan()
src.Rasenganz()
The examples could be better. You say things like:

Procedures save us from writing the same group of statements over and over

procedures provide a way to divide a complex program into smaller, more do-able tasks and takes off some of the load for programmers

These are excellent points but the examples don't really show it. Maybe you could have each example have two parts - a right way and a wrong way. This way you can easily see the benefit to splitting code into procs. Some examples:

1. Like the last example, show how a calculation can either be repeated everywhere you need it or it can be contained within a proc. The right way is to bundle it in a proc so you can easily change the calculation (average might not be a good example because it's a well-defined operation, you wouldn't be changing it).

2. Show how splitting code different ways can lead to useful procs and not-so-useful ones. Show some function being performed all in one proc. Then show different ways to split it into procs. Based on how you split the proc, it may or may not be re-usable. An example of this might be character creation. First you'd have a single proc that asks you to enter a name and pick a class. Then you could split it in half and call both procs. When split properly (one proc to select a name, one to select a class) you can re-use those procs elsewhere (ex: if you later wanted to change your name or class). You could also show a way to split the code into two procs that makes them not able to be re-used like that.

I'd also mention that procs, in addition to the benefits you mentioned, let you give a name to a set of commands. That way you don't have to remember the set of commands, you just have to remember the name of the proc. You don't have to remember that when a mob explodes you need to create a bunch of /obj/effect/explosion/big_explosion objects around the mob to create a visual effect, you just have to remember to call the mob's explode() proc.
Forum_account wrote:
Text

WELL GEEZ WHY DON'T YOU MAKE YOUR OWN MR. KNOWITALL. (jk, of course. :P, good suggestions)
Great tutorial for beginners.
Magnum, would you mind if I made a real "Newbie's Beginner Guide To Programming?"?

Ganing wrote:
Magnum, would you mind if I made a real "Newbie's Beginner Guide To Programming?"?

Your asking him? Why not just make it and have two guides going at once?
Make moar! :)
Truseeker wrote:
Ganing wrote:
Magnum, would you mind if I made a real "Newbie's Beginner Guide To Programming?"?

Your asking him? Why not just make it and have two guides going at once?

That's what I'm trying to get going. :P