Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Lecture21 categoriesof userdefinedfunctions.ppt

2,213 views

Published on

categories of user define function

  • Be the first to comment

Lecture21 categoriesof userdefinedfunctions.ppt

  1. 1. Categories of User Defined Functions in ‘C’ Prakash Khaire Lecturer, B V Patel Inst. of BMC & IT, Gopal VidyanagarPrakash Khaire, B V Patel Inst. Of BMC & *Lecturer IT, Gopal Vidyanagar
  2. 2. IntroductionIntroduction Functions are categorized based on the arguments are ● passed or whether a value is returned or notCategory 1Category 2 ●Functions with no agruments and no return values. ●Functions with arguments and no return values.Category 3 ●Functions with arguments and one return value. ●Functions with no arguments but return a vlues.Category 4 ●Functions that return multiple valuesCategory 5Recursion * Prakash Khaire, Prakash Khaire, Lecturer B V Patel Inst. Of B V Patel Inst. Of BMC & Vidyanagar BMC & IT, Gopal IT, Gopal Vidyanagar
  3. 3. Functions with no arguments and no return valuesIntroduction ●You do not pass data to the called function.Category 1 ●function with no return type does not passCategory 2 back data to the calling function. ●This type of function which does not returnCategory 3 any value cannot be used in an expressionCategory 4 it can be used only as independentCategory 5 statement.Recursion * Prakash Khaire, Prakash Khaire, Lecturer B V Patel Inst. Of B V Patel Inst. Of BMC & Vidyanagar BMC & IT, Gopal IT, Gopal Vidyanagar
  4. 4. Functions with no arguments and no return valuesIntroduction #include<stdio.h> #include<conio.h> void main()Category 1 void printline() { { clrscr();Category 2 int i; printf("Welcome to function in C"); printf("n"); printline(); for(i=0;i<30;i++) printf("Function easy to learn.");Category 3 { printline(); printf("-");Category 4 } getch(); printf("n"); }Category 5 }Recursion * Prakash Khaire, Prakash Khaire, Lecturer B V Patel Inst. Of B V Patel Inst. Of BMC & Vidyanagar BMC & IT, Gopal IT, Gopal Vidyanagar
  5. 5. Functions with no arguments and no return valuesIntroductionCategory 1Category 2Category 3Category 4Category 5Recursion * Prakash Khaire, Prakash Khaire, Lecturer B V Patel Inst. Of B V Patel Inst. Of BMC & Vidyanagar BMC & IT, Gopal IT, Gopal Vidyanagar
  6. 6. Functions with arguments and no return valueIntroduction ●It accept data from calling function.Category 1 ●you send data to the called function fromCategory 2 calling function but you cannot send resultCategory 3 data back to the calling function.Category 4Category 5Recursion * Prakash Khaire, Prakash Khaire, Lecturer B V Patel Inst. Of B V Patel Inst. Of BMC & Vidyanagar BMC & IT, Gopal IT, Gopal Vidyanagar
  7. 7. Functions with arguments and no return valueIntroduction #include<stdio.h> void main() #include<conio.h> {Category 1 void add(int x, int y) clrscr(); { add(30,15);Category 2 int result; add(63,49); result = x+y; add(952,321); printf("Sum of %d and %d is %d.nn",x,y, getch();Category 3 result); } }Category 4Category 5Recursion * Prakash Khaire, Prakash Khaire, Lecturer B V Patel Inst. Of B V Patel Inst. Of BMC & Vidyanagar BMC & IT, Gopal IT, Gopal Vidyanagar
  8. 8. Functions with arguments and no return valueIntroductionCategory 1Category 2Category 3Category 4Category 5Recursion * Prakash Khaire, Prakash Khaire, Lecturer B V Patel Inst. Of B V Patel Inst. Of BMC & Vidyanagar BMC & IT, Gopal IT, Gopal Vidyanagar
  9. 9. Functions with arguments and return valueIntroduction ●It can send arguments (data) from the calling function to the called functionCategory 1 ●It wait for the result to be returned back from the called functionCategory 2 back to the calling function ●This type of function is mostly used in programming worldCategory 3 because it can do two way communications ●It can accept data as arguments as well as can send back dataCategory 4 as return value ●The data returned by the function can be used later in ourCategory 5 program for further calculations.Recursion * Prakash Khaire, Prakash Khaire, Lecturer B V Patel Inst. Of B V Patel Inst. Of BMC & Vidyanagar BMC & IT, Gopal IT, Gopal Vidyanagar
  10. 10. Functions with arguments and return valueIntroduction #include<stdio.h> void main() #include<conio.h> {Category 1 int add(int x, int y) int z;Category 2 { clrscr(); int result; z = add(952,321);Category 3 result = x+y; printf("Result %d.nn",add(30,55)); return(result); printf("Result %d.nn",z);Category 4 } getch(); }Category 5Recursion * Prakash Khaire, Prakash Khaire, Lecturer B V Patel Inst. Of B V Patel Inst. Of BMC & Vidyanagar BMC & IT, Gopal IT, Gopal Vidyanagar
  11. 11. Functions with arguments and return valueIntroductionCategory 1Category 2Category 3Category 4Category 5Recursion * Prakash Khaire, Prakash Khaire, Lecturer B V Patel Inst. Of B V Patel Inst. Of BMC & Vidyanagar BMC & IT, Gopal IT, Gopal Vidyanagar
  12. 12. Functions with no arguments but returns valueIntroduction ●A function which does not take anyCategory 1 argument but only returns values to theCategory 2 calling function. ●The best example of this type of function isCategory 3 “getchar()” library function which is declaredCategory 4 in the header file “stdio.h”.Category 5Recursion * Prakash Khaire, Prakash Khaire, Lecturer B V Patel Inst. Of B V Patel Inst. Of BMC & Vidyanagar BMC & IT, Gopal IT, Gopal Vidyanagar
  13. 13. Functions with no arguments but returns valueIntroduction #include<stdio.h> void main() #include<conio.h>Category 1 { int send() int z; {Category 2 clrscr(); int no1; z = send();Category 3 printf("Enter a no : "); printf("nYou entered : %d.", z); scanf("%d",&no1); getch();Category 4 return(no1); } }Category 5Recursion * Prakash Khaire, Prakash Khaire, Lecturer B V Patel Inst. Of B V Patel Inst. Of BMC & Vidyanagar BMC & IT, Gopal IT, Gopal Vidyanagar
  14. 14. Functions with no arguments but returns valueIntroductionCategory 1Category 2Category 3Category 4Category 5Recursion * Prakash Khaire, Prakash Khaire, Lecturer B V Patel Inst. Of B V Patel Inst. Of BMC & Vidyanagar BMC & IT, Gopal IT, Gopal Vidyanagar
  15. 15. Functions that return multiple valuesIntroduction ●To send values to the called function, in theCategory 1 same way we can also use arguments toCategory 2 send back information to the calling function ●The arguments that are used to send backCategory 3 data are called Output Parameters.Category 4Category 5Recursion * Prakash Khaire, Prakash Khaire, Lecturer B V Patel Inst. Of B V Patel Inst. Of BMC & Vidyanagar BMC & IT, Gopal IT, Gopal Vidyanagar
  16. 16. Functions that return multiple valuesIntroduction #include<stdio.h> void main() #include<conio.h>Category 1 { void calc(int x, int y, int int a=20, b=11, p,q; *add, int *sub)Category 2 clrscr(); { calc(a,b,&p,&q);Category 3 *add = x+y; printf("Sum = %d, Sub = %d",p,q); *sub = x-y; getch();Category 4 } }Category 5Recursion * Prakash Khaire, Prakash Khaire, Lecturer B V Patel Inst. Of B V Patel Inst. Of BMC & Vidyanagar BMC & IT, Gopal IT, Gopal Vidyanagar
  17. 17. RecursionIntroduction ●A function calling itself again and again toCategory 1 compute a valueCategory 2 ●Normally function are called by mainCategory 3 function or by some another function ●But in recursion the same function is calledCategory 4 by itself repeatedlyCategory 5Recursion * Prakash Khaire, Prakash Khaire, Lecturer B V Patel Inst. Of B V Patel Inst. Of BMC & Vidyanagar BMC & IT, Gopal IT, Gopal Vidyanagar
  18. 18. ClosingIntroduction int fact(int k) 1st call -> return(4 * fact(4-1)); { return(4 * fact(3));Category 1 if(k==0) 2nd call -> return(3 * fact(3-1)); { return(3 * fact(2));Category 2 return(1); 3rd call -> return(2 * fact(2-1)); } return(2 * fact(1));Category 3 else {Category 4 return(k * fact(k-1)); } }Category 5Recursion * Prakash Khaire, Prakash Khaire, Lecturer B V Patel Inst. Of B V Patel Inst. Of BMC & Vidyanagar BMC & IT, Gopal IT, Gopal Vidyanagar
  19. 19. Use of Recursive FunctionIntroduction ●Recursive functions are written with lessCategory 1 number of statements compared functionsCategory 2 ●Recursion is effective where terms areCategory 3 generated successively to compute a valueCategory 4Category 5Recursion * Prakash Khaire, Prakash Khaire, Lecturer B V Patel Inst. Of B V Patel Inst. Of BMC & Vidyanagar BMC & IT, Gopal IT, Gopal Vidyanagar

×