#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# How can I place this in a function?

Member Posts: 51
Hello,

I have got my program running to calculate grosspay and taxes, but I am trying to calculate the grosspay using a function.
I have become kind of stuck with this.
Can somebody plz give me some help?

[code]
uses
crt;

const
normalhours = 40;
taxrate1 = 0.195;
taxrate2 = 0.33;
taxrate3 = 0.39;

var
empname : string[20];
payrate , errorcode : real;
workedhours , errorcode2 : real;
grosspay : real;
taxdeduction1 , taxdeduction2, taxdeduction3 : real;

begin
clrscr;
repeat
write('Enter the hourly pay rate: ');
{\$I-}
{\$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-}
{\$I+}
errorcode := IOResult;
if errorcode <> 0 then
begin
sound(1000);
delay(100);
nosound;
end;
until errorcode = 0;
writeln;

function calgrosspay( workedhours, payrate: real) real;
var
paidhours, premiumhours : real;
begin
overtimehours := workedhours - normalhours;
if overtimehours <= 0
begin
paidhours := workedhours;
end
else if overtimehours > 0 then
begin
premiumhours := overtimehours * payrate * 1.5;
end;

paidhours := normalhours * payrate;
grosspay := premiumhours + paidhours;

end;

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;
end;
end;

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: \$', grosspay(workedhours, payrate:2:2);
writeln;
writeln;
writeln('The 19.5% tax rate results in a S', taxdeduction1:2:2 ,' tax deduction');
writeln('from the first \$728.77 of the employees grosspay.');
writeln;
writeln('The 19.5% tax deduction of the first \$728.77 plus a 33% tax deduction');
writeln('of anything above \$728.77 and below \$1150.68 results in \$', taxdeduction2:2:2);
writeln('being deducted from the employees grosspay.');
writeln;
writeln('The 19.5% tax deduction of the first \$728.77 plus a 33% tax deduction');
writeln('of anything above \$728.77 and below \$1150.68, plus a further 39% tax ');
writeln('deduction of any amount above \$1150.68 results in a total tax deduction ');
writeln('of \$', taxdeduction3:2:2);
writeln;
writeln('Press ENTER to exit the program');
end.
[/code]

• Member Posts: 6,349
: Hello,
:
: I have got my program running to calculate grosspay and taxes, but I am trying to calculate the grosspay using a function.
: I have become kind of stuck with this.
: Can somebody plz give me some help?
:
: [code]
: uses
: crt;
:
: const
: normalhours = 40;
: taxrate1 = 0.195;
: taxrate2 = 0.33;
: taxrate3 = 0.39;
:
: var
: empname : string[20];
: payrate , errorcode : real;
: workedhours , errorcode2 : real;
: grosspay : real;
: taxdeduction1 , taxdeduction2, taxdeduction3 : real;
:
: begin
: clrscr;
: repeat
: write('Enter the hourly pay rate: ');
: {\$I-}
: {\$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-}
: {\$I+}
: errorcode := IOResult;
: if errorcode <> 0 then
: begin
: sound(1000);
: delay(100);
: nosound;
: end;
: until errorcode = 0;
: writeln;
:
:
: function calgrosspay( workedhours, payrate: real) real;
: var
: paidhours, premiumhours : real;
: begin
: overtimehours := workedhours - normalhours;
: if overtimehours <= 0
: begin
: paidhours := workedhours;
: end
: else if overtimehours > 0 then
: begin
: premiumhours := overtimehours * payrate * 1.5;
: end;
:
: paidhours := normalhours * payrate;
: grosspay := premiumhours + paidhours;
:
: end;
:
:
: 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;
: end;
: end;
:
: 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: \$', grosspay(workedhours, payrate:2:2);
: writeln;
: writeln;
: writeln('The 19.5% tax rate results in a S', taxdeduction1:2:2 ,' tax deduction');
: writeln('from the first \$728.77 of the employees grosspay.');
: writeln;
: writeln('The 19.5% tax deduction of the first \$728.77 plus a 33% tax deduction');
: writeln('of anything above \$728.77 and below \$1150.68 results in \$', taxdeduction2:2:2);
: writeln('being deducted from the employees grosspay.');
: writeln;
: writeln('The 19.5% tax deduction of the first \$728.77 plus a 33% tax deduction');
: writeln('of anything above \$728.77 and below \$1150.68, plus a further 39% tax ');
: writeln('deduction of any amount above \$1150.68 results in a total tax deduction ');
: writeln('of \$', taxdeduction3:2:2);
: writeln;
: writeln('Press ENTER to exit the program');
: end.
: [/code]
:
:
All functions and procedures are defined before the begin of the main program block. So your code should look like:
[code]
uses
Crt;

