Function calling statement:
It is the statement initiates the execution of a function.
List of actual arguments are specified to send arguments to the function definition.
Here the type, number and sequence of actual arguments must be equal to the type, number and sequence of formal arguments.
The calling statement is assigned to the variable if the function returns a value.
Function declaration or prototype:
By default C compiler considers arguments and return type as integers.
If we use other types as arguments and return value then the behavior of function must be informed to the compiler by writing prototype.
Prototype is also called function declaration statement can be defined either before or within main() function.
Anatomy of a function:
Specification1:
Print your address using a sub function address()
Program:
#include<stdio .h="">
void address(void); /* optional */
void main()
{
clrscr();
address();
getch();
}
void address()
{
printf("\n Stephen William Hawking");
printf("\n Oxford, England");
printf("\n United Kingdom");
}
Output:
Stephen William Hawking
Oxford, England
United Kingdom
Example explained:
address() is a function accepts no arguments and returns no value.
Specification2:
Accept two numbers and print the sum, subtraction, product and division of them using the function manip().
Program:
#include<stdio .h="">
void manip(int,int); /* optional */
void main()
{
int a,b;
printf("Enter two numbers:\n");
scanf("%d%d",&a,&b);
manip(a,b);
getch();
}
void manip(int x,int y)
{
printf("Sum %d",x+y);
printf("\n Subtraction %d",x-y);
printf("\n Product %d",x*y);
printf("\n Division %f",(float)x/y);
getch();
}
Execution:
Enter two numbers:
25
2
Sum 27
Subtraction 23
Product 50
Division 12.5
Example explained:
The function manip() accepts two integers from main() and prints their sum, subtraction, product and division of them
Specification3:
Accept the length, breadth of a rectangle and print the area, perimeter using the function rectangle.
Program:
#include<stdio .h="">
void rectangle(int,int); /* optional */
void main()
{
int l,b;
clrscr();
printf("length of rectangle:");
scanf("%d",&l);
printf("breadth of rectangle:");
scanf("%d",&b);
rectangle(l,b);
getch();
}
void rectangle(int x,int y)
{
printf("Area %d",x*y);
printf("\n Perimeter %d",2*(x+y));
}
Execution:
length of rectangle: 7
breadth of rectangle: 5
Area: 35
Perimeter: 24
Specification4:
Accept the radius of a circle and print the area, circumference using the function circle()
Program:
#include<stdio .h="">
void circle(int);
void main()
{
int rad;
clrscr();
printf("Enter the radius:");
scanf("%d",&rad);
circle(rad);
getch();
}
void circle(int r)
{
printf("Area %f",3.14*r*r);
printf("\n Circumference %f",2*3.14*r);
}
Execution:
Enter the radius: 15
Area 706.500000
Circumference 94.200000
Specification5:
Accept three sides of a box and print its volume using the function box.
Program:
#include<stdio .h="">
int box(int,int,int);
void main()
{
int breath,width,height,vol;
clrscr();
printf("Enter breadth, width and height:\n");
scanf("%d%d%d",&breadth,&width,&height);
vol=box(breadth,width,height);
printf("Volume of box %d",vol);
getch();
}
int box(int b,int w,int h)
{
return b*w*h;
}
Execution:
Enter breadth, width and height:
5 7 3
Volume of box: 105
Example explained:
Here the box() function accepts breadth, width and height from the main and returns a single integer that is volume.
The returned value is assigned to the variable "vol" of main() and is printed as output.
Specification6:
Accept the limit and print the sum of natural numbers from 1 to the given limit using the function getsum()
Program:
#include<stdio .h="">
int getsum(int);
void main()
{
int n,sum;
clrscr();
printf("Enter the limit:");
scanf("%d",&n);
sum=getsum(n);
printf("Sum of natural numbers %d",sum);
getch();
}
int getsum(int n)
{
int i,s;
for(s=0,i=1;i<=n;i++)
s=s+i;
return s;
}
Execution:
Enter the limit: 10
Sum of natural numbers 55
Example explained:
Here both the actual and formal arguments are defined with the same name "n".
It can be done because they are local variables to their functions.
</stdio></stdio></stdio></stdio></stdio></stdio>
0 comments:
Post a Comment