Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 A Little Tutorial About Procedures
Index -> Programming, Turing -> Turing Tutorials
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Hackmaster




PostPosted: Wed Nov 01, 2006 8:33 pm   Post subject: A Little Tutorial About Procedures

Hi everyone, and welcome to not only my first tutorial, but my first post entirely. i have a decent programming backround (3 years in C) but this year, i am taking a grade 10 course on Turing. Most people think that turing is a usless language; one you can learn something better, you should. that is really not the case. turing is a fully functional language that can be used for a multitude of things. But, if you are a member of compsci.ca, i'm sure you know that already:D.

I lot of people in my programming class have lots of problems with procedures. this is understandable. They aren't the easiest things to learn, but they aren't the hardest, either. let's start with some sample code.

code:


% this program asks you your age, and tells you what it is in dog years.

var age : int
var dog : int

put "enter your age: "..
get age

dog:=age*7

put "you are ", dog ," years old in dog years."



This is a VERY simple program. You might have learned about this in your first week on the Turing scene, as it has no scary commands, just put, get, and some math. not a problem. Now, let's make this simple program more complicated then it really needs to be!! Introducing...

THE PROCEDURE!!!

ya. great. so what has it done for me lately?

procedures exist simply to make life easier for programs that are huge. procedures act almost like a loop. you declare that one is going on, you put code into it, and then you end it. now, whenever you call that procedure, it does the code inside it.

Huh? Shocked

Let me demonstrate.

code:


procedure hi
     put"hello user. how are you today?"
end hi

hi



wierd, right? can you guess what the output is? (without looking a few lines below?)


It would look something like this:

Quote:


hello user. how are you today?



great. so what exactly did it do?

It's easy. you made a procedure, and it was named "hi". all it did was say hi to the user. later on, in the bulk of the program, you called it. then, all it did was exactly what was inside of it, namely say hi. then it ended. simple.

procedures like this exist all over turing, all over any language. take 'put' for example. you give it a string, and it displays it on the screen. you have to call it first though, as with any command.

isn't this cool? do you realize what it means???

it means that you can make your own functions. it means that you can make the computer do things that you tell it to do in a nice, clean, way. but wait! there's more...

Procedures have one problem though. what happens if you want your procedure to do math. you get input from the user. but how do you pass that to the procedure? easy. you simple tell it to require parameters. Shocked

hmm.... code time!

code:


var input : int
var answer : int

procedure doMath (number : int)

answer:=number*5

end doMath

put"Hi! enter in a number, and i'll multiply it by 5: "..
get input

doMath(input)

put input," times 5 is ", answer



cool! what happened?

1. you made a procedure called 'doMath' which required an int as input.
2. you asked the user for input.
3. you called your function with the variable 'input' as the int to be passed.
4. doMath did math.
5. since doMath assigned it's result to a variable called 'answer', you can now use that in your main program, and you do. easy-peasy.

so our first program. how do we optimize that for procedures? you figure it out. then, look below for the answer. if you've got that, then you have procedures down.


code:


% this program asks you your age, and tells you what it is in dog years.

var age : int
var dog : int

procedure dogYears(AgeToDog : int)

     dog:=AgeToDog*7

end dogYears

put "enter your age: "..
get age

put "you are ", dog ," years old in dog years."



See? procedures are easy. also, as a side note, you can ask for multiple parameters in a procedure, like this:

code:


procedure doStuff(a,b,c,d : int, blah : string)

%stuff would go here

end doStuff



that's all. please, tell me what you all think of my first FAQ!
Sponsor
Sponsor
Sponsor
sponsor
Clayton




PostPosted: Wed Nov 01, 2006 10:15 pm   Post subject: (No subject)

Nice try, however we already have a tutorial on this subject here

If you wish to create a tutorial, check out the Improving Tutorials Thread for tutorials that need to be improved upon and written entirely.

Hackmaster wrote:
turing is a fully functional language


Pardon? Turing is not a functional programming language, in that it doesn't fit the programming paradigrim of "Functional Programming". For more check out Wikipedia on the subject.

