Function
A function is a block of code that performs a specific task.
Or a number of statement grouped into a single logical unit is called function.
C program may contain two or more than two function. Among these function any one of them must be the main function which is executed at the beginning and other function is executed when main function called them.
A function can also be referred as a method or a sub-routine or a procedure, etc.
Two types of function
- Library function
- User defined function
Library Function |
User defined Function |
These function are predefined in the compiler of C language. |
These functions are not predefined in the Compiler. |
The function which are already written by the C-languages developer is library function. |
The function which are written by the programmer at the time of writting program. |
The programmer can only use these function. These cannot be modify. |
These function can be modify by the programmer. |
There are many library function of different types. |
User defined function are of only four types.
- without return type without argument (0,0)
- without return type with argument (0,1)
- with return type without argument (1,0)
- with return type with argument (1,1)
|
Library function requires a header file to use it. |
User defined function requires a function prototype to use it. |
Program development time will be faster. |
Program development time will be usually slower. |
E.g. printf(), scanf(), getch(), scanf(), pow(). |
E.g. addition(), fact()...etc. |
In order to make use of user defined function we need to established 3 main elements. The three main elements are:
- Function declaration (Function prototype)
- Function call (Calling function)
- Function definition (Called function)
1. Function declarations
A function declaration tells the compiler about a function name and how to call the function.
The actual body of the function is defined separately.
A function declaration has the following parts:
Parameter names are not important in function declaration only their type is required, so following is also a valid declaraction.
2. Function call
While creating a C function, you give a definition of what the function has to do. To use a function, you will have to call that function to perform the defined task.
When a program calls a function, the program control is transferred to the called function. A called function performs a defined task and when its return statement is executed or when its function-ending closing brace is reached, it returns the program control back to the main program.
To call a function, you simply need to pass the required parameters along with the function name, and if the function returns a value, then you can store the returned value.
3. Function Definition
The general form of a function definition in C programming language is as follow:
A function definition in C programming consists of :
- Return Type: A function may return a value. The return_type is the data type of the value the function return. Some functions perform the desired operations without returning a value. In this case, the return_type is the keyword int.
- Function Name: This is the actual name of the function. The function name and the parameter list together constitute the function signature.
- Parameters: When a function called, you pass a value to the parameter. This value is referred to as actual parameter or argument. The parameter list refers to the type, order, and number of the parameters of a function. Parameters are optional; that is, a function may contain no parameters.
- Function Body: The function body contains a collection of statements that define what the function does.
-
Return statement: Return statement is used in the called function in order to return value to the calling function. A return statement return only one value at a time. In order to return more than one value we use call by reference without using return statement.
Types of User define function
- With no return type with no argument (0,0)
- With no return type with argument (0,1)
- With return type with no argument (1,0)
- With return type with argument (1,1)
Two method of passing argument
- Call by Value
- Call by reference
Call by Value |
Call by Reference |
The process of passing values of the variable from the calling function to the called function is call by value. |
The process of passing memory address of variable form calling function to the called function is call by reference. |
Any change made to the formal argument does not affect actual argument. |
Any change made to formal argument affects the actual argument. |
Pointers are not used to pass the value. |
Pointers are used to pass the address. |
In call by value only one value can be return at a time. |
In call by reference more than one value can be return at a time. |
Return statement is used to return the value. |
Return statement is not used to return multiple value. |
Storage class
In order to define a variable completly we need to mention not only its data type and name but also its storage class.
The storage class gives the information about
- initial value of the variable i.e. if we do not explicitly initialize that variable, what will be its default initial value.
- the part of the memory where the variable would be stored
- scope of variable i.e. where the value of the variable would be available inside a program.
- lifetime of variable i.e. for how long will that variable exit.
Four types of storage class:
- Local or automatic storage class
- Global or external storage class
- Static storage class
- Register storage class
Local (Automatic) variable
The variable declared inside a particular area or block is called local variable. And the keyword for local or automatic variable is auto.
Declaration:
- Initial Value: Garbage value i.e. Random value
- Storage space: temporary memory (RAM)
- Scope/Visibility: Within the block where it is declared. (local)
- Lifetime: Till the control remain within that block
Global (External) variable
The variable declared outside the main function and which is active and alive as long as the program execution does not come to an end. And the keyword for global variable is extern.
Declaration:
Note: If the global and local variable is declared with same name then priority is given to local.
- Initial Value: Zero
- Storage space: temporary memory (RAM)
- Scope/Visibility: Global i.e. everywhere in the program, Multiple files
- Lifetime: as long as program execution does not come to an end.
Static variable
The variable whose value is not destoryed on exit and become available again when the function is next called. And the keyword is static.
Declaration:
The variable may be local or global.
- Initial Value: Zero
- Storage space: temporary memory (RAM)
- Scope/Visibility: Value of variable persists between different function call.
- Lifetime: if local: lifetime is within that bloc, if global: lifetime is for whole program.
Register variable
Register variable inform the compiler to store the variable in CPU register instead of memory. Register variables have faster accessibility than a normal variable. Generally, the frequently used variable are kept in registers. But only a few variable can be placed inside registers. One application of register storage class can be in using loops, where the variable gets used a number of times in the program, in a very short span of time.
Declaration:
Note: We can never get the address of such variable.
- Initial Value: Any random value i.e. garbage value
- Storage space: CPU register
- Scope/Visibility: Local to the function in which it is declared.
- Lifetime: Till the end of function, in which the variable is defined.
Recursive Function
In C programming, a function is allowed to call itself.
And a function which calls itself again and again until some specified condition is satisfied is known as Recursive Function.
Advantages of function and recursive function
- Function helps in easy understanding, debugging and testing of the program.
- Function reduces size, length and complexity of program.
- Function save time by avoiding repeatation of same code.
- Distributing of work is possible due to function.
- Motivates the programmer to built new powerful program.