Recent

Author Topic: function or procedure  (Read 1836 times)

ShadyNova

  • Newbie
  • Posts: 4
function or procedure
« on: January 29, 2019, 05:59:39 pm »
hello, today i had to write the mastermind game , now after it worked my teacher asked me to put some code that i wrote in a function or procedure, now i understand the principle behind function or procedure , tho i do not get how to start adjusting my code to a function or procedure. anyone got tips ? it has to be done for the random numbers and for the While loop numbers should be between 1-4 for this one to. How do i convert these to a function, or atleast how do i start ?

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: function or procedure
« Reply #1 on: January 29, 2019, 06:28:50 pm »
Start by moving the code to one (or more) procedures/functions and, where it was, insert calls to the procedures/functions. For example, this portion if code:

Code: Pascal  [Select][+][-]
  1. begin
  2.  
  3.   teller:=0;
  4.   minCijfer:=1;
  5.   maxCijfer:=4;
  6.   RANDOMIZE;
  7.   rndNumber1:= RANDOM(maxCijfer)+1;
  8.   rndNumber2:= RANDOM(maxCijfer)+1;
  9.   rndNumber3:= RANDOM(maxCijfer)+1;
  10.   rndNumber4:= RANDOM(maxCijfer)+1;
  11.   writeln(rndNumber1,' ', rndNumber2,' ', rndNumber3,' ', rndNumber4);
  12.   writeln('Raad de code. Geef 4 cijfers in, telkens gescheiden door een spatie.');
  13. [...etc...]

may end up as:

Code: Pascal  [Select][+][-]
  1. procedure InitRandomNumbers;
  2. begin
  3.   RANDOMIZE;
  4.   rndNumber1:= RANDOM(maxCijfer)+1;
  5.   rndNumber2:= RANDOM(maxCijfer)+1;
  6.   rndNumber3:= RANDOM(maxCijfer)+1;
  7.   rndNumber4:= RANDOM(maxCijfer)+1;
  8. end;
  9.  
  10. begin
  11.  
  12.   teller:=0;
  13.   minCijfer:=1;
  14.   maxCijfer:=4;
  15.   InitRandomNumbers;
  16.   writeln(rndNumber1,' ', rndNumber2,' ', rndNumber3,' ', rndNumber4);
  17.   writeln('Raad de code. Geef 4 cijfers in, telkens gescheiden door een spatie.');
  18. [...etc...]

The rest is left as an exercise for the reader  :)
Now seriously, you'll not learn if you don't try yourself. 8-)
« Last Edit: January 29, 2019, 06:30:31 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

ShadyNova

  • Newbie
  • Posts: 4
Re: function or procedure
« Reply #2 on: January 30, 2019, 01:06:46 pm »
thank you il try to do it also for the while loop :);

While loop also worked  :D tho it did not work as a function;
Now this one is finished but still gotta learn how to write a decent function and how to start writing that decent  function. xD
« Last Edit: January 30, 2019, 01:23:06 pm by ShadyNova »

 

TinyPortal © 2005-2018