EDIT: Your doMath, and dogYears procedures would be better off as a function. It is better coding practice, as it doesn't mercilessly change a variables value as your procedures do. Also, you say that you are going to show how to optimize your program further down the page, I didn't see anything that showed optimization....

EDIT2:
Hackmaster wrote:

procedures like this exist all over turing, all over any language. take 'put' for example. you give it a string, and it displays it on the screen. you have to call it first though, as with any command.


Not quite...

put is a keyword that activates a section of code somewhere in a far off library in a jumbled mess of code. In a way put is like a procedure because you are passing it something to "put", in another way, it's not a procedure because the following is perfectly legal:

Turing:

put "12"
put 12
put true
put 0.3


Because Turing doesn't support function overloading, if put were a procedure, it would cause Turing to crash.

In conclusion, put resembles a procedure, but isn't quite.

/me readys for some kind of backlash comment now...
Silent Avenger




PostPosted: Wed Nov 01, 2006 10:26 pm   Post subject: (No subject)

I too have one more issue about your post. You said you had 3 years of background in C and you also said that you are taking grade 10 compsci. So does this mean you've been programming in C since you were in grade 7 or are you in a higher grade but just taking grade 10 compsci? I personally don't know of any people who have even started programming in grade 7 and I know some pretty smart people.
[Gandalf]




PostPosted: Wed Nov 01, 2006 11:25 pm   Post subject: (No subject)

Such negative people...

Silent Avenger, maybe you'd be surprised to find out that I started programming in, oh, grade 4 or 5 if I remember correctly. Not seriously, but programming nonetheless. And I'm sure I'm not the only one.
Freakman wrote:
/me readys for some kind of backlash comment now...

Darn straight. Wink
Freakman wrote:
Hackmaster wrote:
turing is a fully functional language


Pardon? Turing is not a functional programming language, in that it doesn't fit the programming paradigrim of "Functional Programming". For more check out Wikipedia on the subject.

Sure, he's probably not aware of what functional programming is, but putting the word "functional" beside "language" doesn't neccessarily put it in that context. If you read it more carefully it becomes obvious he meant "functional" as in "doing what it's supposed to" or "working".

Freakman wrote:
EDIT: Your doMath, and dogYears procedures would be better off as a function. It is better coding practice, as it doesn't mercilessly change a variables value as your procedures do.

Probably, but this is a tutorial on only procedures, so that wouldn't be covered in it's scope. That is probably the one main problem with it, functions and procedures should be taught together in order to avoid later confusion.

Freakman wrote:
Because Turing doesn't support function overloading, if put were a procedure, it would cause Turing to crash.

Yes, Turing doesn't support subroutine overloading, but C/C++ does. And as you said, put is hidden away in the depths of Turing's C++ source code, so it can be overloaded.

Alrighty then. Nice first post, by the way, especially comparing it to a lot of others...
Hackmaster




PostPosted: Thu Nov 02, 2006 8:49 am   Post subject: (No subject)

Hey Guys,

Thanks for the critisim, really. i am glad that this first tutorial actually got some good critisim, and quite frankly, it takes multiple attempts to not get rejected by a publisher. By the way, i am in grade 10, and i did start programming when i was in grade 7. Also, i did not realize there was already a tutorial on procedures. also, i am still getting used to the inner workings of turing as opposed to C, and i think this is a good first attempt. My next tutorial will be attempting to revamp the if...elsif...else thing that cervantes said needed an overhaul. i don't know if it's been done... but i suppose to sources of reference are better than one! thanks guys!
Tony




PostPosted: Thu Nov 02, 2006 11:36 am   Post subject: (No subject)

You should talk to Cervantes, he's in charge of guidelines for quality tutorial content.

I hope that you contribute more. We are very picky about tutorial content and will critic you plenty. Though you'll definitly learn a lot from such experiences Wink

Btw, I've been programming in grades 5~6, but then I moved and computers fell out of my school curiculum until grade 10.

Amailer has also started programming at an early age Wink
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
wtd




