Lecture21 categoriesof userdefinedfunctions.ppt
-
Upload
eshikshak -
Category
Technology
-
view
1.731 -
download
3
description
Transcript of Lecture21 categoriesof userdefinedfunctions.ppt
*
Categories of User Defined Functions in ‘C’
Prakash KhaireLecturer, B V Patel Inst. of BMC & IT, Gopal
Vidyanagar
Prakash Khaire, Lecturer
B V Patel Inst. Of BMC & IT, Gopal Vidyanagar
*
Category 1
Category 2
Category 3
Introduction
Category 4
Category 5
Recursion
Introduction● Functions are categorized based on the arguments are
passed or whether a value is returned or not
●Functions with no agruments and no return values.●Functions with arguments and no return values.●Functions with arguments and one return value.●Functions with no arguments but return a vlues.●Functions that return multiple values
Prakash Khaire, Lecturer B V Patel Inst. Of BMC & IT, Gopal VidyanagarPrakash Khaire, Lecturer B V Patel Inst. Of BMC & IT, Gopal Vidyanagar
*
Category 1
Category 2
Category 3
Introduction
Category 4
Category 5
Recursion
Functions with no arguments and no return values
●You do not pass data to the called function.●function with no return type does not pass back data to the calling function.●This type of function which does not return any value cannot be used in an expression it can be used only as independent statement.
Prakash Khaire, Lecturer B V Patel Inst. Of BMC & IT, Gopal VidyanagarPrakash Khaire, Lecturer B V Patel Inst. Of BMC & IT, Gopal Vidyanagar
*
Category 1
Category 2
Category 3
Introduction
Category 4
Category 5
Recursion
Functions with no arguments and no return values
#include<stdio.h> #include<conio.h> void printline(){ int i; printf("\n"); for(i=0;i<30;i++) { printf("-"); } printf("\n"); }
Prakash Khaire, Lecturer B V Patel Inst. Of BMC & IT, Gopal Vidyanagar
void main() { clrscr(); printf("Welcome to function in C"); printline(); printf("Function easy to learn."); printline(); getch(); }
Prakash Khaire, Lecturer B V Patel Inst. Of BMC & IT, Gopal Vidyanagar
*
Category 1
Category 2
Category 3
Introduction
Category 4
Category 5
Recursion
Functions with no arguments and no return values
Prakash Khaire, Lecturer B V Patel Inst. Of BMC & IT, Gopal VidyanagarPrakash Khaire, Lecturer B V Patel Inst. Of BMC & IT, Gopal Vidyanagar
*
Category 1
Category 2
Category 3
Introduction
Category 4
Category 5
Recursion
Functions with arguments and no return value
●It accept data from calling function.●you send data to the called function from calling function but you cannot send result data back to the calling function.
Prakash Khaire, Lecturer B V Patel Inst. Of BMC & IT, Gopal VidyanagarPrakash Khaire, Lecturer B V Patel Inst. Of BMC & IT, Gopal Vidyanagar
*
Category 1
Category 2
Category 3
Introduction
Category 4
Category 5
Recursion
Functions with arguments and no return value
#include<stdio.h> #include<conio.h> void add(int x, int y) { int result; result = x+y; printf("Sum of %d and %d is %d.\n\n",x,y,result); }
Prakash Khaire, Lecturer B V Patel Inst. Of BMC & IT, Gopal Vidyanagar
void main() { clrscr(); add(30,15); add(63,49); add(952,321); getch(); }
Prakash Khaire, Lecturer B V Patel Inst. Of BMC & IT, Gopal Vidyanagar
*
Category 1
Category 2
Category 3
Introduction
Category 4
Category 5
Recursion
Functions with arguments and no return value
Prakash Khaire, Lecturer B V Patel Inst. Of BMC & IT, Gopal VidyanagarPrakash Khaire, Lecturer B V Patel Inst. Of BMC & IT, Gopal Vidyanagar
*
Category 1
Category 2
Category 3
Introduction
Category 4
Category 5
Recursion
Functions with arguments and return value
●It can send arguments (data) from the calling function to the called function●It wait for the result to be returned back from the called function back to the calling function●This type of function is mostly used in programming world because it can do two way communications●It can accept data as arguments as well as can send back data as return value●The data returned by the function can be used later in our program for further calculations.
Prakash Khaire, Lecturer B V Patel Inst. Of BMC & IT, Gopal VidyanagarPrakash Khaire, Lecturer B V Patel Inst. Of BMC & IT, Gopal Vidyanagar
*
Category 1
Category 2
Category 3
Introduction
Category 4
Category 5
Recursion
Functions with arguments and return value
#include<stdio.h> #include<conio.h> int add(int x, int y) { int result; result = x+y; return(result); }
Prakash Khaire, Lecturer B V Patel Inst. Of BMC & IT, Gopal Vidyanagar
void main() { int z; clrscr(); z = add(952,321); printf("Result %d.\n\n",add(30,55)); printf("Result %d.\n\n",z); getch(); }
Prakash Khaire, Lecturer B V Patel Inst. Of BMC & IT, Gopal Vidyanagar
*
Category 1
Category 2
Category 3
Introduction
Category 4
Category 5
Recursion
Functions with arguments and return value
Prakash Khaire, Lecturer B V Patel Inst. Of BMC & IT, Gopal VidyanagarPrakash Khaire, Lecturer B V Patel Inst. Of BMC & IT, Gopal Vidyanagar
*
Category 1
Category 2
Category 3
Introduction
Category 4
Category 5
Recursion
Functions with no arguments but returns value
●A function which does not take any argument but only returns values to the calling function.●The best example of this type of function is “getchar()” library function which is declared in the header file “stdio.h”.
Prakash Khaire, Lecturer B V Patel Inst. Of BMC & IT, Gopal VidyanagarPrakash Khaire, Lecturer B V Patel Inst. Of BMC & IT, Gopal Vidyanagar
*
Category 1
Category 2
Category 3
Introduction
Category 4
Category 5
Recursion
Functions with no arguments but returns value
#include<stdio.h> #include<conio.h> int send() { int no1; printf("Enter a no : "); scanf("%d",&no1); return(no1); }
Prakash Khaire, Lecturer B V Patel Inst. Of BMC & IT, Gopal Vidyanagar
void main() { int z; clrscr(); z = send(); printf("\nYou entered : %d.", z); getch(); }
Prakash Khaire, Lecturer B V Patel Inst. Of BMC & IT, Gopal Vidyanagar
*
Category 1
Category 2
Category 3
Introduction
Category 4
Category 5
Recursion
Functions with no arguments but returns value
Prakash Khaire, Lecturer B V Patel Inst. Of BMC & IT, Gopal VidyanagarPrakash Khaire, Lecturer B V Patel Inst. Of BMC & IT, Gopal Vidyanagar
*
Category 1
Category 2
Category 3
Introduction
Category 4
Category 5
Recursion
Functions that return multiple values
●To send values to the called function, in the same way we can also use arguments to send back information to the calling function●The arguments that are used to send back data are called Output Parameters.
Prakash Khaire, Lecturer B V Patel Inst. Of BMC & IT, Gopal VidyanagarPrakash Khaire, Lecturer B V Patel Inst. Of BMC & IT, Gopal Vidyanagar
*
Category 1
Category 2
Category 3
Introduction
Category 4
Category 5
Recursion
Functions that return multiple values
#include<stdio.h> #include<conio.h> void calc(int x, int y, int *add, int *sub) { *add = x+y; *sub = x-y; }
Prakash Khaire, Lecturer B V Patel Inst. Of BMC & IT, Gopal Vidyanagar
void main() { int a=20, b=11, p,q; clrscr(); calc(a,b,&p,&q); printf("Sum = %d, Sub = %d",p,q); getch(); }
Prakash Khaire, Lecturer B V Patel Inst. Of BMC & IT, Gopal Vidyanagar
*
Category 1
Category 2
Category 3
Introduction
Category 4
Category 5
Recursion
Recursion
Prakash Khaire, Lecturer B V Patel Inst. Of BMC & IT, Gopal Vidyanagar
●A function calling itself again and again to compute a value●Normally function are called by main function or by some another function●But in recursion the same function is called by itself repeatedly
Prakash Khaire, Lecturer B V Patel Inst. Of BMC & IT, Gopal Vidyanagar
*
Category 1
Category 2
Category 3
Introduction
Category 4
Category 5
Recursion
Closingint fact(int k) 1st call -> return(4 * fact(4-1));{ return(4 * fact(3));
if(k==0) 2nd call -> return(3 * fact(3-1));{ return(3 * fact(2));
return(1); 3rd call -> return(2 * fact(2-1));} return(2 * fact(1));else{
return(k * fact(k-1));}
}
Prakash Khaire, Lecturer B V Patel Inst. Of BMC & IT, Gopal VidyanagarPrakash Khaire, Lecturer B V Patel Inst. Of BMC & IT, Gopal Vidyanagar
*
Category 1
Category 2
Category 3
Introduction
Category 4
Category 5
Recursion
Use of Recursive Function
Prakash Khaire, Lecturer B V Patel Inst. Of BMC & IT, Gopal Vidyanagar
●Recursive functions are written with less number of statements compared functions●Recursion is effective where terms are generated successively to compute a value
Prakash Khaire, Lecturer B V Patel Inst. Of BMC & IT, Gopal Vidyanagar