Need help with Pascal.

Started by
9 comments, last by dave123 19 years, 5 months ago
Hey! this is a class assigment grade 11 and i need help with addding functions.

program assig_8;
uses crt;
type
        st=string;
        ch=char;
var
     number1,str2:st;
procedure enter_data (var number:st);
 begin
 writeln('Please a number');
 readln(number);
 end;

procedure err_chck (number1:st;var str1:st);
 var
    count:integer;
 begin
 str1:='';
 randomize;
 for count:= length(number1) downto 1 do
   begin
   if (ord(number1[count])<47) or (ord(number1[count])>57) and
      (ord(number1[count])<>45) and (ord(number1[count])<>13) then
     begin
     textcolor(3);
     writeln('You had Enter an Invaild Number');
     writeln('Please Re-enter the number');
     sound(random(1000));
     delay(400);
     nosound;
     textcolor(9);
     readln(number1);
     count:=length(number1) + 1;
     end
     else
      if (number1[1] < number1[3]) then
        begin
        writeln('*****ERROR*****');
        writeln('Your First Digit Must be Greater Than Your Last Digit');
        readln(number1);
        count:=length(number1) + 1;
        end
        else
         if (length(number1)>3) or (length(number1)<3) then
           begin
           writeln('You Must Enter Three Digits!');
           readln(number1);
           count:=length(number1)+1;
           end;
   end;
 str1:=str1+number1;
 end;
var
   int2:integer;
Procedure reverse (str2:st;var int1:integer);
var
     count,factor:integer;
 begin
 str2:='';
 factor:=1;
 int1:=0;
 str2:=str2[3] + str2[2] + str2[1];
 for count:= length(str2) downto 1 do
  begin
  int1:=int1 + (ord(str2[count])- 48) * factor;
  factor:=factor*10;
  end;
 writeln('reversed number = ',str2);
 end;
procedure sub (int2:integer; var tsub:integer);
var
        l_int,count,factor:integer;
        str1:st;
 begin
 factor:=1;
 l_int:=0;
 str(int2,str1);
 str1:=str1[3] + str1[2] + str1[1];
 writeln('str1 = ',str1);
 for count:=length(str1) downto 1 do
  begin
  l_int:=l_int+(ord(str1[count])-48)*factor; factor:=factor*10;
  end;
 if (int2 > l_int) or (l_int > int2) then
        tsub:=l_int - int2
 else
  if (l_int > int2) or (int2 > l_int) then
        tsub:= int2 - l_int;
  writeln('number sub = ', tsub);
  end;
var
  tsub1:integer;
procedure add (tsub1:integer);
var
        l_int,tadd,count,factor:integer;
        str1:st;
 begin
 factor:=1;
 l_int:=0;
 str(tsub1,str1);
 str1:=str1[3] + str1[2] + str1[1];
 writeln('reversed number = ',str1);
 for count:= length(str1) downto 1 do
  begin
  l_int:=l_int + (ord(str1[count])-48 )*factor; factor:=factor*10;
  end;
 writeln('total sum =', l_int + tsub1);

 end;
begin
clrscr;
enter_data(number1);
err_chck(number1,str2);
reverse(str2,int2);
sub(int2,tsub1);
add(tsub1);
readln;
end.

that's the entire program and i am not sure how to add functions...
Advertisement
could you use the [ source ] [ /source ] tags? (w/o the spaces)
it would improve readability by 138497234987%
Now get down on your hands and knees and start repeating "Open Source Good, M$ Evil", smacking your head against the pavement after each repetition. Once you have completed your training you may change your first name to GNU/, to show that you are free from the slavery of the closed source world. -Michalson
Quote:Original post by dave123
that's the entire program and i am not sure how to add functions...

Have you even tried yet?
If you know just a little Pascal, it would be very easy to add functions or to convert the procedures into functions.

A function signature in Pascal looks something like this:
<code>
function function_name (param : type) : return_type
</code>

Ya.......and it doesn't work!
Quote:Original post by dave123
Ya.......and it doesn't work!

This is not very informative.
What exactly have you tried?
What do you mean by 'it doesn't work'? (compililation errors, program not running the way you expected it to, etc.)
This is not a forum for homework, and it is quite obvious that you signed up specifically to do that.

