/* Ask the user to enter input */
scanf (“%f %d”, &wage, &hours) ;
/* read input */
calc_net_pay (wage, hours) ;
/* call the function
by passing the input variables */
}
// end of main
void
calc_net_pay (float wage, int hours)
/*
function
header same as function prototype
given at the start
of program */
{
float
g_pay, f_tax, soc_sec, net_pay ;
/*
function body */
const
float
FED_TAX = 0.28 ;
/*
constant declaration */
const
float
SOC_SEC = 0.055 ;
g_pay = wage * hours ;
f_tax = FED_TAX * g_pay ;
soc_sec = SOC_SEC * g_pay ;
net_pay = g_pay – (f_tax + soc_sec) ;
printf (“The net pay = %8.2f\n”, net_pay) ;
/* print
the result stored in net_pay */
}
// end of user defined function
calc_net_pay
Argument List Correspondence Rules :
•
The number of actual arguments/parameter, the order of
actual argument and type of actual arguments
used in
a call to a function must be the same as the number of
formal parameters, the order of formal parameters and
type of formal parameters/arguments listed in the
function definition.
Use of return Statement in Function :
An return statement can take one of the following form :
return;
Or
return( expression );
The first
, the ‘plain’ return
does not return any value,
just it returns control to calling function.
e.g
if(error)
Page 8 of 10

return;
The second form
of return
with an expression returns the
value of the expression.
e.g
int mul(int x, int y)
{
int p;
p=x*y;
return(p);
}
In this above last two statements can be combined into one
statement as
follows :
return (x*y);
Can a function have more than one return Statements ? :
Yes. A function may have more than one return
statements .
This situation arises when the value returned
is based on
certain conditions:
e.g
if(x<=0)
return(0);
else
return(1);
Note
:
All functions by default
return int
type data. But we
can make it any type.
Exercise:
Write a menu driven program
, which has the following
options:
1. Find Factorial of a Number.
2. Check if Number is Prime or not.
3. Check if Number is Even.
4. Exit
It must execute appropriate choice entered by user.
You need to write, in addition to the main function, three
other functions:
factorial that returns a factorial of its parameter.
The
return type should be long.
Page 9 of 10

prime which receives an integer number and returns 1 if it
is prime, 0 otherwise.
A prime number is a number greater
than 1, with no divisor except 1 and itself.
even which should return 1 if its parameter is even, 0
otherwise.
Page 10 of 10

#### You've reached the end of your free preview.

Want to read all 10 pages?

- Spring '10
- zaman
- Subroutine, Return statement, Control flow, User defined function