Pyramid
asked on
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
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
ASKER
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
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
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 !!)
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 !!)
i dont think that is what he asked
ASKER
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi!
Your answer seemed to work ok with a few modifications but it did work.
Thanks,
Pyramid
Your answer seemed to work ok with a few modifications but it did work.
Thanks,
Pyramid
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