[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Writeing Procedures and Functions

Posted on 2000-02-29
4
Medium Priority
?
278 Views
Last Modified: 2010-04-16
Can some one please explain in a way that is understandable about how to write functions and procedures?  I need it for a college assignment and our teacher is not very good at his job.  If you can give some examples for me it would be a good help.  Its supposed to be for an electricity company to calculate customers bills.  I don't want it doing for me just enough info so that I can teach myself.  Thanks :0)
0
Comment
Question by:3_Armed_Human
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 3

Expert Comment

by:Alisher_N
ID: 2574293
procedures and functions use the same approach and have a little difference (explained later), basically it is a block or part of program which can be 'called' many times from different parts of 'main' program. the structure is:

procedure p1

procedure p2

{main program starts here}
x=0 {some code}
call procedure p1
y=4 {some code}
call procedure p2
....
x=y+3 {another main part's code}
call procedure p1
etc etc

(this is pseudocode, just for explanation ;-)

ok, here is a 'life example': imagine you have 3 friends and you want to invite them for party, your 'main program' will be
buy food & booze ;-)
invite Peter
invite Adam
invite George
begin party
end party

this means that you have to do the same actions 3 times... which actions ? - inviting ! in this example it would be:
find a phone number of X person
pick up phone
dial that number
ask for X
say 'hi'
tell him about party
return to main program ;-)

you just have replace X with different names 3 times, but actions the same, this why convenient to use procedures. Now a program example, you have to print Y values for 3 different X using formula Y = X * 2 + 1, where Xs are 1,4,6  so the program could be:

Procedure PrintY( X : Byte );
begin
  writeln( X * 2 + 1 );
end;

begin {main}
  PrintY( 1 );
  PrintY( 4 );
  PrintY( 6 );
end.

this is very simple example and not very practical, but you have to understand how execution of program flows, you start in main, PrintY(1) is a call to procedure with 'parameter', so program 'jumps' inside PrintY code, executes using passed parameter, then returns back and continues to PrintY(4).
parameters maybe of any type... you can also pass some values calculated in procedure back to main program, the difference between proc. and funct. is that function always has a result, but proc. - not, so calling is different:
myprocedure(parameters);  {this is call to procedure}
x:=myfunction(parameters); {this is call to function}
honestly in Delphi you can call functions without asigning their result, as procedure, but this is another story....

got it ?


0
 
LVL 49

Accepted Solution

by:
dbrunton earned 300 total points
ID: 2575434
Different viewpoint on procedures and functions.

If you were outputting a song that had four verses and a chorus that was repeated between verses and repeated twice at end of song you could create procedures for each verse and for the chorus and assemble as follows.

{* each of these procedures is full of writeln('this is line') or similar *}

procedure verse1;

procedure verse2;

procedure verse3;

procedure verse4;

procedure chorus;

begin
  verse1;
  chorus;
  verse2;
  chorus;
  verse3;
  chorus;
  verse4;
  chorus;
  chorus;
end.


It is possilbe to pass information into the procedures - these are called parameters.

procedure verse5(no : integer);
begin
  if no = 4 then
    writeln('Life is wonderful')
  else
    writeln('Life is hell');
end;

It is possible to get information out of procedures.  Note the var

procedure dble(var no : integer);
begin
  no := no *2;
end;

The above procedure doubles any number passed into it
eg

number := 4;
dble(number);
writenl(number);

Functions are designed to return a value.

function triple(no : integer) : integer;
var
  i : integer;
begin
  i := no * 3:
  tripe := i;
end;

The above function returns the triple of any number passed to it.

number := 4;
newnumber := triple(number);
writeln(newnumber);

These are basic procedures and functions.  More complicated variations exist but these are enough for a starter.

How you use and design them depends on what you are required to code.



0
 
LVL 1

Expert Comment

by:ntdragon
ID: 2581648
did you understand or not???
0
 

Author Comment

by:3_Armed_Human
ID: 2582869
Thanks alot, a great help @:0)
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

WooCommerce is becoming the most powerful e-commerce plugin for Wordpress. And why not. The platform comprises of numerous core plugins that may come in handy, powerful options to make your website development task much easier.
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question