We have mentioned earlier that one of the strengths of the C language is C functions. They are easy to define and use. We have used functions in every program.As a pointed out earlier,main is a specially recognized function in C.Every program must have a main function indicate where the program has to begin its executions. If a program is divided into functional parts, then each part is independently coded and late combined into a single unit. These independently coded programs are called subprograms that are much easier to understand, debug and test.In C,such subprogram referred to as functions.
There are two types of functions in C
- library functions
- user-defined functions
In-built Functions or library Function
These functions are provided by the system and stored in the library, therefore it is also called Library Functions.For example:- scanf,printf..
User Defined Functions
These functions have to be developed by the user at the time of writing a program
The main difference between these two categories is that library function is not required to write by us whereas user-defined functions have to be developed by the user at the time of writing a program.
C Code Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <stdio.h> int sum(int a,int b) { int c; c=a+b; return c; } void main() { int i,j,k; printf("Enter element"); scanf("%d%d",&i,&j); k=sum(i,j); printf("\n Sum=%d",k); } |
Parts of Function
- Function Definition
- Function Call
- Function Prototype (function declaration)
Function definition
A function definition also known as function implementation shall include the following element:-
- function name
- list of parameters
- function statement(body)
- return type
A general form of a function definition is:-
1 2 3 4 5 6 7 8 |
return_type function_name( parameter list ) { -------------- function_body; -------------- -------------- } |
Function Call
A function can be called by simple using the function name followed by a list of actual parameters(or argument),if any ,enclosed in parentheses.A general form of function call is:-
1 |
function_name(list of actual parameters); |
When a compiler encounters a function call,the control is transferred to the function definition ,Then function is expecuted line by line and a value is return when a return statement is encountered.
Function Declaration
Like variable, all function in c program must be declared, before they are invoked.A function declaration consists of four parts:-
- function return_type
- function name
- list of parameter
- Termination semi-colon(;)
A general form of function declaration is:-
1 |
return_type function_name(list of actual parameters); |
Recursion
In C, When a function calls itself again and again until breakpoint not found.
Suppose we have such a condition which repeats indefinitely if not stopped, thus recursion needs to have some sort of end condition to break recursion.
Syntax:
1 2 3 4 5 6 |
return_type function_name( parameter list ) { function_name(); } |
C Code Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <stdio.h> int factorial(int x) { if(x <= 1) { return 1; } return x * factorial(x - 1); } int main() { int a,result; printf("Enter a integer: "); scanf("%d", &a); result=factorial(a); printf("\nFactorial of %d = %d", a,result); return 0; } |