pascal functions help 
Author Message
 pascal functions help

I know that the following program is really awful but was hoping that
someone could offer me some help/suggestions on how to straighten it
out.  I am trying to compose a program which uses a module and function.
The module is supposed to get an ID number and the function is supposed
to determine whether that value falls between 100 and 799 and return a
value so that the module can print a 'valid' or 'invalid' response.  Any
help would be appreciated as I am having trouble with this.  Thanks
Mike

Program functionb;
procedure getID;
var ID:integer;
begin
   writeln('Enter an ID number ');
   readln(ID);
   validation;
   if valid=1 then
      writeln('valid') else
          writeln('invalid');
end;
function validation;
var valid :integer;
begin
   if (ID>99) AND (ID<800) then
      valid:= 1 else
         valid:=0;
end;
begin
   getID;
end.

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Wed, 18 Jun 1902 08:00:00 GMT  
 pascal functions help
This should work, but it is not really clean code. You have to declare
the validation function before the getIT procedure, because of the
scope.To be able to call a procedure/function, this procedure/function
has always been declared prior to the calling procedure/function (I am
not sure, if Turbo Pascal already offers it, but you could use the
forware modifier to change this behaviour). Further you have to declare
your variables globabally, because otherwise you cant access them in the
other procedure/function, as they are out of scope.

Program functionb;

var
ID:integer;
valid :integer;

procedure validation;   {since you are not returing any value this must
be a procedure}
  begin
    if (ID>99) AND (ID<800) then valid:=1 else valid:=0;
  end;

procedure getID;
  begin
    writeln('Enter an ID number ');
    readln(ID);
    validation;
    if valid=1 then writeln('valid') else writeln('invalid');
  end;

begin
   getID;
end.

Alex
--
Author of the free Chatsystem PINO! || Website: http://www.alcomp.net
Available at http://pino.cjb.net    || Chat   : pinochat.dhs.org:8080



Wed, 18 Jun 1902 08:00:00 GMT  
 pascal functions help
If you use a function you are assigning the result of the function to the
function name. This being the case you must assign the function name to a
variable in an expression eg. variable := function_name(parameters)

Here is one of many possible solutions, it could be done quicker but this
way helps you practice the difference between functions and procedures,

Program program_name (input, output);

VAR id_num : integer;
         id_result : boolean;

Procedure procedure_name (VAR id : integer);

Begin { procedure }
  Write(' Enter ID number ');
  Readln(id);
End; {procedure}

Function function_name(id_num : integer): boolean;
begin {function}
IF (id_num > 99) AND (id_num < 800) THEN
  function_name := true
    else
  function_name := false;
End; {function}

Begin {main}
  procedure_name(id_num);
  id_result := function_name(id_num);     { assigns the function value in an
expression }
IF id_result THEN
  writeln('valid')
ELSE
  writeln('invalid');
End. {main}


Quote:
> I know that the following program is really awful but was hoping that
> someone could offer me some help/suggestions on how to straighten it
> out.  I am trying to compose a program which uses a module and function.
> The module is supposed to get an ID number and the function is supposed
> to determine whether that value falls between 100 and 799 and return a
> value so that the module can print a 'valid' or 'invalid' response.  Any
> help would be appreciated as I am having trouble with this.  Thanks
> Mike

> Program functionb;
> procedure getID;
> var ID:integer;
> begin
>    writeln('Enter an ID number ');
>    readln(ID);
>    validation;
>    if valid=1 then
>       writeln('valid') else
>           writeln('invalid');
> end;
> function validation;
> var valid :integer;
> begin
>    if (ID>99) AND (ID<800) then
>       valid:= 1 else
>          valid:=0;
> end;
> begin
>    getID;
> end.

> Sent via Deja.com http://www.deja.com/
> Before you buy.



Wed, 18 Jun 1902 08:00:00 GMT  
 pascal functions help
Heres an easier one.....

Program program_name (input, output);

VAR id_num : integer;
         id_result  : integer;
Procedure procedure_name (VAR id : integer);

Begin { procedure }
  Write(' Enter ID number ');
  Readln(id);
End; {procedure}

Function function_name(id_num : integer): integer;
begin {function}
IF (id_num > 99) AND (id_num < 800) THEN
  function_name := 1    else
  function_name := 0;
End; {function}

Begin {main}
  procedure_name(id_num);
  id_result := function_name(id_num);     { assigns the function value in an
expression }
IF id_result = 1THEN
  writeln('valid')
ELSE
  writeln('invalid');
End. {main}