function CalGrossPay(workedhours, payrate: real) real;
begin
{ code of the function }
end;

var
empname: string[20];
{ other variables }

begin
{ main program block }
end.
[/code]
I've abbreviated the code to shorten the message.
• Member Posts: 3
You must place the FUNCTION section out of the main BEGIN..END section.
So the code should be write like:

---------------------------------------------------
uses
crt;

const
normalhours = 40;
taxrate1 = 0.195;
taxrate2 = 0.33;
taxrate3 = 0.39;

var
empname : string[20];
payrate , errorcode : real;
overtimehours, workedhours , 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;

end;

begin
clrscr;
repeat
write('Enter the hourly pay rate: ');
{\$I-}
{\$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-}
{\$I+}
errorcode := IOResult;
if errorcode <> 0 then
begin
sound(1000);
delay(100);
nosound;
end;
until errorcode = 0;
writeln;

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;
end;
end;

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));
writeln;
writeln;
writeln('The 19.5% tax rate results in a S', taxdeduction1:2:2 ,' tax deduction');
writeln('from the first \$728.77 of the employees grosspay.');
writeln;
writeln('The 19.5% tax deduction of the first \$728.77 plus a 33% tax deduction');
writeln('of anything above \$728.77 and below \$1150.68 results in \$', taxdeduction2:2:2);
writeln('being deducted from the employees grosspay.');
writeln;
writeln('The 19.5% tax deduction of the first \$728.77 plus a 33% tax deduction');
writeln('of anything above \$728.77 and below \$1150.68, plus a further 39% tax ');
writeln('deduction of any amount above \$1150.68 results in a total tax deduction ');
writeln('of \$', taxdeduction3:2:2);
writeln;
writeln('Press ENTER to exit the program');
end.
---------------------------------------------------------------ENDCODE
• Member Posts: 51
Hi, thanks for your help. But when I tried to compile this code, it tells me a ";" is expected and the cursor is placed at the beginning of else. I don't think I should put a ; at the end of the first end, right?
so why doesn't it run?

thank you

: You must place the FUNCTION section out of the main BEGIN..END section.
: So the code should be write like:
:
: ---------------------------------------------------
: uses
: crt;
:
: const
: normalhours = 40;
: taxrate1 = 0.195;
: taxrate2 = 0.33;
: taxrate3 = 0.39;
:
: var
: empname : string[20];
: payrate , errorcode : real;
: overtimehours, workedhours , 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;
:
: end;
:
:
: begin
: clrscr;
: repeat
: write('Enter the hourly pay rate: ');
: {\$I-}
: {\$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-}
: {\$I+}
: errorcode := IOResult;
: if errorcode <> 0 then
: begin
: sound(1000);
: delay(100);
: nosound;
: end;
: until errorcode = 0;
: writeln;
:
:
:
:
: 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;
: end;
: end;
:
: 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));
: writeln;
: writeln;
: writeln('The 19.5% tax rate results in a S', taxdeduction1:2:2 ,' tax deduction');
: writeln('from the first \$728.77 of the employees grosspay.');
: writeln;
: writeln('The 19.5% tax deduction of the first \$728.77 plus a 33% tax deduction');
: writeln('of anything above \$728.77 and below \$1150.68 results in \$', taxdeduction2:2:2);
: writeln('being deducted from the employees grosspay.');
: writeln;
: writeln('The 19.5% tax deduction of the first \$728.77 plus a 33% tax deduction');
: writeln('of anything above \$728.77 and below \$1150.68, plus a further 39% tax ');
: writeln('deduction of any amount above \$1150.68 results in a total tax deduction ');
: writeln('of \$', taxdeduction3:2:2);
: writeln;
: writeln('Press ENTER to exit the program');
: end.
: ---------------------------------------------------------------ENDCODE
:

