Function के फायदे :
- Function में लिखा हुआ code बार-बार लिखना नहीं पड़ता |
- Function Programmer का समय और Program की space बचाता है |
- बड़े Program को छोटे-छोटे function में विभाजित किया जा सकता है |
- अगर Program में कहा पर error आ जाए तो उसे आसानी से निकाला जा सकता है |
- जहाँ पर जरुरत हो वहाँ पर function को बार-बार call किया जा सकता है |
Function कैसा होता है ?
Function का एक विशिष्ट नाम होता है | Function की शुरुआत में उसका नाम बाद में दो parethesis () और function के statements दो curly braces {} होते है |Function के दो प्रकार है |
- In-built / Predefined Function
- User-defined Function
1. In-built / Predefined Function
- In-built Functions को predefined या Library Functions भी कहते है |
- In-built Functions में हर एक Functions के लिए अलग-अलग header file या preprocessor बनाये गए है |
- Function का declaration, definition header files में होता है |
- C में बहुत सारे Header files है इनमे अलग-अलग functions grouped करके रखे हुए है |
- अगर Programmer चाहे तो अपने खुदके header files भी बना सकता है |
for Example
- printf() : ये Function stdio.h इस header file के अन्तर्गत आता है | अगर Programmer #include <stdio.h> ये preprocessor Program में include नहीं करता तो printf का इस्तेमाल नहीं कर सकता |
- strlen() : ये Function string.h इस header file के अन्तर्गत आता है | अगर Programmer #include <string.h> ये preprocessor Program में include नहीं करता तो strlen का इस्तेमाल नहीं कर सकता |
2. User-defined Function
User-defined Function के तीन विभाग होते है |- Function Declaration
- Function Calling
- Function Definition
1. In Function Declaration
Function create करते वक्त Compiler को बताना पड़ता है की आप कैसा Function create करना चाहते हो, इस process को Function Declaration कहते है |Syntax for Function Declaration
return_type function_name(parameter(s));Function Declaration के चार विभाग है |
- return_type
- function_name
- parameter(s)/argument(s)
- semicolon
1. return_type
हर Function कोई ना कोई value return करता है, वो null (void) हो या numeric(int, float, double) या character(char) | Function का return_type void default होता है |function का return_type program के requirement में होता है |2. function_name
Function का नाम उसके code के अनुसार होना चाहिए | अगर ना भी हो तो भी compiler को चलता है, लेकिन ये Good Programming नहीं कहलाता | Function का नाम C का कोई keyword नहीं होता | C के Function के parenthesis () के अंदर parameters भी होते है |Function का नाम case-sensitive होता है | for eg. hello() और Hello() ये दोनों अलग-अलग functions है |3. parameter(s)/argument(s)
Function के argument data_types होते है या data type के साथ उनके variable के नाम होते है | User किस प्रकार की value function calling के जरिये receive करना चाहता है, ये function के argument में declare होता है |4. semicolon
हर Function के Declaration के बाद semicolon देते है | ये भी function declaration का हिस्सा है |Example for Function Declaration with two parameters
int add(int a,int b); // function declaration
Function Declaration without parameter(s)
int add();
Function Declaration with one parameter
int add(int a);
2. Function Calling
Syntax for Function Calling
function_name(Parameter1 ,Parameter2 ,.Parameter n);Function Calling में सिर्फ function का नाम, function के arguments और semicolon होता है | निचे दिए हुए example function का return type 'integer' है | function का नाम 'add' है और function को दो parameters मतलब a और b पास किये गए है |जब तक function को call नहीं किया जाता तब तक function के declaration और definition को कोई महत्व नहीं रहता |
Example for Function calling with two parameters
add(a, b); // funtion callling
Function calling without parameter(s)
add();
Function calling with one parameter
add( a );
3. Function Definition
Syntax for Function Definition
return_type function_name(Parameter(s)){function_body;
}
Function Definition के चार हिस्से होते है |
1. return_type
हर Function कोई ना कोई value return करता है, वो null (void) हो या numeric(int, float, double) या character(char) | Function का return_type void default होता है |function का return_type program के requirement में होता है |2. function_name
Function का नाम उसके code के अनुसार होना चाहिए | अगर ना भी हो तो भी compiler को चलता है, लेकिन ये Good Programming नहीं कहलाता | Function का नाम C का कोई keyword नहीं होता | C के Function के parenthesis () के अंदर parameters भी होते है |Function का नाम case-sensitive होता है | for eg. hello() और Hello() ये दोनों अलग-अलग functions है |3. parameter(s)/argument(s)
Function के argument data_types होते है या data type के साथ उनके variable के नाम होते है | User किस प्रकार की value function calling के जरिये receive करना चाहता है, ये function के argument में declare होता है |4. function_body
Function body में variables होते है , लेकिन function के अंदर होने के कारण इनका scope Local होता है | Body के अंदर कुछ statement(s) होते है और value return करता है |Example for Function Definition
int add(int x,int y){ // function definition
int z;
z = x + y ;
return z;
}
Full Example for Function
#include <stdio.h>
int add(int a,int b); // function declaration with argument
int main(){
int a,b,c;
printf("Enter value of a and b : ");
scanf("%d %d", &a, &b);
c = add(a,b); // funtion calling
printf("Addition of a and b : %d", c);
return 0;
}
int add(int x,int y){ // function definition
int z;
z = x + y ;
return z;
}
Call By Value और Call By Reference सिखने से पहले Parameters को समझे |
Function में दो प्रकार के Parameters होते है |- Formal Parameter
- Actual Parameter
Formal Parameter
जो parameter function के declaration में और definition में लिखे जाते है, उसे Formal Parameter कहते है |for eg.
void swap(int x, int y); // Formal Parameters
int main(){
int a=2; b=10
swap (a, b)
}
void swap (int x, int y){ //Formal Parameters
------------
------------
}
जो parameter function call में लिखे जाते है, उसे Actual Parameter कहते है |
for eg.
void swap(int x, int y);
int main(){
int a=2; b=10
swap (a, b) //Actual Parameter
}
void swap (int x, int y){
------------
------------
}
Function Calling के दो प्रकार है |
- Call By Value
- Call By Reference
1. Call By Value
- Call By Value में Variable के value को parameter के रूप से function को pass किया जाता है |
- Call By Value में Actual Parameter की value Formal Parameter पर copy की जाती है |
यहाँ पर Program में variable 'a' और 'b' function 'add' को pass किये गए है |
यहाँ पर 'a' और 'b' की values 'x' और 'y' variable पर copy की जाती है |
for eg.
#include <stdio.h>
void swap(int x, int y);
int main(){
int a = 2, b = 10;
printf("Before Swapping a = %d and b = %d\n", a, b);
swap(a, b);
}
void swap(int x, int y)
{
int temp;
temp = x;
x = y;
y = temp;
printf("After Swapping a = %d and b = %d", x, y);
}
2. Call By Reference
- Call By Reference में Variable के address को parameter के रूप से function को pass किया जाता है |
- Call By Value में Actual Parameter की value Formal Parameter पर copy नहीं की जाती है |
यहाँ पर Program में variable 'a' और 'b' address को function 'add' को pass किये गए है |
यहाँ पर 'a' और 'b' की values 'x' और 'y' variable पर copy नहीं की जाती है |
ये सिर्फ variables का address hold करके रखता है |
for eg.
#include <stdio.h>
void swap(int *x, int *y);
int main(){
int a = 2, b = 10;
printf("Before Swapping a = %d and b = %d\n", a, b);
swap(&a, &b);
}
void swap(int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
printf("After Swapping a = %d and b = %d", *x, *y);
}







No comments: