Prsentation on functions


Published on

Published in: Education
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Prsentation on functions

  2. 2. The program may become too large and complex and as a result the task of debugging, testing and maintaining becomes difficult. if a program is divided into functional parts , then each part may be independently coded and later combined into single unit .These sub programs are known as functions
  3. 3. <ul><li>Functions are the user defined data types. </li></ul><ul><li>Functions are having modular approach. </li></ul><ul><li>It facilitates top - down modular programming. </li></ul><ul><li>A function may be used by many other programs. </li></ul><ul><li>It is easy to locate and isolate faulty function for further investigations. </li></ul>
  4. 4. <ul><li>Functions are of two types : </li></ul><ul><ul><ul><ul><li>library functions </li></ul></ul></ul></ul><ul><ul><ul><ul><li>User defined functions </li></ul></ul></ul></ul>
  5. 5. <ul><li>main() </li></ul><ul><li>printf() </li></ul><ul><li>scanf() </li></ul><ul><li>getch() </li></ul><ul><li>sqrt() </li></ul><ul><li>cos() </li></ul><ul><li>strcat() </li></ul><ul><li>And etc. </li></ul>
  6. 6. <ul><li>User defined functions are the functions which are defined by the user itself . </li></ul>Main Program Function2 Function3 Function 1
  7. 7. <ul><li>return type <function name > (arguments) </li></ul><ul><li>{ </li></ul><ul><li> local variable declarations ; </li></ul><ul><li> execute statement 1; </li></ul><ul><li>execute statment2 ; </li></ul><ul><li>… .. </li></ul><ul><li> … .. </li></ul><ul><li>return (expression); </li></ul><ul><li>} </li></ul>
  8. 8. <ul><li>A function must follow the same rules of information as other variables names . </li></ul><ul><li>Additional care must be taken to avoid duplicating library routine names or operating system commands . </li></ul>
  9. 9. <ul><li>The arguments may be void. </li></ul><ul><li>The argument list contains valid variable names separated by commas. </li></ul><ul><li>The list must be surrounded by parenthesis . </li></ul><ul><li>No semicolon follows the parenthesis. </li></ul><ul><li>The argument receive value form the calling function ,thus providing a means for data communication from the calling function to the called function. </li></ul><ul><li>All the arguments should be declare with its data type. </li></ul>
  10. 10. <ul><li>Categories of function </li></ul><ul><li>Category 1: Function with no arguments no return value </li></ul><ul><li>Category 2: Functions with arguments and return values </li></ul><ul><li>Category 3: Functions with arguments and return values </li></ul>
  11. 12. <ul><li>Class student // declaration of class </li></ul><ul><li>{ </li></ul><ul><li>char name[15]; </li></ul><ul><li>int rollno; </li></ul><ul><li>float marks; </li></ul><ul><li>public: </li></ul><ul><li>void getdata() // Function declaration </li></ul><ul><li>{ </li></ul><ul><li>cout<< “Input the name of the student” ; </li></ul><ul><li>cin>>name; </li></ul>
  12. 13. <ul><li>cout<< “Input the rollno” ; </li></ul><ul><li>cin>>rollno; </li></ul><ul><li>cout<< “Input the marks of the student” ; </li></ul><ul><li>cin>>marks; </li></ul><ul><li>} </li></ul><ul><li>void putdata() // declaration of another function </li></ul><ul><li>{ </li></ul><ul><li>cout<< “Name = “ <<name<<endl; </li></ul><ul><li>cout<< “Roll no = ” <<rollno<<endl; </li></ul><ul><li>cout<< “Marks = ” <<marks<<endl; </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul>
  13. 14. <ul><li>main() </li></ul><ul><li>{ </li></ul><ul><li>class student obj; </li></ul><ul><li>obj.getdata(); // calling of the function </li></ul><ul><li>obj.putdata(); // calling of the another function </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul>
  14. 15. <ul><li>Input the name of student XYZ </li></ul><ul><li>Input the rollno 1 </li></ul><ul><li>Input the marks of the student 49.02 </li></ul>
  15. 17. <ul><li>#include<iostream.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>class add </li></ul><ul><li>{ </li></ul><ul><li>int c; </li></ul><ul><li>public: </li></ul><ul><li>void sum(int a ,int b) </li></ul><ul><li>{ </li></ul><ul><li>c=a+b ; </li></ul><ul><li>cout<< “The addition is “ <<c; </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul>Actual arguments
  16. 18. <ul><li>main() </li></ul><ul><li>{ </li></ul><ul><li>cout<< “We are in main Function ”; </li></ul><ul><li>add a; </li></ul><ul><li>a.sum(10,20); </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul>Formal Arguments
  17. 19. <ul><li>We are in main Function </li></ul><ul><li>The addition is 30 </li></ul>
  18. 20. <ul><li>The main objective of passing arguments to function is message passing. The message passing is also known as communication between two functions i.e., between caller and callee functions. There are three methods which can pass values to the function:- </li></ul><ul><li>Call by value (Pass by value) </li></ul><ul><li>Call by address (Pass by address) </li></ul><ul><li>Call by reference (pass by reference) </li></ul>
  19. 21. <ul><li>The value of the actual argument is passed to the formal arguments and operation is done on the formal arguments. </li></ul><ul><li>Any change in the formal arguments does not effect the actual arguments because formal arguments are photo copy of the actual arguments. </li></ul><ul><li>When the function is called it does not affect the actual arguments. </li></ul><ul><li>Changes are made in the f0rmal arguments are local to the block of called function </li></ul>
  20. 22. <ul><li>#include<iostream.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>main() </li></ul><ul><li>{ </li></ul><ul><li>void change (int,int); </li></ul><ul><li>int x=10; </li></ul><ul><li>int y=20; </li></ul><ul><li>change(x,y); </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  21. 23. <ul><li>void change (int a , int b) </li></ul><ul><li>{ </li></ul><ul><li>int temp; </li></ul><ul><li>temp=a; </li></ul><ul><li>a=b; </li></ul><ul><li> b=temp; </li></ul><ul><li>cout<<a<<endl<<b; </li></ul><ul><li>} </li></ul>
  22. 24. <ul><li>Instead of passing values address is passed. </li></ul><ul><li>Function operates on address rather than values. </li></ul><ul><li>Formal arguments are pointers to the actual arguments. </li></ul><ul><li>Changes made in the argument are permanent. </li></ul>
  23. 25. <ul><li>#include<iostream.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>main() </li></ul><ul><li>{ </li></ul><ul><li>void change (int * , int *); </li></ul><ul><li>int x=10; </li></ul><ul><li>int y=20; </li></ul><ul><li>change( &x , &y); </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  24. 26. <ul><li>void change (int *a , int *b) </li></ul><ul><li>{ </li></ul><ul><li>int *temp; </li></ul><ul><li>*temp = *a; </li></ul><ul><li>*a = *b; </li></ul><ul><li> *b = *temp; </li></ul><ul><li>cout<<a<<endl<<b; </li></ul><ul><li>} </li></ul>
  25. 27. <ul><li>When a function is declared as inline ,the compiler copies the code of the function in the calling function. </li></ul><ul><li>Function body is inserted in place of function call during compilation. </li></ul><ul><li>Passing of control between caller and callee function is avoided. </li></ul>
  26. 28. <ul><li>The inline function is mostly used when calling of function is small. it is advisable to use the inline function for small programs. </li></ul><ul><li>Inline mechanism increases execution performance in terms of speed.the program using inline function needs more memory space since the inline functions are copied at every point </li></ul>
  27. 29. <ul><li>#include<iostream.h> </li></ul><ul><li>#include<conio.h> </li></ul><ul><li>Inline void sum(int a ,int b) </li></ul><ul><li>{ </li></ul><ul><li>int c; </li></ul><ul><li>c=a+b ; </li></ul><ul><li>cout<< “The addition is “ <<c; </li></ul><ul><li>} </li></ul>
  28. 30. <ul><li>main() </li></ul><ul><li>{ </li></ul><ul><li>cout<< “We are in main Function ”; </li></ul><ul><li>sum(10,20); </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul>
  29. 31. <ul><li>If the functions is recursive. </li></ul><ul><li>Function contain a static variables. </li></ul><ul><li>Function contain control structures. </li></ul><ul><li>Main function can not work as inline. </li></ul>