Sunday, September 8, 2013

some more about function

Posted by Unknown On 5:58 AM No comments

Global Variable And Local Variable

Local Variable : a variable declared within the body of a function will be evaluated only within the function. The portion of the program in which a variable is retained in memory is known as the scope of the variable. The scope of the local variable is a function where it is defined. A variable may be local to function or compound statement.
Global Variable : a variable that is declared outside any function is known as a global variable. The scope of such a variable extends till the end of the program. these variables are available to all functions which follow their declaration. So it should be defined at the beginning, before any function is defined.

Variables and storage Class

The storage class of a variable determines which parts of a program can access it and how long it stays in existence. The storage class can be classified as automatic register static external
Automatic variable
All variables by default are auto i.e. the declarations int a and auto int a are equivalent. Auto variables retain their scope till the end of the function in which they are defined. An automatic variable is not created until the function in which it defined is called. When the function exits and control is returned to the calling program, the variables are destroyed and their values are lost. The name automatic is used because the variables are automatically created when a function is called and automatically destroyed when it returns.
Register variable 
A register declaration is an auto declaration. A register variable has all the characteristics of an auto variable. The difference is that register variable provides fast access as they are stored inside CPU registers rather than in memory.
Static variable
A static variable has the visibility of a local variable but the lifetime of an external variable. Thus it is visible only inside the function in which it is defined, but it remains in existence for the life of the program.
External variable 
A large program may be written by a number of persons in different files. A variable declared global in one file will not be available to a function in another file. Such a variable, if required by functions in both the files, should be declared global in one file and at the same time declared external in the second file.

Function Prototype

A function prototype declares the function name, its parameters, and its return type to the rest of the program. This is done before a function is declared. (In most cases at the beginning of a program.) To understand why function prototypes are useful, try the following program:
#include<iostream>
 using namespace std;
 int main()
 {
  int answer;
  answer = Add(3,3);
  cout << answer;
  return 0;
 }
 int Add(int A, int B)
 {
  return A + B;
 }



This program will not compile on most compilers. (On some compilers you have to set a compiler option before it gives a warning or error.) The compiler will give an error, something like: ” Add, identifier not found”. (As we said before the function must be known before it’s called).
To solve this problem you can make use of function prototypes. If you use prototypes, the compiler can find the function and the compiler will check the parameters list (for wrong type usage). Try the next example:
#include<iostream>
 using namespace std;
 int Add(int,int); //Function prototype
 int main()
 {
  int answer;
  answer = Add(3,3);
  cout << answer;
  return 0;
 }
 int Add(int A, int B)
 {
  return A + B;
 }
The compiler can now find the Add function. It will also check if correct types (int,int) are used. This program should compile without a problem.
So in the future use function prototypes for every function you declare. It will save you a lot of time.
now let us look at some more of functions:--
a program to find the area and volume of a circle
#include<iostream> #include<string> using namespace std; int getapositive() { int num; cout<<" enter a radius"; cin>>num; return num; } double area(int r) { return 3.14*r*r; } double volume(int r) { return 4/3.0*3.14*r*r*r; } int main() { int radius=getapositive(); double carea=area(radius); double cvolume=volume(radius); cout<<" the area of a circle is"<<carea<<endl; cout<<" the volume of the circle is"<<cvolume<<endl; return 0; }

0 comments:

Post a Comment