Prsentation on functions
Upcoming SlideShare
Loading in...5
×
 

Prsentation on functions

on

  • 2,205 views

 

Statistics

Views

Total Views
2,205
Views on SlideShare
2,204
Embed Views
1

Actions

Likes
2
Downloads
173
Comments
0

1 Embed 1

http://khangvu.dev 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Prsentation on functions Prsentation on functions Presentation Transcript

  • GROUP MEMBERS
  • 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
    • Functions are the user defined data types.
    • Functions are having modular approach.
    • It facilitates top - down modular programming.
    • A function may be used by many other programs.
    • It is easy to locate and isolate faulty function for further investigations.
    • Functions are of two types :
          • library functions
          • User defined functions
    • main()
    • printf()
    • scanf()
    • getch()
    • sqrt()
    • cos()
    • strcat()
    • And etc.
    • User defined functions are the functions which are defined by the user itself .
    Main Program Function2 Function3 Function 1
    • return type <function name > (arguments)
    • {
    • local variable declarations ;
    • execute statement 1;
    • execute statment2 ;
    • … ..
    • … ..
    • return (expression);
    • }
    • A function must follow the same rules of information as other variables names .
    • Additional care must be taken to avoid duplicating library routine names or operating system commands .
    • The arguments may be void.
    • The argument list contains valid variable names separated by commas.
    • The list must be surrounded by parenthesis .
    • No semicolon follows the parenthesis.
    • The argument receive value form the calling function ,thus providing a means for data communication from the calling function to the called function.
    • All the arguments should be declare with its data type.
    • Categories of function
    • Category 1: Function with no arguments no return value
    • Category 2: Functions with arguments and return values
    • Category 3: Functions with arguments and return values
  •  
    • Class student // declaration of class
    • {
    • char name[15];
    • int rollno;
    • float marks;
    • public:
    • void getdata() // Function declaration
    • {
    • cout<< “Input the name of the student” ;
    • cin>>name;
    • cout<< “Input the rollno” ;
    • cin>>rollno;
    • cout<< “Input the marks of the student” ;
    • cin>>marks;
    • }
    • void putdata() // declaration of another function
    • {
    • cout<< “Name = “ <<name<<endl;
    • cout<< “Roll no = ” <<rollno<<endl;
    • cout<< “Marks = ” <<marks<<endl;
    • }
    • };
    • main()
    • {
    • class student obj;
    • obj.getdata(); // calling of the function
    • obj.putdata(); // calling of the another function
    • getch();
    • }
    • Input the name of student XYZ
    • Input the rollno 1
    • Input the marks of the student 49.02
  •  
    • #include<iostream.h>
    • #include<conio.h>
    • class add
    • {
    • int c;
    • public:
    • void sum(int a ,int b)
    • {
    • c=a+b ;
    • cout<< “The addition is “ <<c;
    • }
    • };
    Actual arguments
    • main()
    • {
    • cout<< “We are in main Function ”;
    • add a;
    • a.sum(10,20);
    • getch();
    • }
    Formal Arguments
    • We are in main Function
    • The addition is 30
    • 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:-
    • Call by value (Pass by value)
    • Call by address (Pass by address)
    • Call by reference (pass by reference)
    • The value of the actual argument is passed to the formal arguments and operation is done on the formal arguments.
    • Any change in the formal arguments does not effect the actual arguments because formal arguments are photo copy of the actual arguments.
    • When the function is called it does not affect the actual arguments.
    • Changes are made in the f0rmal arguments are local to the block of called function
    • #include<iostream.h>
    • #include<conio.h>
    • main()
    • {
    • void change (int,int);
    • int x=10;
    • int y=20;
    • change(x,y);
    • getch();
    • }
    • }
    • void change (int a , int b)
    • {
    • int temp;
    • temp=a;
    • a=b;
    • b=temp;
    • cout<<a<<endl<<b;
    • }
    • Instead of passing values address is passed.
    • Function operates on address rather than values.
    • Formal arguments are pointers to the actual arguments.
    • Changes made in the argument are permanent.
    • #include<iostream.h>
    • #include<conio.h>
    • main()
    • {
    • void change (int * , int *);
    • int x=10;
    • int y=20;
    • change( &x , &y);
    • getch();
    • }
    • }
    • void change (int *a , int *b)
    • {
    • int *temp;
    • *temp = *a;
    • *a = *b;
    • *b = *temp;
    • cout<<a<<endl<<b;
    • }
    • When a function is declared as inline ,the compiler copies the code of the function in the calling function.
    • Function body is inserted in place of function call during compilation.
    • Passing of control between caller and callee function is avoided.
    • The inline function is mostly used when calling of function is small. it is advisable to use the inline function for small programs.
    • 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
    • #include<iostream.h>
    • #include<conio.h>
    • Inline void sum(int a ,int b)
    • {
    • int c;
    • c=a+b ;
    • cout<< “The addition is “ <<c;
    • }
    • main()
    • {
    • cout<< “We are in main Function ”;
    • sum(10,20);
    • getch();
    • }
    • If the functions is recursive.
    • Function contain a static variables.
    • Function contain control structures.
    • Main function can not work as inline.
  •