Showing posts with label Functions. Show all posts
Showing posts with label Functions. Show all posts

Function-III

Menu-driven program using Switch case to calculate the following:
1. Addition
2. Subtraction 
3. Multiplication
4. Division 
5. Modulus 
     
             #include<stdio.h>
             #include<conio.h>
             int addition(int, int ); 
             int subtraction(int, int ); 
             int multiplication(int, int ); 
             int division(int, int);
             int modulus(int, int);                           
             int main(void)
             {   
               int num1, num2,result, choice;
               clrscr();   
               printf("1. Addition\n");
               printf("2. Subtraction\n");
               printf("3. Multiplication\n");
               printf("4. Division\n");
               printf("5. Modulus\n");
               printf("Enter your choice");
               scanf("%d", &choice);
               switch(choice)
               {
                  case 1:
                  printf("Enter two numbers");
                  scanf("%d%d",&num1,&num2);
                  result = addition(num1,num2);
                  printf("Addition=%d",result);
                  break;
                  case 2:
                  printf("Enter two numbers");
                  scanf("%d%d",&num1,&num2);
                  result = subtraction(num1,num2);
                  printf("Subtraction=%d",result);
                  break;
                  case 3:
                  printf("Enter two numbers");
                  scanf("%d%d",&num1,&num2);
                  result = multiplication(num1,num2);
                  printf("Multiplication=%d",result);
                  break;
                  case 4:
                  printf("Enter two numbers");
                  scanf("%d%d",&num1,&num2);
                  result = division(num1,num2);
                  printf("division=%d",result);
                  break;
                  case 5:
                  printf("Enter two numbers");
                  scanf("%d%d",&num1,&num2);
                  result = modulus(num1,num2);
                  printf("Modulus=%d",result);
                  break;
                  default :
                  printf("Wrong choice");
            }
                 getch();
                 return 0;
             }
             int addition(int n1, int n2 )
             {
                 return(n1+n2);
             }

             int subtraction(int n1, int n2 )
              {
                 return(n1-n2);
              } 
             int multiplication(int n1, int n2)
               {
                 return(n1*n2);
               } 
  
             int division(int n1, int n2)
              {
                 return(n1/n2);
              }
             int modulus(int n1, int n2);
             {
                 return(n1%n2);
             }
                                       


Output :
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Modulus
Enter your choice 1
Enter two numbers 10 20
Addition = 30


Function की definition में variable name छोड़ भी सकते है । जैसा कि उपरोक्त प्रोग्राम में है। 

About return statement 
  1. Return a value to the place from the function has been called.
  2. Also return the control from the called function to the calling function.


    /* program to calculate the factorial of a number through function*/      
              #include<stdio.h>
             #include<conio.h>
             int factorial(int n); /* Function prototype or declaration */
             int main(void)

             {   
               int number, result;
               clrscr();   
               printf("Enter the number");
               scanf("%d", &number);
               result = factorial(number) /* Function call */
               printf("Factorial =%d", result);
               getch(); 
               return 0;
             }
        int factorial(int n) /* Function definition*/
        {
           int i,fact=1;
           for(i=1;i<=n;i++)
           {
                 fact = fact*i;
           }           return fact;
        }

Output :
Enter the number 5
Factorial =120





NEXT   : Recursion
PREVIOUS : User defined functions-II

Function -II

2. Function with no parameters and return values 
ये इस तरह के Functions होते है जिसमे न हो कोई parameters होते है परन्तु ये function कोई value return करते है मतलब इन function का return type, कोई int, float, double, char आदि और parameters में void होता है। 
जैसे  


    /* program to find addition of two numbers using functions*/      
              #include<stdio.h>
             #include<conio.h>
             int add(void); /* Function prototype or declaration */
             int main(void)

             {   
               int sum;
               clrscr();   
               sum=add() /* Function call */
               printf("Sum=%d", sum);  
                getch(); 
                return 0;
             }
        int add(void) /* Function definition*/
        {
           int a=10, b=20;
           return(a+b);
        }
Output :
Sum =30


3. Function with parameters and no return values
ये इस तरह के Functions होते है जिसमे parameters होते है परन्तु ये function कोई value return नही करते है मतलब इन function का return type,  void होेता है और parameter, int, float, double, char आदि होते है।
जैसे  

    /* program to find addition of two numbers using functions*/      
              #include<stdio.h>
             #include<conio.h>
             void add(int a, int b); /* Function prototype or declaration */
             int main(void)

             {   
               int num1, num2;
               clrscr();   
               printf("Enter two numbers");
               scanf("%d%d", &num1, &num2);
               add(num1, num2) /* Function call */
               getch(); 
               return 0;
             }
        void add(int a, int b) /* Function definition*/
        {
           int sum;
           sum= a+b;
           printf("Sum =%d", sum);
        }

Output :
Enter two numbers 10 20
Sum =30

उपरोक्त प्रोग्राम दो parameter के लिए है यदि हम चाहे तो  एक से अधिक parameter के लिए भी function बना सकते है।


user defined function

उपरोक्त प्रोग्राम में function call में जो Argument है उन्हे Actual Argument और function definition में जो Argument है उन्हे Formal Argument कहते है। 

