www.pickatutorial.com Computer Tutorials
Top eBooks: C/C++ | C# | Android | Mathematics | Database | Cloud | Graphics | Networking | Oracle | Hardware | AI
Top Tutorials: C/C++ | C#.NET | PHP MySQL | Java | Java Script | jQuery | HTML | xHTML | HTML5 | VB Script| CSS
Lessons Lesson 11: Header Files Bookmark and Share
Lesson 1
Lesson 2
Lesson 3
Lesson 4
Lesson 5
Lesson 6
Lesson 7
Lesson 8
Lesson 9
Lesson 10
Lesson 11
Lesson 12
Lesson 13
Lesson 14
Lesson 15
Lesson 16
Lesson 17
Lesson 18
Lesson 19
Lesson 20
Lesson 21
Lesson 22
Lesson 23
Lesson 24
Lesson 25
Lesson 26
Lesson 27
Lesson 28
Lesson 29
Lesson 30
Lesson 31
Lesson 32
Lesson 33
Lesson 34
Lesson 35
Lesson 36
Lesson 37
Lesson 38
Lesson 39
Lesson 40
We have already been using a header file from day-zero. We used to write at the top before the start of the main() function <iostream.h>, with ‘.h’ as an extension, you might have got the idea that it is a header file. Now we will see why a Header file is used. In the previous lessons, we discussed a little bit about Function Prototypes. One thing is Declaration and other is Definition. Declaration can also be called as 'Prototype'. Normally, if we have lot of functions and want to use them in some other function or program, then we are left with only one way i.e. to list the prototypes of all of them before the body of the function or program and then use them inside the function or program. But for frequent functions inside a program, this technique increases the complexity (of a program). This problem can be overcome by putting all these function prototypes in one file and writing a simple line of code for including the file in the program. This code line will indicate that this is the file, suppose 'area.h' containing all the prototypes of the used functions and see the prototypes from that file. This is the basic concept of a header file. So what we can do is:

- Make our own header file which is usually a simple text file with '.h' extension ('.h' extension is not mandatory but it is a rule of good programming practice).
- Write function prototypes inside that file. (Recall that prototype is just a simple line of code containing return value, function name and an argument list of data types with semi-colon at the end.)
- That file can be included in your own program by using the ‘#include’ directive and that would be similar to explicitly writing that list of function prototypes.

Function prototypes are not the only thing that can be put into a header file.There are some preprocessor directives which we are going to cover later. At the moment, we will discuss about ‘#define’. With the help of #define we can define the constants of our programs like the value of pi the header files and use them in our programs without declaring them again. We define the onstants using this preprocessor directive as:

#define pi 3.1415926

Scope of Identifiers

An 'Identifier' means any name that the user creates in his/her program. These names can be of variables, functions and labels. Here the scope of an identifier means its visibility. We will focus Scope of Variables in our discussion. Suppose we write the function:

void func1()
int i;
. . . //Some other lines of code
int j = i+2; //Perfectly alright
. . .

Now this variable ‘i’ can be used in any statement inside the function func1(). But consider this variable being used in a different function like:

void func2()
int k = i + 4; //Compilation error
. . .

The variable ‘i’ belongs to func1() and is not visible outside that. In other words, ‘i’ is local to func1(). To understand the concept of scope further, we have to see what are Code Blocks? A code block begins with ‘{‘ and ends with ‘}’.Therefore, the body of a function is essentially a code block. Nonetheless, inside a function there can be another block of code like 'for loop' and 'while loop' can have their own blocks of code respectively. Therefore, there can be a hierarchy of code blocks. A variable declared inside a code block becomes the local variable for that for that block.
It is not visible outside that block. See the code below:

void func()
int outer; //Function level scope
. . .

int inner; //Code block level scope
inner = outer; //No problem
. . .
inner ++; //Compilation error

Please note that variable ‘outer’ is declared at function level scope and variable ‘inner’ is declared at block level scope. The ‘inner’ variable declared inside the inner code block is not visible outside it . In other words, it is at inner code block scope level. If we want to access that variable outside its code block, a compilation error may occur.

Global Variables

Now, is there a way that we declare a variable only once and then use it inside all functions. We have already done a similar task when we wrote a function prototype outside the body of all the functions. The same thing applies to declaration of variables. You declare variables outside of a function body (so that variable declarations are not part of any function) and they become visible and accessible inside all functions of that file. Notice that we have just used a new word ‘file’. A file or a source code file with extension ‘.c’ or ‘.cpp’ can have many functions inside. A file will contain one main() function maximum and rest of the functions as many as required. If you want a variable to be accessible from within all functions, you declare the variable outside the body of any function like the following code snippet has declared such a variable ‘size’ below.

#include <iostream.h>
. . .
// Declare your global variables here
int size;
. . .
int main( … )
. . .

Now, this ‘size’ is visible in all functions including main(). We call this as 'file scope variable' or a 'global variable'.

Home - Advertise - Contact - Disclaimer - About Us
© Since 2006 pickatutorial.com -- All Rights Reserved.