- Introduction to C language functions in Hindi
- Types of functions in C language Hindi
- Create a function using C language in Hindi
Introduction of Functions
C language मे function एक ऐसा method है | जिसे हम अपने according manage कर सकते है | और जरुरत पड़ने पर access भी कर सकते है | और जब भी function बनाते है | तो सबसे पहले data type declare करना पड़ता है | फिर function का एक unique name define करते है |
जब भी Function का name और parameters define करते है | तो उसे function declaration कहा जाता है। और जब हम function में execute होने वाले statements लिखते है | तो उसे function definition कहा जाता है। और जब program में कँही भी function को use करते है | तो उसे function call कहते है।
C language मे Functions create करने से हमारा time और computer की memory बचती है। और जरुरत पड़ने पर एक ही code को बार बार use किया जा सकता है।
C language मे function create करने से Program modules में divide हो जाता है | जिसे आसानी से manage और debug किया जा सकता है। और साथ साथ read भी किया जा सकता है |
C language मे दो प्रकार के function provide किये गये है | पहला Predefined Functions और दूसरा User-defined Functions आईये इन function के बारे मे details से जानने की कोशिश करते है |
- Predefined Functions
- User defined Functions
Predefined Functions
C language मे Predefined functions वह functions होते है | जो library में पहले से ही provide किये गए है। यानी इन function को पहले से ही declare किया गया है। बस Predefined functions use करने के लिए हमे header files include करना होता है। उसके बाद Predefined functions use कर सकते है |
उदाहरण के लिए हम अपने program में printf() और scanf() जैसे functions use करते है | ये दोनों ही predefined functions है। और इन function को use करने के लिए <stdio> header file अपने program में include कर लेते है |
Predefined functions के कुछ example निचे दिये जा रहे है |
- scanf()
- printf()
- gets()
- puts()
User Defined Function
C language मे User Defined Functions वो function होते है | जिसे programmer खुद create करता है | यानी इस type के functions को हम खुद create करते है | और अपने अनुसार use करते है |
C language मे User Defined Functions main() function के बाहर भी create कर सकते है | और main() function के अन्दर भी create कर सकते है |
जब भी हम User Defined Functions create करते है | तो उसका नाम अपने अनुसार रख सकते है | पर ध्यान रहे एक program मे 2 function एक नाम से नही होने चाहिए नही तो error आता है |
C language मे User Defined Functions का नाम main() function नही रख सकते है | क्योकि यह main() function है | जहा से हमारा program execute करना शुरू करता है |
Create a Function
C language में functions create करना काफी आसान है। बस आपको rule पता होना चाहिए की function कैसे create किये जाते है | और इन्हें use कैसे करते है | function के तिन मुख्य steps होते है | जो हम निचे block मे दे रहे है |
- Function Declaration
- Function Definition
- Function Call
Function Declaration
इस part मे हम function का नाम और function का return type declare करते है | और अगर हम चाहे तो function मे parameter भी पास कर सकते है |
- Return-type – हमारे function का return type क्या होगा इसे हम data type के द्वारा define करते है। उदाहरण के लिए मान लीजिये हम एक addition का program बना रहे है | जो 2 whole numbers को add करता है | तो उसका return type int होगा।
- Function-name – यह हमारे function का एक नाम होता है। और यह नाम पुरे program मे unique होना चाहिए। क्योकि जब हम function को call करते है | तो function के नाम को use किया जाता है |
- Parameters – यह उन variables की list होती है | जिसे हम parameter के रूप मे use करते है | अब यह variables int भी हो सकती है | और float भी हो सकती है |
Note :- एक बात आपको हमेशा याद रखनी चाहिए function declaration statement को हम semicolon से break कर सकते है। इसका example निचे दिया जा रहा है |
int addition(int a, int b);
Function Definition
इस part मे हम वह code लिखते है | जिसे execute करना चाहते है | इसे function का body भी कहा जाता है। इसका example निचे दिया जा रहा है |
int addition(int a, int b) { int c; c = a+b; printf("Answer is : %d",c); }
Function Call
Program में जब भी function use करना चाहते है | तो हम उसे call करते है। इसका example निचे दिया जा रहा है |
addition(20,20);
जब भी हम function को call करते है | तो हमे arguments भी पास करने होते है | arguments हम तभी use करते है | जब हम function को declare करते समय parameter को पास किये रहते है | अगर आप function मे parameter पास नही किये है | तो आप function को directly call कर सकते है |
अगर function मे parameter पास किये है | तो आप function को call करते समय argument पास कर सकते है | ऊपर दिये गये code मे 20, 20 एक arguments है | C language में functions को 2 प्रकार से call किया जा सकता है।
- Call by Value
- Call by Reference
Call by Value
जब हम function को call करते समय argument के रूप में कोई value पास करते है | तो वह parameter variables में copy हो जाता है | फिर function के अन्दर जो operation perform किया गया है | उसके अनुसार हमे result मिल जाता है |
इसी प्रकार जब हम function को call करते समय कोई variable पास करते है | तो उस variable की value parameter variable में copy किया जाता है | फिर function के अन्दर जो operation perform किया गया है | उसके अनुसार हमे result मिल जाता है |
अगर आप function को call करते समय variables पास करते है | तो ध्यान रहे जो variables function को call करने के लिए use कर रहे है | वह variables main() function मे declare होना चाहिए | नही तो program मे error आ जाता है |
Example by value
#include<stdio.h> #include<conio.h> int addition(int a, int b); int main() { addition(20,20); return 0; } int addition(int a, int b) { int c; c = a+b; printf("Answer is : %d",c); }
OUT PUT
Answer is : 40
Example by variables
#include<stdio.h> #include<conio.h> int addition(int a, int b); int main() { int x=20, y=20; addition(x,y); return 0; } int addition(int a, int b) { int c; c = a+b; printf("Answer is : %d",c); }
OUT PUT
Answer is : 40
Call by Reference
किसी function को call करने के लिए variables pass करने की बजाय उनका address भी पास किया जा सकता है। जो argument variables को ही point करेगा | इस तरह के function call में arguments को address of operator ( & ) के साथ pass किया जाता है। और parameters को value at ( * ) operator के साथ define किया जाता है। इसका भी example निचे दिया जा रहा है |
#include <stdio.h> #include <conio.h> void swap(int *x, int *y); int main () { int a = 100; int b = 200; printf("Before swap, value of a : %d\n", a ); printf("Before swap, value of b : %d\n", b ); swap(&a, &b); printf("After swap, value of a : %d\n", a ); printf("After swap, value of b : %d\n", b ); return 0; }
OUT PUT
Before swap, value of a :100 Before swap, value of b :200 After swap, value of a :200 After swap, value of b :100
C – 2 D Arrays | Next is Coming Soon |
Previous | Next |
---|