How do you Print a Procedure?

Hi,
I'm a beginning programmer who is having some difficulties.
I'm wondering how you can call a procedure to output the values of its' variables to a text file instead of to the screen. I know how to output information to a text file but I need to print variables from a procedure without writing out all of the statements from the procedure to the main program again. Please show me a small example if this is possible.  

Thanks,

Pyramid
PyramidAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

omsecCommented:
There are two types of variables, called LOCOAL VARIABLES used in a procedure or a function. The first ones are the parameter-variables you give as arguments to the procedure, and the second ones are internal variables of the procedure.

procedure WriteToFile(YourName : String);

Var
  YourCode : Byte;

You see, the variable YourName is an argument variable, the parameter of the procedure and YourCode is an internal, local variable.

Note, you can't access local variables from any others points in your programs. They're only avaiable in their procedures or functions, this is why they are called local variables. the opposit of a local variable is a global variable.

So ok, enough of "book stuff"

below you see a procedure, that writes some variables and constants into a file, and how to call that from a program :


Program Examples;

Uses
  Crt;

Var
  WriteTo : String;

Procedure WriteToFile(FileName : String);

Var
  FirstName  : String[20];
  FamilyName : String[20];
  Age        : Byte;
  FileHandle : Text;

Begin
  WriteLn;
  WriteLn('This text comes from the procedure..');
  WriteLn;
  WriteLn('Enter your Name : ');
  ReadLn(FirstName);
  WriteLn;
  WriteLn('Enter your Family Name : ');
  ReadLn(FamiliyName);
  WriteLn;
  WriteLn('Enter your Age : ');
  ReadLn(Age);
  Assign(FileHandle, FileName);
  Rewrite(FileHandle);
  WriteLn(FileHandle);
  WriteLn(FileHandle, 'This is an auto-generated file.');
  WriteLn(FileHandle);
  WriteLn(FileHandle, 'His/Her Name is ',FirstName);
  WriteLn(FileHandle, 'His/Her Family Name is ',FamilyName);
  WriteLn(FileHandle, 'His/Her Age is ',Age);
  WriteLn(FileHandle);
  Close(FileHandle);
  WriteLn;
End;

Begin
  WriteLn;
  WriteLn('Enter the file you wanna write in...');
  ReadLn(WriteTo);
  WriteToFile(WriteTo);
End.

That's all. If you need some others, like reading writing binary files or you need additional info, u may leave a comment. I didnt include any IO (input - output checks) to keep the example code small.

Hope that helps, greets, Omsec
0
PyramidAuthor Commented:
Thanks for the reply!

I just need a little extra. In your example, if I wanted say, just the first & last name written to the screen and all of it written to a text file, how would I do this using the same procedure? Again, if this is possible.

Basically, if I was using this procedure to calculate the results of 3 variables x,y,z and I had to make 2 calls from the main program to this procedure( 1st call to print x and y to screen and 2nd call to print x,y,z to a text)how could I do this without printing x,y,z to the screen.

I need to find the most efficient way of using a procedure without duplicating its contents throughout the program.

Thanks in advance!

Pyramid
0
nibblerCommented:
Have you ever heard of redirection ??
You must NOT use crt, (or set the variable "direct video" to false).

example, if you executable is test.exe you could enter this:

test > output.txt

In the file output.txt you will see the results you would otherwise see on the screen. The values in the writeln statements will be put into the file..

I hope this is the correct answer.. (I may have misunderstood your question !!)
0
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

omsecCommented:
i dont think that is what he asked
0
PyramidAuthor Commented:
Hi!

Thanks for the reply, Nibbler. To be honest with you, I really have no idea what you are talking about. I don't know that much about pascal. Omsec's reply is a lot closer to the answer I'm looking for. I do appreciate your help, though. If anybody needs any clarification on this problem please ask me.

Thanks,

Pyramid
0
jlove1Commented:
Here's an answer I think you may enjoy. I don't know if it's EXACTLY what you're asking for, so I'll include 2 answers.

here's the first

type
mytype = object
 Variable : string;   {or whatever type}
 tofile : boolean;     {this chooses weather or not it will output to file}
 display : boolean;   {This chooses weather or not it will output to screen}
 end;
var
test1,test2,test3 : mytype ; {creates 3 of my objects}
procedure doit(var1 , var2 ,var3 : mytype) ;

var
f : text;
filename : string;
begin;
filename := 'testfile.txt';
assign(f,filename);
rewrite(f);
{do whatever you have to do here}

{...}

if var1.tofile then writeln(f,var1.variable);
if var1.display then writeln(var1.variable);

if var2.tofile then writeln(f,var2.variable);
if var2.display then writeln(var2.variable);

if var3.tofile then writeln(f,var3.variable);
if var3.display then writeln(var3.variable);

close(f);
end;

begin
test1.variable := 'this is some To File  ONLY DATA';
test1.tofile := true;
test1.display := false;

test2.variable := 'This is some Display ONLY DATA';
test2.tofile := false;
test2.display := true ;

test3.variable := 'This will be on Screen and in File';
test3.tofile := true;
test3.display := true;

doit(test1,test2,test3);

readln
end.


That's one way to do it using an OBJECT.
In the above example, objects (collections of variables) are passed to the function DOIT()
The properties of the object (variable,tofile, and display) are described
variable : String The data part of the object.
Tofile : Boolean value -- Set to TRUE will output to file FALSE will not.
Display : Boolean Value -- Set to True will output to screen, False will not.



the next example is a simple one, and I don't think this is what you're asking, but it could be.

procedure (somestring : string; someotherstring, whatever : string);
var
f : text;

begin
{Somestring will go to the SCREEN ONLY}
{Someotherstring will go to the file only}
{Whatever will go to BOTH}
assign(f, "testfile");
rewrite(f);
writeln(somestring);

writeln(f,someotherstring);

writeln(f,whatever);
writeln(whatever);
end;


there you go.

if you're asking for a "one line" way of printing something to the screen AND to a file try this.

procedure screenandfile( myvar : string);
var
f : text
begin;
assign(f,"testfile");
rewrite(f);
writeln(myvar);
writeln(f,myvar);
end;

Then in your program use screenandfile() instead of writeln()..

I hope this lengthy explanation is all that you ever wanted to know and more about printing to the screen and to a file. Let me know if I can be of further assistance

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PyramidAuthor Commented:
Hi!

Your answer seemed to work ok with a few modifications but it did work.

Thanks,

Pyramid
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Pascal

From novice to tech pro — start learning today.