Quote:
> I know that the following program is really awful but was hoping that
> someone could offer me some help/suggestions on how to straighten it
> out.  I am trying to compose a program which uses a module and function.
> The module is supposed to get an ID number and the function is supposed
> to determine whether that value falls between 100 and 799 and return a
> value so that the module can print a 'valid' or 'invalid' response.  Any
> help would be appreciated as I am having trouble with this.  Thanks
> Mike

> Program functionb;
> procedure getID;
> var ID:integer;
> begin
>    writeln('Enter an ID number ');
>    readln(ID);
>    validation;
>    if valid=1 then
>       writeln('valid') else
>           writeln('invalid');
> end;
> function validation;
> var valid :integer;
> begin
>    if (ID>99) AND (ID<800) then
>       valid:= 1 else
>          valid:=0;
> end;
> begin
>    getID;
> end.

> Sent via Deja.com http://www.deja.com/
> Before you buy.



Wed, 18 Jun 1902 08:00:00 GMT  
 pascal functions help
Even an easier (and shorter) one...

{-----------------------}
program procedures_and_functions;

var id_num : integer;

procedure input_name (varid : integer);
begin { procedure }
  write(' Enter ID number ');
  readln(id);
end; {procedure}

function check_name(id_num : integer): integer;
begin {function}
  check_name := (id_num > 99) and (id_num < 800);
end; {function}

begin {main}
  input_name(id_num);
if check_name(id_num) then
  writeln('valid')
else
  writeln('invalid');
end. {main}

{-----------------------}

-------------------------------------------

Quote:
> Heres an easier one.....

> Program program_name (input, output);

> VAR id_num : integer;
>          id_result  : integer;
> Procedure procedure_name (VAR id : integer);

> Begin { procedure }
>   Write(' Enter ID number ');
>   Readln(id);
> End; {procedure}

> Function function_name(id_num : integer): integer;
> begin {function}
> IF (id_num > 99) AND (id_num < 800) THEN
>   function_name := 1    else
>   function_name := 0;
> End; {function}

> Begin {main}
>   procedure_name(id_num);
>   id_result := function_name(id_num);     { assigns the function value in
an
> expression }
> IF id_result = 1THEN
>   writeln('valid')
> ELSE
>   writeln('invalid');
> End. {main}



> > I know that the following program is really awful but was hoping that
> > someone could offer me some help/suggestions on how to straighten it
> > out.  I am trying to compose a program which uses a module and function.
> > The module is supposed to get an ID number and the function is supposed
> > to determine whether that value falls between 100 and 799 and return a
> > value so that the module can print a 'valid' or 'invalid' response.  Any
> > help would be appreciated as I am having trouble with this.  Thanks
> > Mike

> > Program functionb;
> > procedure getID;
> > var ID:integer;
> > begin
> >    writeln('Enter an ID number ');
> >    readln(ID);
> >    validation;
> >    if valid=1 then
> >       writeln('valid') else
> >           writeln('invalid');
> > end;
> > function validation;
> > var valid :integer;
> > begin
> >    if (ID>99) AND (ID<800) then
> >       valid:= 1 else
> >          valid:=0;
> > end;
> > begin
> >    getID;
> > end.

> > Sent via Deja.com http://www.deja.com/
> > Before you buy.



Wed, 18 Jun 1902 08:00:00 GMT  
 pascal functions help
Please notice that the type of function check_name should be boolean:

function check_name(id_num : integer): boolean;


Quote:
> Even an easier (and shorter) one...

> {-----------------------}
> program procedures_and_functions;

> var id_num : integer;

> procedure input_name (varid : integer);
> begin { procedure }
>   write(' Enter ID number ');
>   readln(id);
> end; {procedure}

> function check_name(id_num : integer): integer;
> begin {function}
>   check_name := (id_num > 99) and (id_num < 800);
> end; {function}

> begin {main}
>   input_name(id_num);
> if check_name(id_num) then
>   writeln('valid')
> else
>   writeln('invalid');
> end. {main}

> {-----------------------}



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Help With Pascal Iwant to call C function from Pascal

2. Turbo Pascal for Windows and Round function and randomize function

3. Need help porting function to Free Pascal

4. Need help porting function to Free Pascal

5. Need help with recursive functions in Turbo Pascal 7.0

6. Help with a pascal function

7. Need help with a pascal function

8. why define a function w/in another function?

9. functions as function

10. functions as function

11. BP7: Function of type function

12. Pass Functions into Function

 

 
Powered by phpBB® Forum Software