(Function)
© Manzur
Ashraf
Objective:
Learn how to write
user defined functions.
Scope:
The student should
know the following:
1.
Functions That Return a Single Result
2.
void Functions with Input Parameters
Basic Concepts about Function : (Please read out it )
Till now we were using only one function, the main function in our programs. Programs can also be written where there can be one or more function subprograms other than the main function. The program should always have a main function whether it has function subprograms or not. Without the main function the program will not do anything as the program always starts from the main function.
Functions can be of different types:
Let abc be a function name.
Then function that does not return any value and accept any value can be declared as
void abc (void)
Function that accepts one or more value and returns no value can be declared as
void abc (int x, float y, double d, int a)
Function that returns one value and accepts one or more values can be written as
int abc (int b, double k, char p)
Here the function is
returning an integer value.
float abc (int g, char h, double r)
Here the function is
returning a float value.
A short function is written to calculate the net pay of an employee based on values for the wage rate and hours worked. These values are passed as arguments to function called calc_net_pay. The function then computes the net pay and returns the computed value to main.
Solved Example (i):
#include
<stdio.h>
float calc_net_pay (float wage, int hours) ; /* function
prototype. Write this before main always */
void main (void)
{
float
wage, net_pay ;
int hours
;
printf (“Enter the wage rate and number of
hours worked\n”) ; /* Ask the user to enter input */
scanf (“%f %d”, &wage, &hours) ; /* read input */
net_pay = calc_net_pay (wage, hours) ; /* call the function
by passing the input variables */
printf (“The net pay = %8.2f\n”, net_pay) ; /* print the result stored
in net_pay returned by function */
}
float
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) ;
return
( net_pay ) ; /* end of
function by returning the result in net_pay */
}
The calc_net_pay function is called from main
and given two arguments wage and hours which are
usually the input variables. The name of
the function, the number of arguments and the types of arguments
should
always be same while calling the function and while declaring the function in
the header or as
prototype. Once the function is called the control
is transferred to the function and returns back to the main
program with the return statement.
In the above program if the function calc_net_pay does not return any value
then it has to print the result inside the function itself. Also while calling
the function just call the function along with its arguments without assigning
the call to any variable.
#include
<stdio.h>
void calc_net_pay (float wage, int hours) ; /* function
prototype. Write this before main always */
void main (void)
{
float
wage, net_pay ;
int hours
;
printf (“Enter the wage rate and number of
hours worked\n”) ; /* 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 */
}
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 */
}
Problem# 1 Compile and Execute Program given in
Solved Example(i) and (ii) and explain difference.
Program # 2 Write
a program that reads 2 integers start and end in the main
program. It passes these 2 integers to a function subprogram that uses
for loop to find the sum of all the integers between start and end and returns
the sum to the main program. The main program should print the sum.
Program # 3 Write
a program that reads 3 integers start, end and number in the main
program. It passes these 3 integers to a function subprogram that uses for loop
to find those integers between start and end that are divisible by number. If
it finds the numbers it prints them in the function only. Here the
function is not returning anything.
Divisible means giving a remainder of 0.
Evaluation:
Your grade will depend on your active
participation , individual efforts in solving problems , and seriousness during
the lab.