What you are going to learn today:
Procedures and functions are the topics today, so listen up! After going through this tutorial, you will never want to give up programming again!
What are Procedures and Functions?Procedures? Functions? What? Is this some sort of calculator manual? No, procedures and functions are a feature of Pascal which allow you to repeat a certain piece of code or calculation again and again. Isn't that great? But first, we must find out what procedures and functions are in reality.
First of all, what is a procedure? Well, let's say you've got a job as a dishwasher. The process of washing a dish could be:
Now about functions. A function is just like a procedure except that it returns a value. For example, somebody may ask you to count the number of chairs in a hall. You first step in, count, then return the number of chairs in the hall back to the person who first asked you. That is a function. A function simply returns another value back into the program, such as complex calculation results, the position of the mouse cursor, or the number of times an atomic bomb had dropped onto the Earth. Functions can report almost anything. Numbers, strings, characters, anything! And if you want to, you can use functions replace procedures completely! Isn't that great?
Scope of VariablesNow that you understand the concepts, let's be honest. Everything good in life has a catch. Procedures and functions are no different, and this catch is the concept of scope. To understand this concept, visualise a stack of exercise books on your table. Everytime you call a procedure or a function, it will be placed at the top of the stack. When the procedure or function finishes, it is removed. Now, when a procedure and function goes, everything else declared in it goes too! That means that if you declare a variable in procedure x, you can't use that variable in procedure y! The variable simply goes poof, and disappears into a mist of sparkling electronic dust. However, you can use variables which were declared in lower procedures and functions in the stack. Unbelievable! Confusing, maybe, but very powerful...
How procedures and functions are implementedHow are procedures and functions implemented? Well, here's the low-down on the syntax for declaring your very own procedures and functions.
Procedures are declared using the procedure keyword, as you can see below. The name of the procedure comes first, followed by a semicolon, then the contents of the procedure.
procedure WhyMe; var i: integer; begin for i := 1 to 10 Writeln('Why Me?!'); end;A procedure is just like a separate program in itself. You can declare variables and use them within your program code. However, as described above, after the procedure ends, the variables vapourize with it. So, now that you have declared a procedure. How do you call it? Ahh, this is easy. Just type in the name of the procedure. What else could be easier?
begin WhyMe; Writeln('That was my true feelings regarding Exams'); end.When this program executes, it will print 10 "Why Me?!"s and print the final message "That was my true feelings regarding Exams". After a procedure finishes, it returns back to place where it was called. Isn't that smart? Pretty innovative.
Functions, on the other hand, cannot be used just like that because they return a value back to the program instead of just doing things. For instance, we may have a function which acts like this:
function AddEmUp : integer; begin AddEmUp := a + b + c; end;This function will return the sum of a, b, and c. Well, how does it work? The function name comes after the keyword 'function'. After the function name, you get the data type the function should return. Huh? Yes, even when using functions, you can't run away from the nearly limitless list of data types. The value the function should return is assigned to the name of the function. In this case, because the name of the function is 'AddEmUp', we assign the result of the function to the same name! How do you use functions then? Well...
begin a := 5; b := 9; c := 4; Writeln(AddEmUp); end.This program will print out the sum of a, b, and c! Marvellous use of functions, isn't it? Well, it isn't finished yet!
Procedures and functions would have been dreadfully boring and unflexible if it wasn't for a great feature called parameters. You can use parameters to pass values to the procedure or function to use. For example:
function AddEmUp2( x, y, z: integer ) : integer; begin AddEmUp2 := x + y + z; end;Then, to use this function, you must provide values for x, y, and z. This is done by using brackets.
begin Writeln(AddEmUp2(2, 3, 5)); end.In this example, x would be 2, y would be 3, and z would be 5. You're passing values over for the procedure or function to make use of. Got the idea? No? Well, here's another example.
procedure PrintEm( x, y, z: integer ); begin Writeln(x); Writeln(y); Writeln(z); end;If you pass the values 2, 3, and 5, this procedure would print out those numbers. That's great! Now you can provide information to the program on the fly. Power on the go...
The final example program...Here it is, in its full glory, the example program of the day! Buckle up, and enjoy typing this utterly simple yet powerful program.
1: program AddEmUpAgain; 2: 3: function AddEmUp( a, b, c: integer ) : integer; 4: begin 5: AddEmUp := a + b + c; 6: end; 7: 8: procedure PrintIt( a, b, c: integer ); 9: begin 10: Writeln('The sum of a, b, and c is ', AddEmUp(a, b, c), '.'); 11: end; 12: 13: begin 14: PrintIt(2, 3, 4); 15: end.
The sum of a, b, and c is 9.
Well, that was the great example program of the day! But this topic is far from over. Here are the tips and rules we couldn't fit into the text without making it ridiculously boring. (As if it wasn't already...)
The Final Chapter is comingJoy! Rapture! Divinity! (Get the joke?) Day 5 is finally over and you can go on to Day 6 - Comments and Other Neat Stuff. Neat Stuff? What neat stuff?! Well, find out for yourself. Avé!