Try phrasing your question a little better. As it is, you have simply dumped some poor source code, given a vague indication of what your teacher wanted, and asked for someone to do the work. Ask specific questions about what you are having trouble with in Pascal, show what work you have done (that means what parts of this do you already understand, and what parts are you having trouble on), and then you'll get some help.

BTW, what would your school be?
lol.........all i am asking is how do i add a function
i just want to see how it is done...so can you guys like convert 1 procedure of the program in to a function so i can the basic idea of how i am suppose to write it...PLEASE..
Yes. And all we're asking is that you prove that you deserve to be told. It's kind of a thing around here. If you don't meet us half way and prove you tried, then you're not going to get an answer.

Incidentally, if you'd tried Google, you'd have definitely seen examples of how to do functions in Pascal by now.

-Auron
from what I understand of Pascal, functions are exactly the same as procedures, except they have return types, simply give them a return type and change "procedure" to "function", and as the person above stated: [google](click the image)
Hi Dave,

I've replaced the enter_data procedure you use with
the function enter_data. Here is the code:

program assig_8;uses crt;type        st=string;        ch=char;var     number1,str2:st;{Comment this procedureprocedure enter_data (var number:st); begin writeln('Please a number'); readln(number); end;}{ Add a function instead --> }function enter_data: st;var number: st;begin  writeln('Please a number'); readln(number);  enter_data := number;end;procedure err_chck (number1:st;var str1:st); var    count:integer; begin str1:='; randomize; for count:= length(number1) downto 1 do   begin   if (ord(number1[count])<47) or (ord(number1[count])>57) and      (ord(number1[count])<>45) and (ord(number1[count])<>13) then     begin     textcolor(3);     writeln('You had Enter an Invaild Number');     writeln('Please Re-enter the number');     sound(random(1000));     delay(400);     nosound;     textcolor(9);     readln(number1);     count:=length(number1) + 1;     end     else      if (number1[1] < number1[3]) then        begin        writeln('*****ERROR*****');        writeln('Your First Digit Must be Greater Than Your Last Digit');        readln(number1);        count:=length(number1) + 1;        end        else         if (length(number1)>3) or (length(number1)<3) then           begin           writeln('You Must Enter Three Digits!');           readln(number1);           count:=length(number1)+1;           end;   end; str1:=str1+number1; end;var   int2:integer;Procedure reverse (str2:st;var int1:integer);var     count,factor:integer; begin str2:='; factor:=1; int1:=0; str2:=str2[3] + str2[2] + str2[1]; for count:= length(str2) downto 1 do  begin  int1:=int1 + (ord(str2[count])- 48) * factor;  factor:=factor*10;  end; writeln('reversed number = ',str2); end;procedure sub (int2:integer; var tsub:integer);var        l_int,count,factor:integer;        str1:st; begin factor:=1; l_int:=0; str(int2,str1); str1:=str1[3] + str1[2] + str1[1]; writeln('str1 = ',str1); for count:=length(str1) downto 1 do  begin  l_int:=l_int+(ord(str1[count])-48)*factor; factor:=factor*10;  end; if (int2 > l_int) or (l_int > int2) then        tsub:=l_int - int2 else  if (l_int > int2) or (int2 > l_int) then        tsub:= int2 - l_int;  writeln('number sub = ', tsub);  end;var  tsub1:integer;procedure add (tsub1:integer);var        l_int,tadd,count,factor:integer;        str1:st; begin factor:=1; l_int:=0; str(tsub1,str1); str1:=str1[3] + str1[2] + str1[1]; writeln('reversed number = ',str1); for count:= length(str1) downto 1 do  begin  l_int:=l_int + (ord(str1[count])-48 )*factor; factor:=factor*10;  end; writeln('total sum =', l_int + tsub1); end;beginclrscr;{ Here we call the function }  number1 := enter_data;{ The return value of enter_data function placed to the variable number1 }err_chck(number1,str2);reverse(str2,int2);sub(int2,tsub1);add(tsub1);readln;end.


the procedure definition:
procedure enter_data (var number:st);

changed to
function enter_data: st;

Inside the function code the instruction
enter_data := number;
is acctually the code that give the return value of the function.

In main program the instruction
number1 := enter_data;

place to the variable number1 the return value of the function.

I hope this is helpful.

This topic is closed to new replies.

Advertisement