Subscribe by Email

Tuesday, September 6, 2011

What are functions,variables and prototyping in C?

Variables or identifiers are the fundamental basic building blocks of a program. They are general terminology for objects, classes, functions, arrays etc. It can be defined as an arbitrarily long sequence of letters and digits and the first character must always be a letter. Upper and lower case letters are treated differently and all the characters are significant. C is implementation dependent. C is case sensitive as it treats lower and upper case letters differently. These are basically named storage, whose values can be manipulated during the execution of the program. There are 2 values associated with every variable namely r value and l value. The value of the variable is called r value whereas l value is the address in memory where the variable is located. Generally a variable is declared as :

type name;

A simple definition consists of a type specifier followed by a variable name. There are 2 types of variables: local variables and global variables. Local variables are declared inside a function and are available to that function only whereas global variable are declared outside main() and are available to all the functions.

A large program is difficult to manage and understand. Thus, it is broken down into different modules called functions which can be put together to form the complete program. So we can define a function as a sub program that acts on data and often returns a value. A good function contains a number of functions since it is easier to maintain update and debug. Each function has its own name. On the encounter of the function’s name in any part of the program the function is called. There are basically 2 types of functions namely built-in functions and user-defined functions. Built in functions are parts of the compiler package and are made available to the program by header files. For example: exit(), sqrt(), strlen(), size() etc. user defined functions are created by the programmer as per the requirements of the program. A function has 3 parts: function definition, function call and function prototype. In C a function must be defined before it is used anywhere. Generally a function is defined as:

type function-name(parameter list)
Body of the function

Here the type specifies the type of return value. The parameter list is the list of arguments passed to the function separated by commas. A function may be defined without any arguments or it can also have an open parameter. A function definition must have a return statement.

One of the most important features of C is function prototyping as it describes the function interface to the compiler. It can be defined as the declaration of the function that tells the program about the type of the value returned by the function and the number and type of arguments. Therefore, we can say that a prototype has following parts:
- Return type
- Name of the function
- Argument list

Function prototyping is essential as it allows a compiler to compare each use of the function with the prototype to determine whether the function has been invoked properly or not. This makes it easy for compiler to correct the errors. The function prototype and function definition must exactly agree on the function name, return type, and argument list. The prototype looks just like a function definition except that the code is missing. A prototype introduces a function to the program whereas a definition being a declaration also tells the program what the function is doing and how it is doing. C makes prototyping essential. We can skip the arguments in a prototype but not in the definition.

No comments:

Facebook activity