4. Function with parameters and return values
ये इस तरह के Functions होते है जिसमे parameters होते है और ये function कोई value return करते है इन functions और return type में parameter, int, float, double, char आदि होते है।
जैसे  

    /* program to find addition of two numbers using functions*/      
              
             #include<stdio.h>
             #include<conio.h>
             int add(int a, int b); /* Function prototype or declaration */
             int main(void)

             {   
               int num1, num2,sum;
               clrscr();   
               printf("Enter two numbers");
               scanf("%d%d", &num1, &num2);
               sum = add(num1, num2) /* Function call */
               printf("Sum =%d", sum);
               getch(); 
               return 0;
             }
        void add(int a, int b) /* Function definition*/
        {
           int s;
           s= a+b;
           return s;
        }


Output :
Enter two numbers 10 20
Sum =30



NEXT   : function -III
PREVIOUS : User defined functions

User defined Functions

Function
A function is predefined set of statements (or instructions) in c that execute for a special task. 
A function is the basic unit of a c program. A function is just like a worker that does a special & particular work. A pair of parenthesis denotes a function i.e. ()

Example of functions
main()
clrscr()
printf()
scanf()
getch()
exit()

Functions are two types
  1. Standard Library Functions (Built in functions)
  2. User defined Functions 
How to create your own functions
Elements of user defined function Function 
  1. Function declaration (Function prototype)
  2. Function definition
  3. Function call 
1. Function declaration 
  • Return type
  • Function name
  • List of arguments or parameter list 
Function declaration में  function का नाम, उसका return type और list of argument or parameter list में बारे में बताते है। 

<Return Type> <Function Name>(Parameter List);


Return type का मतलब Function किस तरह की value return  करेगा जैसे int, float, double इत्यादि,  Function name कुछ भी ले सकते है जैसे square, add इत्यादि परन्तु  function के नाम लेने के rule, variable name rules की तरह होगे और parameter list का मतलब की function में किस तरह value pass होगी जैसे int, float, double इत्यादि  

जैसे 
  int square(int);

यहाँ Start वाला int, function का return type है, square, function का नाम है और parenthesis वाला int, parameter list है। parameter list एक या अधिक हो सकते है। 

2. Function declaration 
Function definition में function के specific कार्य को define करते है । जैसे अगर Square, calculate करने का function बनाना हो को उसकी definition इस तरह लिखेगे। 

int square (int s)
{
   return (s*s);
}

3. Function call
Function call का मतलब function को invoke करना 
जैसे
       square(5);
Note : 
1. प्रत्येक program का execution main function से start होता है।  
2. मान लीजिए हमे एक कोड को कई बार उपयोग करना है इसके लिए हम function का उपयोग करेगे । function का उपयोग करने से हमे एक ही कोड को बार बार नही लिखना पड़ेगा। उसके लिए हम एक Function बनाएगे और जब भी उसकी जरूरत होगी उसको call करा लेगे। हम                      function को कितने ही बार call करा सकते है।   
अब एक उदाहरण के द्वारा समझते है। 
square निकालने के लिए function बनाते है


    /* program to calculate square of a number*/      
              #include<stdio.h>
             #include<conio.h>

             int main(void)

             {   
               int number;
               void square(int); /* Function prototype or declaration */ 
               clrscr();   
               printf("Enter a number");
               scanf("%d", &number);
               square(number) /* Function call */
                getch(); 
                return 0;
             }
        void square(int n) /* Function definition*/
        {
             printf("The square = %d", n*n);
        }

Output :
Enter a number 5
The square =25 
  
उपरोक्त प्रोग्राम में एक square का नाम का  function, void square(int); बनाया है जो एक ही parameter int type का लेता है और  function का return type, void लिया है मतलब function कोई भी value, return नही करेगा और सबसे अन्त में function की definition है। 
Function बनाते है समय एक बात हमेशा याद रखना चाहिए कि function की definition और function declaration का header एक जैसा होना चाहिए । मतलब function जो नाम, return type और parameter list, function declaration में लिये है  उसी के समान function definition में लेना है। जैसा कि उपरोक्त प्रोग्राम में बताया गया है। 

Categories of Function
  1. Functions with no parameters and no return values 
  2. Functions with no parameters and return values
  3. Functions with parameters and no return values
  4. Functions with parameter and return values

1. Function with no parameters and no return values 
ये इस तरह के Functions होते है जिसमे न हो कोई parameters होते है और न ये function कोई value return करते है मतलब इन function का return type और parameters दोनो void होते है। 
जैसे  


    /* program to print simple message  using functions*/      
              #include<stdio.h>
             #include<conio.h>
             void print_msg(void); /* Function prototype or declaration */
             int main(void)

             {   
               clrscr();   
               printf_msg() /* Function call */
                getch(); 
                return 0;
             }
        void print_msg(void) /* Function definition*/
        {
             printf("Hello, C");
        }
Output :
Hello, C 


उपरोक्त प्रोग्राम में एक void print_msg(void) नाम का function है जिसका return type,  void है और उसकी parameter list में भी void है। इसका मतलब है कि function न तो कोई value, return करेगा और न ही कोई parameter accept करेगा । जब function से कोई value return नही कराते तो उसका return type void देते है।

Note : Functions को कितनी भी बार call करा सकते है और एक Program में एक से अधिक भी Function बना सकते है।    

Functions को प्रकार से बना सकते है 
1. Global Functions
2. Local  Functions.

1. Global Function : 
जो function, main() function के ऊपर declare किये जाते है उन्हे Global function कहते है।

2. Local Function :
जो function main() functionकी body के अंदर declare किये जाते है उन्हे Local function कहते है।

NEXT   : User defined functions-II
PREVIOUS : continue statement