Local and Global variables
There
are two types of variables: local and global variables
Local
variables:
Local
variables are defined within the body of the function or the block({}).
The variable defined is local to that
function or block only. Other functions cannot access these variables. The
compiler shows errors in case other functions try to access the variables.
In
above figure, value1, value2, value3 and value5 are local variables. These are
also called automatic variables.
Global
Variables:
Global
variables are defined outside the main() function.
Multiple
functions can use them. The example is illustrated below for understanding.
The
variable value1, which appears at the beginning of the file, is declared at
global scope, as is ‘value4’, which appears after the function main(). The
scope of each global variable extends from the point at which it is defined to
the end of the file. Even though value4 exists when execution starts, it cannot
be referred to in main(), because main() is not within the variables scope,
which is not the case for the automatic variables.
Usage
of global variables is not encouraged.
Function Prototypes
In
‘C’, we use many built-in functions. the prototypes of these functions are
given in the respective header files. Also, while defining user-defined
functions, we must declare its prototype. A prototype statement helps the
compiler to check the return type and argument type of the function.
A
function prototype declaration consists of the functions return type, name and
arguments list. When the programmer defines the function, the definition of the
function must be the same like its prototype declaration. If the programmer
makes any mistake, the compiler flags an error message. The function prototype
declaration statement is always terminated with a semi-colon. The statements
given below are examples of function prototypes.
Syntax of the function
prototype
return_type
function_name (type arg1, type arg2, ...... type arg n);
Example:
float
sum(float x,inty);
/*
In the above example the prototype functions sum() is declared. Its return
value is float and arguments are float and integer type respectively */