• Member Posts: 6,349
: Hi, thanks for your help. But when I tried to compile this code, it tells me a ";" is expected and the cursor is placed at the beginning of else. I don't think I should put a ; at the end of the first end, right?
: so why doesn't it run?
:
: thank you
:
: : You must place the FUNCTION section out of the main BEGIN..END section.
: : So the code should be write like:
: :
[code]
: : ---------------------------------------------------
: : uses
: : crt;
: :
: : const
: : normalhours = 40;
: : taxrate1 = 0.195;
: : taxrate2 = 0.33;
: : taxrate3 = 0.39;
: :
: : var
: : empname : string[20];
: : payrate , errorcode : real;
: : overtimehours, workedhours , 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
[red]: : else if overtimehours > 0 then[/red]
: : begin
: : premiumhours := overtimehours * payrate * 1.5;
: : end;
: :
: : paidhours := normalhours * payrate;
: : grosspay := premiumhours + paidhours;
: :
: : end;
: :
: :
: : begin
: : clrscr;
: : repeat
: : write('Enter the hourly pay rate: ');
: : {\$I-}
: : {\$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-}
: : {\$I+}
: : errorcode := IOResult;
: : if errorcode <> 0 then
: : begin
: : sound(1000);
: : delay(100);
: : nosound;
: : end;
: : until errorcode = 0;
: : writeln;
: :
: :
: :
: :
: : if grosspay <= 728.77 then
: : taxdeduction1 := grosspay * taxrate1
[red]: : else[/red]
: : begin
: : taxdeduction1 := 728.77 * taxrate1;
: : if grosspay <= 1150.68 then
: : taxdeduction2 := taxdeduction1 + (grosspay - 728.77) * taxrate2
[red]: : else[/red]
: : begin
: : taxdeduction2 := taxdeduction1 + (1150.68 - 728.77) * taxrate2;
: : taxdeduction3 := taxdeduction2 + (grosspay - 1150.68) * taxrate3;
: : end;
: : end;
: :
: : 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));
: : writeln;
: : writeln;
: : writeln('The 19.5% tax rate results in a S', taxdeduction1:2:2 ,' tax deduction');
: : writeln('from the first \$728.77 of the employees grosspay.');
: : writeln;
: : writeln('The 19.5% tax deduction of the first \$728.77 plus a 33% tax deduction');
: : writeln('of anything above \$728.77 and below \$1150.68 results in \$', taxdeduction2:2:2);
: : writeln('being deducted from the employees grosspay.');
: : writeln;
: : writeln('The 19.5% tax deduction of the first \$728.77 plus a 33% tax deduction');
: : writeln('of anything above \$728.77 and below \$1150.68, plus a further 39% tax ');
: : writeln('deduction of any amount above \$1150.68 results in a total tax deduction ');
: : writeln('of \$', taxdeduction3:2:2);
: : writeln;
: : writeln('Press ENTER to exit the program');
: : end.
: : ---------------------------------------------------------------[/code]
: :
:
:
I just compiled this code successfully. Could you tell us which else, since you have 3?
• Member Posts: 51
[b][red]This message was edited by Rodimus Prime at 2004-3-25 2:4:49[/red][/b][hr]
Hi,
Thank you too for wanting to help me out. Ok, it was the first Else where I got the error message. But I tested the program on two different computers, and it compiled. So there's probably something wrong with the Turbo version on that computer that couldn't compile it.
But what the problem is now, is that, the function isn't called I think! I enter some input, but the grosspay always shows up as \$0.00!!
I have looked at some examples of other functions in PASCAL, and I can't get my head round why the function isn't used, coz I have checked my algorithm, and if I enter a \$31.5 payrate, and 43 hours worked for example. The grosspay should come out as \$1401.75, but it's just \$0.00

Thank you

EDIT: It does display something in grosspay when I don't round it, but it give this long real number which isn't of much use. But when I added :2:2) to the end of the function, that's when grosspay displayed \$0.00

• Member Posts: 51
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

[code]

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-}
{\$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-}
{\$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');
end.

• Member Posts: 6,349
: 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
:
: [code]
:
: 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-}
: {\$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-}
: {\$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');