PostPosted: Thu Nov 02, 2006 1:35 pm   Post subject: (No subject)

On a slightly different note, I would encourage Hackmaster to check out other languages. Aside from anything I could say about Turing's failings as a language...

C is a manifestly-typed, procedural programming language. So is Turing. You may wish to check out a broader range of programming languages. Smile
Silent Avenger




PostPosted: Thu Nov 02, 2006 4:41 pm   Post subject: (No subject)

[Gandalf] wrote:
Such negative people...

Silent Avenger, maybe you'd be surprised to find out that I started programming in, oh, grade 4 or 5 if I remember correctly. Not seriously, but programming nonetheless. And I'm sure I'm not the only one.


Well now I know of two poeple who have started programming before highschool and I'm sure there are many more. I swear if you ask anyone in my school who does computer programming (even the teachers) they'll say they didn't start until grade 10 or 11 so that's why I said I don't know anyone personally that started at grade 7.
Sponsor
Sponsor
Sponsor
sponsor
Cervantes




PostPosted: Thu Nov 02, 2006 7:16 pm   Post subject: (No subject)

Hi Hackmaster, welcome to the forum!

I just love it when people contribute something good on their first post, rather than asking a total n00b question. Heh.

Anyways, here's my critique:
As has been mentioned, procedures really need to be taught in conjunction with functions so that the student can see the differences between them, see the similarities between them, and understand when to use functions and when to use procedures. Most importantly, it needs to be stressed that functions are way better.

I personally think the idea of parameters could be introduced in a better way. Here's how I would do it: I'd show the student a few procedures that do very similar things. I would highlight the parts that are similar, highlight the parts that are different. Then I would explain that we can generalize this set of procedures down to a single procedure. This serves to let us write less code as well as cover more possibilities. I would then illustrate how such a generalization can be made by replacing the differences in the procedures with a variable. Then I'd add parameters.

It really pains me to see these doMath and dogYears procedures modify a global variable. doMath and dogYears are way better suited to being functions. There are plenty of good uses for procedures, so some better examples would be good.

Also, your dogYears code is missing a line:
Hackmaster wrote:

code:

% this program asks you your age, and tells you what it is in dog years.

var age : int
var dog : int

procedure dogYears(AgeToDog : int)

     dog:=AgeToDog*7

end dogYears

put "enter your age: "..
get age

put "you are ", dog ," years old in dog years."


This crashes, since you never actually called the dogYears procedure. Wink


Hackmaster wrote:
My next tutorial will be attempting to revamp the if...elsif...else thing that cervantes said needed an overhaul. i don't know if it's been done... but i suppose to sources of reference are better than one! thanks guys!

I'd prefer if you tackled another one, simply because that was has some versions in progress. I was working on it a long time ago (like, 11 months ago...) and got half finished. I'd pick it up again, if not for the fact that Ultrahex made a rather nice one. Now where'd that go? As I recall, it's almost done.
Clayton




PostPosted: Fri Nov 03, 2006 7:58 am   Post subject: (No subject)

errrr... It can be found here. For reasons best known to himself he posted it in v3 Very Happy
BenLi




PostPosted: Sun Nov 05, 2006 10:09 pm   Post subject: (No subject)

as cervantes said, procedures are best taught in conjuction with functions. And yes, most of your examples should be functions, but they served their purpose as an example. Note that you can create variable within your procedure. Even if you already declared it, a variable declared within a procedure will take greater priority, and as far as i know, i won't mess with the variable you already created, but don't quote me on it...
[Gandalf]




PostPosted: Sun Nov 05, 2006 10:38 pm   Post subject: (No subject)

BenLi wrote:
as cervantes said, procedures are best taught in conjuction with functions.

I believe, at least within this topic, that I said this first. Razz

And you're right:
code:
var stuff := "foo"
proc bar
    var stuff := "baz"
    put stuff
end bar
bar
Display posts from previous:   
   Index -> Programming, Turing -> Turing Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 12 Posts ]
Jump to:   


Style:  
Search: