C family of languages are procedure oriented languages. You split your work into functions and these functions call other functions and get the work done. A function is like a miniprogram, with its own set of variables, statements (instructions). In addition, a function will take some parameters and returns a values.
Another feature of C functions are, they are a self contained blocks. All variables defined inside the functions are local to the function. Even the parameters sent by caller are sent as copies without affecting the original values.
C does not distinguish between procedures and functions like Pascal. If a function is meant to execute statements and not return an answer, it is supposed to return void. Note here that void is also a data type. But you can have only void functions and pointers but not have void variables.
A C function can return only one value. This return value can be any built in or extended data type except for an array. A FUNCTION CAN NOT RETURN AN ARRAY. NOR CAN IT RETURN ANOTHER FUNCTION. If there is a need to return an array, then the function can be made to return a pointer because of similarities between array and pointer.
C functions are isolated from each other. A function can not use variables defined in other functions. It can only use local variables or so called global variables which are variable defined outside of all functions.
In order to use a function, three steps are needed. 1> Declare a function 2> Define a function 3> Invoke a function. Declaration is like a variable declaration. It tells the type of function (ie type of its return value) and type and number of its parameters. Function declaration is function header with a semicolon.
If a function is not declared, it is assumed to be a function which can take any number of parameters and which returns an int. Note int not void. Hence many students have the practice of writing main() instead of int main(). But that is OK when compared to the blunder of writing void main(). Never write void main(). main function returns an integer which is the exit status of the program.
A function can call itself. Then it becomes a recursive function. These recursive functions are useful for solving some algorithms. But if a recursive call is the last statement of a function, then it is tail end recursion which should be avoided. As a rule of thumb, you should use recursion only when iterative solution is too compicated.
A function creates a stack frame when it is invoked. This frame will store all local variables, parameters etc. When the function is returned, the stack frame is removed. This is an advantage, as the variable take up memory only when they are in use. But it will not be possible to remember the previous value of local variable. For that you must use a static variable.
Every C program has a special function called main(). This function is the starting point of the program for hosted environments. Other functions do get executed when they are called by main directly or indirectly.
main() can accept parameters in the form of command line arguemnts. These are a series of strings supplied with the exec file name. They will be stored in array of strings normally called argv (argument vector) and their count will be stored in argc.
An array gets passed to the function as call by address method unlike other types of parameters which are passed as copies (ie call by value). If the function modifies array parameter in the function, the actual parameter array also gets modified (For any other type of parameter, this does not happen). The reason for this is, the function treats array parameter as a pointer to first element of the array and manipulates the array using this pointer.
Note that in any expression (except for sizeof operator) which uses an array, the array gets converted into pointer.
Sunday, June 29, 2008
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment