: Hi,
:
: I got the program compiling succesfully now too. I don't what the problem was. Perhaps one of the compilers on the computer.
: I have also decided to put the tax calculations into a function.
: My question is, how can I make the output print every tax deduction amount after each deduction?
: Currently it displays the first tax deduction for all three. It's probably obvious that it shouldn't be there.
: But where do the other 2 taxdeduction results go?
:
: Further help is appreciated
:
:
:
: uses
: crt;
:
: const
: normalhours = 40;
: taxrate1 = 0.195;
: taxrate2 = 0.33;
: taxrate3 = 0.39;
:
: var
: empname : string[20];
: payrate , errorcode : real;
: workedhours , overtimehours , errorcode2 : real;
: grosspay : real;
: taxdeduction1 , taxdeduction2, taxdeduction3 : real;
:
:
: function calgrosspay(workedhours, payrate: real): real;
: var
: paidhours, premiumhours : real;
:
: begin
: overtimehours := workedhours - normalhours;
: if overtimehours <= 0 then
: begin
: paidhours := workedhours;
: end
: else if overtimehours > 0 then
: begin
: premiumhours := overtimehours * payrate * 1.5;
: end;
:
: paidhours := normalhours * payrate;
: grosspay := premiumhours + paidhours;
: calgrosspay := grosspay;
: end;
:
: function caltaxdeduction(grosspay: real): real;
: begin
: if grosspay <= 728.77 then
: taxdeduction1 := grosspay * taxrate1
: else
: begin
: taxdeduction1 := 728.77 * taxrate1;
: if grosspay <= 1150.68 then
: taxdeduction2 := taxdeduction1 + (grosspay - 728.77) * taxrate2
: else
: begin
: taxdeduction2 := taxdeduction1 + (1150.68 - 728.77) * taxrate2;
: taxdeduction3 := taxdeduction2 + (grosspay - 1150.68) * taxrate3;
: caltaxdeduction := taxdeduction1;
: caltaxdeduction := taxdeduction2;
: caltaxdeduction := taxdeduction3;
: end;
: end;
: end;
:
: begin
: clrscr;
: repeat
: write('Enter the hourly pay rate: ');
: {$I-}
: readln(payrate);
: {$I+}
: errorcode := IOResult;
: if errorcode <> 0 then
: begin
: sound(1000);
: delay(100);
: nosound;
: end;
: until errorcode = 0;
:
: repeat
: write('Enter the hours worked this week: ');
: {$I-}
: readln(workedhours);
: {$I+}
: errorcode := IOResult;
: if errorcode <> 0 then
: begin
: sound(1000);
: delay(100);
: nosound;
: end;
: until errorcode = 0;
: writeln;
:
:
:
: writeln('Current employee is: ', empname);
: writeln('Employee hourly pay rate is: $', payrate:2:2);
: writeln('The hours the employee worked this week is: ', workedhours:2:2);
: writeln('Employee grosspay for this week is: $', calgrosspay(workedhours, payrate):2:2);
: writeln;
: writeln;
: writeln('19.5% tax deduction of the first $728.77 equals S', caltaxdeduction(grosspay):2:2);
: writeln;
: writeln;
: writeln('The 19.5% tax deduction of the first $728.77 plus a 33% tax deduction of');
: writeln('anything above that but below $1150.68 equals $', caltaxdeduction(grosspay):2:2);
: writeln;
: writeln;
: writeln('The 19.5% tax deduction of the first $728.77 plus a 33% tax deduction of');
: writeln(' anything above that but below $1150.68 and a 39% deduction of anything');
: writeln('above $1150.68 equals $' , caltaxdeduction(grosspay):2:2);
: writeln;
: writeln;
: writeln('Press ENTER to exit the program');
: readln;
: end.
:
:
A function can only return 1 result. If you want return more results, you either need to split the function into smaller functions (in this case 1 for each tax deduction); or you can combine the result into either an array or a record; or you need to use a procedure with variable parameters.
I would advice you to make 1 function per tax deduction. Hint: you can call a function from within another function, if the former is defined before the latter.