Such functions are defined same with return type followed by used defined name and a pair of parenthesis () and a block of codes. These functions uses two type of variables AUTOMATIC STORAGE CLASS VARIABLES and GLOBAL VARIABLES.
AUTOMATIC STORAGE CLASS
These variables are declared inside the block in which they are to be utilized. They are created when a program is called and they are destroyed when control exits the function. Some of it’s properties are:1. Automatic variables are stored in computer memory.
2. They are not initialised automatically i.e. if no initial value is given to these variables then they will pick some garbage value.
3. The visibility of automatic variables are within the block in which they are declared.
4. The life time of automatic variables is limited to time the block is executed in which they are declared.
/*Program to show student’s name and taking values from user and perform operation*/
#include<stdio.h>#include<conio.h>
void fun1()
{
int a,b,sum=0;
printf("\n enter any value of a and b");
scanf("%d%d",&a,&b);
sum=a+b;
printf("\n sum of %d and %d is %d",a,b,sum);
printf("\n");
}
void main()
{
char nm[200];
printf("\n enter your name");
scanf("%s",nm);
fun1();
getch();
}
GLOBAL OR EXTERNAL VARIABLES
These are those variables which are active and are assess-able throughout the program. These are declared above the main function as well as in the user defined function. It means such variables have life and support throughout the program. Some of it’s properties are:1. Global variables are stored in computer memory.
2. If no initial value is given then they are automatically initialized to 0.
3. They are visible throughout the program.
4. They are active as long as program is running.
/*Program to enter his favorite line and then observe the function*/
#include<stdio.h>#include<conio.h>
int a=10,b=20;
void fun1()
{
int sum=0;
sum=a+b;
printf("\n sum of %d and %d is %d",a,b,sum);
printf("\n");
}
void main()
{
char nm[200];
printf("\n enter your favourite line");
scanf("%s",nm);
fun1();
getch();
}
No comments:
Post a Comment