C Functions :
Introduction to C Functions
เคเค เคฌाเคฐ เคเคชเคे program เคฎें เคुเค เคเคธे statements เคนो เคธเคเคคे เคนै เคिเคจ्เคนें เคเคชเคो compiler เคธे เคฌाเคฐ เคฌाเคฐ execute เคเคฐเคตाเคจे เคी เคเคตเคถ्เคฏเคเคคा เคนोเคคी เคนै। เคเคธी situation เคฎें เคเคจ statements เคो เคฌाเคฐ เคฌाเคฐ เคฒिเคเคจे เคฎें เคฌเคนुเคค เคธเคฎเคฏ เคฒเค เคธเคเคคा เคนै। เคธाเคฅ เคนी เคเคธเคธे program เคฌเคนुเคค เคฌเฅा เคนो เคाเคคा เคนै เคिเคธเคธे เคเคธे เคชเฅเคจे เคเคฐ เคธเคฎเคเคจे เคฎें เคฎुเคถ्เคिเคฒ เคนोเคคी เคนै।
เคเคธ situation เคธे เคฌเคเคจे เคे เคฒिเค C เคเคชเคो เคเค mechanism provide เคเคฐเคคी เคนै เคिเคธे function เคเคนเคคे เคนै। เคिเคจ เคญी statements เคो เคเคช program เคฎें เคเค เคเคเคน เคฌाเคฐ เคฌाเคฐ execute เคเคฐเคตाเคจा เคाเคนเคคे เคนै เคเคจ्เคนें เคเค block เคฎें เคฒिเคเคคे เคนै เคเคฐ เคเคธ block เคो เคเค unique เคจाเคฎ เคฆेเคคे เคนै।
เคเคธเคे เคฌाเคฆ program เคฎें เคँเคนा เคญी เคिเคคเคจी เคญी เคฌाเคฐ เคเคจ statements เคो เคเคช execute เคเคฐเคตाเคจा เคाเคนเคคे เคนै เคคเคฌ เคเคช function เคे เคเคธ unique เคจाเคฎ เคे เคฆ्เคตाเคฐा เคเคธ function เคो call เคเคฐเคคे เคนै। Call เคเคฐเคคे เคนी function (block) เคฎें เคฒिเคे เคเค เคธเคญी statements execute เคนो เคाเคคे เคนै।
Function เคा name เคเคฐ parameters (เคเคจเคे เคฌाเคฐे เคฎें เคเคช เคเคे เคชเคข़ेंเคे) เคเคฌ เคเคช เคฒिเคเคคे เคนै เคคो เคเคธे function declaration เคเคนा เคाเคคा เคนै। เคเคฌ เคเคช function เคฎें execute เคนोเคจे เคตाเคฒे เคธเคญी statements เคฒिเคเคคे เคนै เคคो เคเคธे function definition เคเคนा เคाเคคा เคนै। เคเคฌ เคเคช เคชूเคฐे program เคฎें เคँเคนी เคญी function เคो use เคเคฐเคคे เคนै เคคो เคเคธे function call เคเคนा เคाเคคा เคนै।
เคिเคธी เคญी program เคฎें functions เคो use เคเคฐเคจे เคी advantages เคจीเคे เคฆी เคा เคฐเคนी เคนै।
- Functions create เคเคฐเคจे เคธे programmer เคा time เคเคฐ computer เคी memory เคฌเคเคคी เคนै।
- เคเค เคนी code เคो เคเคธाเคจी เคธे เคฌाเคฐ เคฌाเคฐ use เคिเคฏा เคा เคธเคเคคा เคนै। เคเคธเคธे code re-usability เคฌเคข़เคคी เคนै।
- Program modules เคฎें divide เคนो เคाเคคा เคนै เคिเคธเคธे เคเคธे เคเคธाเคจी เคธे manage เคเคฐ debug เคिเคฏा เคा เคธเคเคคा เคนै।
- Program เคी readability เคฌเคข़เคคी เคนै।
C language เคฎें เคฆो เคช्เคฐเคाเคฐ เคे functions เคนोเคคे เคนै।
- Predefined Functions
- User-defined Functions
PREDEFINED FUNCTIONS
Predefined functions or Built in function เคตो functions เคนोเคคे เคนै เคो C library เคฎें เคชเคนเคฒे เคธे เคนी provide เคिเคฏे เคเค เคนै। เคเคจ function เคो เคชเคนเคฒे เคธे เคนी declare เคเคฐ define เคिเคฏा เคเคฏा เคนोเคคा เคนै। เคฌเคธ เคเคจ्เคนें use เคเคฐเคจे เคे เคฒिเค header files เคो include เคเคฐเคจा เคนोเคคा เคนै
เคเคฆाเคนเคฐเคฃ เคे เคฒिเค เคฏเคฆि เคเคช เค
เคชเคจे program เคฎें scanf() เคเคฐ printf() เคैเคธे functions use เคเคฐเคจा เคाเคนเคคे เคนै เคคो เคเคธเคे เคฒिเค เคเคช <stdio> header file เคो เค
เคชเคจे program เคฎें include เคเคฐเคคे เคนै। เคฏे เคฆोเคจों เคนी predefined functions เคนै।
Predefined functions เคे เคुเค เคเคฆाเคนเคฐเคฃ –
- scanf()
- printf()
- strcpy()
- void *malloc()
- int to lower() เคเคฆि เคนै।
USER DEFINED FUNCTIONS
User defined functions เคตो function เคนोเคคे เคนै เคो programmer (เคเคช) เคुเคฆ create เคเคฐเคคे เคนै। Programmer เค
เคชเคจी เคเคตเคถ्เคฏเคเคคा เคे เค
เคจुเคธाเคฐ เคिเคคเคจे เคญी functions create เคเคฐ เคธเคเคคा เคนै। เคฏे functions เคैเคธे create เคिเคฏे เคाเคคे เคนै เคเคฐ เคเคจ्เคนें เคैเคธे use เคिเคฏा เคाเคคा เคนै।
Creating and Using C Functions
C เคฎें functions create เคเคฐเคจा เคเคฐ เคเคจ्เคนें use เคเคฐเคจा เคฌเคนुเคค เคนी เคเคธाเคจ เคนै। เคเคธ process เคे 3 เคฎुเค्เคฏ steps เคนोเคคे เคนै।
- Functions Declaration
- Function Definition
- Function Call
FUNCTION DECLARATION
เคเคธ part เคฎें เคเคช function เคा เคจाเคฎ, เคเคธเคा return type เคเคฐ parameters define เคเคฐเคคे เคนै। Function declaration เคा general syntax เคจीเคे เคฆिเคฏा เคเคฏा เคนै।
<return-type> <function-name>(<list-of-parameters>);
|
- return-type – เคเคชเคा function execution complete เคนोเคจे เคชเคฐ เคिเคธ เคช्เคฐเคाเคฐ เคी value return เคเคฐेเคा เคฏे เคเคช return type เคे เคฆ्เคตाเคฐा define เคเคฐเคคे เคนै। เคฏเคฆि เคเคช เคเค addition เคा program เคฌเคจा เคฐเคนे เคนै เคो 2 whole numbers เคो add เคเคฐเคคा เคนै เคคो เคเคชเคा return type int เคนोเคा।
- function-name – เคฏे เคเคชเคे function เคा เคจाเคฎ เคนोเคคा เคนै। เคฏे เคชूเคฐे program เคฎें unique เคนोเคจा เคाเคนिเค। เคเคฌ เคเคช function เคो call เคเคฐเคคे เคนै เคคो เคเคธ เคจाเคฎ เคो เคนी เคฒिเคเคคे เคนै।
- list-of-parameters – เคฏे เคเคจ variables เคी list เคนोเคคी เคนै เคो เคเคช function เคो call เคเคฐเคคे เคธเคฎเคฏ เคชाเคธ เคเคฐेंเคे। เคैเคธे เคी เคฏเคฆि เคเคช addition เคा function เคฌเคจा เคฐเคนे เคนै เคคो parameters เคे เคฐूเคช เคฎें เคเคช 2 numbers เคฏा 2 variables เคชाเคธ เคเคฐ เคธเคเคคे เคนै เคเคฐ เคซिเคฐ function เคे เค ंเคฆเคฐ เคเคจเคो add เคเคฐเคे result show เคเคฐ เคธเคเคคे เคนै। เคฏเคน เคเคตเคถ्เคฏเค เคจเคนीं เคी เคเคช เคธเคญी functions เคฎें parameters define เคเคฐें।
เคเค เคฌाเคค เคเคชเคो เคนเคฎेเคถा เคฏाเคฆ เคฐเคเคจी เคाเคนिเค เคी function declaration statement เคो เคเคช semicolon เคธे terminate เคเคฐเคคे เคนै। เคฒेเคिเคจ function definition เคे เคธाเคฅ เคเคธा เคจเคนीं เคนोเคคा เคนै। เคฎाเคจ เคฒीเคिเคฏे เคी เคเคช addition เคा function เคฌเคจा เคฐเคนे เคนै เคो เคฆो numbers เคो add เคเคฐเคคा เคนै เคคो เคเคธे เคเคช เคเคธ เคช्เคฐเคाเคฐ declare เคเคฐเคคे เคนै।
/* A function declaration with two parameters */
int add(int a, int b); |
FUNCTION DEFINITION
เคเคธ part เคो function body เคญी เคเคนा เคाเคคा เคนै। เคเคธเคฎें เคเคช เคตो statements เคฒिเคเคคे เคนै เคिเคจ्เคนें เคเคช execute เคเคฐเคตाเคจा เคाเคนเคคे เคนै।
<return-type> <function-name>(<list-of-parameters>)
{
statement 1;
statement 2;
.
.
statement N;
}
|
Function definition เคฎें return-type, function-name เคเคฐ list-of-parameters เคเคธी เคช्เคฐเคाเคฐ เคนोเคคे เคนै เคैเคธे เคी function declaration เคฎें เคนोเคคे เคนै। เคเคจเคे เคฌाเคฆ เคฎें curly brackets เคे block เคฎें เคตो statements เคฒिเคे เคाเคคे เคนै เคो เคเคช execute เคเคฐเคตाเคจा เคाเคนเคคे เคนै।
เคฏเคฆि เคเคช addition เคे function เคी เคฌाเคค เคเคฐे เคคो เคเคธเคी definition เคเคช เคเคธ เคช्เคฐเคाเคฐ เคฒिเค เคธเคเคคे เคนै।
/* Definition of add function */
int add(int a, int b)
{
/* Statements to be executed when add function is called */
int c;
c = a+b;
return c;
}
|
FUNCTION CALL
Program เคฎें เคเคฌ เคญी เคเคช function เคो use เคเคฐเคจा เคाเคนเคคे เคนै เคคो เคเคธे call เคเคฐเคคे เคนै।
<function-name>(<arguments-list>);
|
argument-list – arguments เคตो real values เคนोเคคी เคนै เคो เคเคช functions เคो call เคเคฐเคคे เคธเคฎเคฏ เคชाเคธ เคเคฐเคคे เคนै। เคฏे values function definition เคฎें parameters เคो assign เคนोเคคी เคนै। เคเคธเคे เคฌाเคฆ เคเคจ values เคชเคฐ processing เคนोเคคी เคนै เคเคฐ result return เคिเคฏा เคाเคคा เคนै।
Addition เคे function เคो call เคเคฐเคคे เคธเคฎเคฏ เคोเค 2 values เคชाเคธ เคเคฐेंเคे เคैเคธे เคी 2 เคเคฐ 7, เคเคจเคी เคเคเคน variables เคญी pass เคिเคฏे เคा เคธเคเคคे เคนै เคिเคจเคฎें values store เคी เคเคฏी เคนै।
เคฏे values parameter variables a เคเคฐ b เคो assign เคนो เคाเคँเคी เคเคฐ function เคे เค
ंเคฆเคฐ เคเคจ variables เคชเคฐ เคนी processing เคนोเคคी เคนै। เคเคธे functions เคिเคจเคฎें parameters defined เคिเคฏे เคเค เคนै เคเคฐ เคฏเคฆि เคเคช function call เคเคฐเคคे เคธเคฎเคฏ arguments เคชाเคธ เคจเคนीं เคเคฐเคคे เคนै เคคो program เคฎें error เคเคคी เคนै।
Addition เคे function เคो เคเคช เคเคธ เคช्เคฐเคाเคฐ call เคเคฐ เคธเคเคคे เคนै।
/* Calling add function with arguments 2 and 7*/
add(2,7); |
C language เคฎें functions เคो 2 เคช्เคฐเคाเคฐ เคธे call เคिเคฏा เคा เคธเคเคคा เคนै।
- Call by Value – เคเคธ เคคเคฐीเคे เคฎें argument เคे เคฐूเคช เคฎें values เคเคฐ variables เคชाเคธ เคिเคฏे เคाเคคे เคนै।
- Call by Reference – เคเคธ เคคเคฐीเคे เคฎें argument เคे เคฐूเคช เคฎें variables เคा reference เคชाเคธ เคिเคฏा เคाเคคा เคนै।
CALL BY VALUE
เคเคฌ เคเคช function call เคเคฐเคคे เคธเคฎเคฏ argument เคे เคฐूเคช เคฎें เคोเค value เคชाเคธ เคเคฐเคคे เคนै เคคो เคตเคน parameter variables เคฎें copy เคนो เคाเคคी เคนै เคเคฐ เคเคธเคे เคฌाเคฆ เคเคจ variables เคชเคฐ operations perform เคिเคฏे เคाเคคे เคนै।
เคเคธी เคช्เคฐเคाเคฐ เคเคฌ เคเคช function เคो call เคเคฐเคคे เคธเคฎเคฏ เคोเค variable เคชाเคธ เคเคฐเคคे เคนै เคคो เค
เคธเคฒ เคฎें เคตเคน variable function เคฎें เคจเคนीं pass เคिเคฏा เคाเคคा เคนै เคฌเคฒ्เคि เคเคธ variable เคी value parameter variable เคฎें copy เคी เคाเคคी เคนै เคเคฐ เคเคธเคे เคฌाเคฆ เคเคธ parameter variable เคชเคฐ operations perform เคिเคฏे เคाเคคे เคนै।
เคฏเคฆि เคเคชเคฐ เคฆिเค เคเค addition เคे function เคो call เคเคฐเคคे เคธเคฎเคฏ เคฆो integer variables x เคเคฐ y pass เคिเคฏे เคाเคँ เคคो เคเคธा เคเคฐเคจे เคชเคฐ เคเคจ เคฆोเคจों variables เคी value parameter variables a เคเคฐ b เคฎें copy เคนो เคाเคฏेเคी เคเคฐ เคเคจ เคชเคฐ addition perform เคเคฐเคे result return เคिเคฏा เคाเคเคा।
เคเคธ เคคเคฐเคน เคे function call เคो call by value เคเคนा เคाเคคा เคนै। เคเคธเคฎें เค
เคธเคฒ argument variables เคी value change เคจเคนीं เคนोเคคी เคนै เคเคฐ เคो เคญी operation เคนोเคคा เคนै เคตเคน parameter variables เคชเคฐ เคนी perform เคนोเคคा เคนै।
#include<stdio.h>
int add(int a, int b);
int main()
{ int result; int x=5; int y=4;
printf(“This addition is performed using function call by valuen”);
/* Calling add function by value */
result = add(x,y);
printf(“n Result is : %d”,result);
printf(“nn Value of a is %d and value of b is %d”,x,y);
}
/* Below function takes values from variables
as arguments and performs operations on them */
int add(int a, int b)
{ int c; c=a+b; return c; } |
This addition is performed using function call by value
Result is 9
Value a is 5 and value of b is 4
|
CALL BY REFERENCE
เคिเคธी function เคो call เคเคฐเคคे เคธเคฎเคฏ เค
เคธเคฒ variables pass เคเคฐเคจे เคी เคฌเคाเคฏ เคเคจเคा address เคญी เคชाเคธ เคिเคฏा เคा เคธเคเคคा เคนै। เคเคธा เคเคฐเคจे เคชเคฐ เคเคจ variables เคा address parameter variables เคฎें copy เคนोเคा เคเคฐ parameter variable memory เคฎें เค
เคธเคฒ argument variables เคो เคนी point เคเคฐेंเคे।
เคเคธ เคคเคฐเคน เคे function call เคो call by reference เคเคนा เคाเคคा เคนै। เคเคธ เคคเคฐเคน เคे function call เคฎें เค
เคธเคฒ argument variables เคा address pass เคिเคฏा เคाเคคा เคนै। เคเคธा เคเคฐเคจे เคธे function เคे เค
ंเคฆเคฐ เคฏเคฆि parameter variables เคी values เคฎें changes เคเคคे เคนै เคคो เคเคธเคธे เค
เคธเคฒ argument variables เคी values เคฎें เคญी changes เคเคคे เคนै। เคฏाเคจी parameters เคฎें change เคเคจे เคชเคฐ arguments เคญी change เคนो เคाเคคे เคนै।
เคเคธ เคคเคฐเคน เคे function call เคฎें arguments เคो address of operator (&) เคे เคธाเคฅ pass เคिเคฏा เคाเคคा เคนै। เคฏเคน operator argument เคा address parameter variables เคो pass เคเคฐเคคा เคนै। เคเคธเคे เค
เคฒाเคตा function เคे declaration เคเคฐ definition เคฎें parameters เคो value at (*) operator เคे เคธाเคฅ define เคिเคฏा เคाเคคा เคนै।
#include<stdio.h>
/* Function declaration with value at operator */
int add(int *s);
int main()
{ int s=2;
printf(“Call by Reference Demon”);
/* Function call with variable reference */
add(&s);
printf(“Sum is %dn”,s);
}
/* Function definition with value at operator */
int add(int *s) { *s = *s+2; return 0; } |
Call by Reference Demo
Sum is 4 |
Example of C Functions
#include<stdio.h>
/* Function declaration */
int add(int a, int b);
int main()
{
int result;
printf(“This is addition program using function!n”);
/* Function call */
result = add(2,7);
printf(“Result is : %d”, result);
}
/* Function definition */
int add(int a, int b)
{
int c;
c = a + b;
return c;
}
|
This is addition program using function!
Result is : 9
|
Comments
Post a Comment