Classification of Functions
Classification - I
Main Passing
Information.
Function Returning
Information
Program
#include
<stdio.h>
void main()
{
int add_fun(int,
int);
int a, b;
printf("\nEnter
Values for 'a' and 'b' : ");
scanf("%d%d",
&a, &b);
printf("\nThe
Addition of %d and %d = %d", a, b, add_fun(a, b));
}
int add_fun(int x,
int y)
{
return(x + y);
}
Classification - II
Main Passing
Information.
Function not
Returning Information.
Program
#include
<stdio.h>
void main()
{
void add_fun(int,
int);
int a, b;
printf("\nEnter
any Two Integers : ");
scanf("%d%d",
&a, &b);
add_fun(a, b);
}
void add_fun(int x,
int y)
{
printf("\nAddition
of %d and %d = %d", x, y, x+y);
return;
}
Classification - III
Main does not Supply
any Information.
Function Returns
Data.
Program
#include
<stdio.h>
void main()
{
int add_fun(void);
printf("is
%d", add_fun());
}
int add_fun(void)
{
int x,y;
printf("\nEnter
Any Two Integers : ");
scanf("%d%d",
&x, &y);
printf("\nThe
Addition of %d and %d ", x, y);
return(x + y);
}
Classification - IV
Main not Passing any
Information
Function not
Returning any Information
Program
#include
<stdio.h>
void main()
{
void add_fun(void);
printf("\nCalling
the Add Function...Please Wait...");
add_fun();
}
void add_fun(void)
{
int x, y;
printf("\nEnter
any Two Integers : ");
scanf("%d%d",
&x, &y);
printf("\nThe
Addition of %d and %d = %d", x, y, x + y);
return;
}
Illustrative Examples
Function with no
Parameters and no Returns a Value
Program
#include<stdio.h>
void main()
{
void div(void);
printf("\nCalling
The Division Function...");
div();
}
void div(void)
{
int num, deno;
printf("\nEnter
The Numerator and Denominator : ");
scanf("%d%d",
&num, &deno);
if(deno == 0)
{
printf("\nFatal
Error: Denominator Cannot by Zero");
deno = 1;
}
printf("\nDivision
of %d by %d = %0.2f ", num, deno, (float)num / (float)deno);
return;
}
Function with
Parameters and Returns a Value
Program
# include
<stdio.h>
void main()
{
int num, deno;
float div(int, int);
printf("\nEnter
the Numerator and Denominator : ");
scanf("%d%d",
&num, &deno);
printf("\nThe
division of %d by %d is %0.2f", num, deno, div(num, deno));
}
float div(int n, int
d)
{
if(d == 0)
{
printf("\nFatal
Error : Denominator cannot be Zero.");
d = 1;
}
return((float)n /
(float)d);
}
No comments:
Post a Comment