Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Procedures

pascal

  • Please log in to reply
16 replies to this topic

#13 smith427

smith427

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 27 April 2010 - 04:03 AM

where are the variables which you declared in function ?
PROCEDURE Compute_Surface_Area (VAR Radius, Surface_area_of_cylinder, Length:
you miss ) in the end of this.
calling procedure :
name(variables);


I am trying to make this code work and am having difficulty.

How do you pass the parameters? I moved the writeln and readln out of the procedure down to the main body of the program so that it will prompt for the variable entry by the user, then pass it to the procedures, but I'm getting erros.

This is what I have so far:

PROGRAM Computes (Volume, Surface_Area);

CONST
Pi = 3.142;

VAR
Radius, Length, Cylinder_Vol, Cylinder_Surf:
Real;

PROCEDURE Compute_Volume

(VAR Radius, Length, Cylinder_Vol:
Real);

BEGIN

Cylinder_Vol := Pi * Radius * Radius * Length;
Writeln(‘Cylinder_Vol: ‘, Cylinder_Vol);
Realn();
END;

PROCEDURE Compute_Surface
(VAR Radius, Length, Cylinder_Surf:
Real);

BEGIN

(* Writeln(‘Enter the radius of the cylinder.’);
Readln(Radius);
Writeln(‘Enter the length of the cylinder.’);
Readln(Length); *)
Cylinder_Surf := 2 * Pi * Radius * (Length + Radius);
Writeln(‘The surface area of the cylinder is: ‘, Cylinder_Surf);
Readln();
END;

BEGIN //main program Computes

Writeln(‘Enter the radius of the cylinder.’);
Readln(Radius);
Writeln(‘Enter the length of the cylinder.’);
Readln(Length);
Compute_Volume( Radius, Length );
Compute_Surface( Radius, Length );


END.
  • 0

#14 smith427

smith427

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 27 April 2010 - 10:21 AM

here is what I have now that is working. How do I limit the decimal points on the output?

PROGRAM Computes (Volume, Surface Area);

CONST
Pi = 3.142;

VAR
Radius: Real;
Length: Real;


procedure Compute_Volume (Radius, Length:Real) ;
BEGIN
Writeln(‘The Volume of the cylinder is: ‘, Pi * Radius * Radius * Length);
END;

procedure Compute_Surface (Radius, Length:Real);
BEGIN
Writeln(‘The surface area of the cylinder is: ‘, 2 * Pi * Radius * (Length + Radius));
END;


BEGIN //main program Computes

Writeln(‘Enter the radius of the cylinder.’);
Readln(Radius);

Writeln(‘Enter the length of the cylinder.’);
Readln(Length);

Compute_Volume(Radius,Length);
Compute_Surface(Radius,Length);


END.
  • 0

#15 Firebird_38

Firebird_38

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 117 posts

Posted 03 May 2010 - 12:47 PM

First off, this is not good. You need a function to calculate the values, not a procedure. But that's beside the point, because you're obviously not versed enough yet.

Your parameters (inside the () as in procedure Something_Silly(var thisparam:Real); shouldn't be there. Parameters are there to pass info to the procedure, and/or (if using var) getting information back out.
You don't need the information anywhere else except inside the procedure, after all, you get input, calculate, and output inside the procedure. So no other part of your program needs this info.

So, use NO params. Write

procedure MyProc;
begin
//your STUFF here
end;

Then call it.
begin
MyProc;
end.

But that's a bad solution... It's better to ask for radius and length once and then give both answers. Obviously.

your program part should read:
var radius,length:single; //Don't use reals, they're dead... and have been for some 10 years at least
begin
 write('Radius? '); readln(radius);
 write('Lengh? ')); readln(length);
 writeln('Thinking....');
 Delay(1000); //use sleep(1000) in windows
 writeln('Still thinking, hold on please, don''t rush me!');
 Delay(1000); //use sleep(1000) in windows
 WriteLn('The volume is: ',CylVol(radius,length));
 WriteLn('The surface area is: ',CulSurf(radius,length));
end.

Then you have to make functions:

function CylVol(radius,length:single):Single;

and

function CylSurf(radius,length:single):Single;

if you've paid attention in math, you may know the "sin" function to make a sine... sin(30) means the same thing as 0.5, just like (1+1) means the same thing as 2.
So, CylVol(2,3) means the same thing as 12 (except that my numbers are off.... :()

Anyway, to get such a function to return the right value:

function CylVol(radius,length:single):Single;
begin
 CylVol:=radius*500+length*12+length*radius+radius*radius*length;
end;

My formulas may be a bit off, but then again, this isn't my homework...

You may get errors compiling this, I didn't (compile it). It should be helpful, but in the end, you'll still have to do some of the work yourself.

Good luck.
  • 0

Posted Image


#16 Firebird_38

Firebird_38

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 117 posts

Posted 03 May 2010 - 12:53 PM

Sorry, I hadn't noticed the other page :) lol.
Anyway, to format floats using writeln, just write:

WriteLn(1.23456789:10:2);

that'll write 1.23

the "10" is the precision. Generally, numbers won't be printed bigger than that.
the "2" is the decimals. Rounding is done.
  • 0

Posted Image


#17 smith427

smith427

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 04 May 2010 - 08:17 AM

Firebird, thanks for your response. I figured out the decimal thing, the book was good for something LOL. I turned this in last week.
  • 0





Also tagged with one or more of these keywords: pascal

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download