prsentation on functions

32
GROUP MEMBERS GROUP MEMBERS

Upload: alisha-korpal

Post on 24-Jan-2015

2.167 views

Category:

Education


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Prsentation on functions

GROUP GROUP MEMBERSMEMBERS

Page 2: Prsentation on functions

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

Page 3: Prsentation on 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.

Page 4: Prsentation on functions

Functions are of two types :library functionsUser defined functions

Page 5: Prsentation on functions

main()printf()scanf()getch()sqrt()cos()strcat()And etc.

Page 6: Prsentation on functions

User defined functions are the functions which are defined by the user itself .

Main Program

Function2

Function3

Function 1

Page 7: Prsentation on functions

return type <function name > (arguments)

{

local variable declarations ;

execute statement 1;

execute statment2 ;

…..

…..

return (expression);

}

Page 8: Prsentation on functions

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.

Page 9: Prsentation on functions

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.

Page 10: Prsentation on functions

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

Page 11: Prsentation on functions
Page 12: Prsentation on functions

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;

Page 13: Prsentation on functions

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;

}

};

Page 14: Prsentation on functions

main()

{

class student obj;

obj.getdata(); //calling of the function

obj.putdata(); //calling of the another function

getch();

}

Page 15: Prsentation on functions

Input the name of student XYZ

Input the rollno 1

Input the marks of the student 49.02

Page 16: Prsentation on functions
Page 17: Prsentation on functions

#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

Page 18: Prsentation on functions

main()

{

cout<<“We are in main Function”;

add a;

a.sum(10,20);

getch();

}

Formal Arguments

Page 19: Prsentation on functions

We are in main Function

The addition is 30

Page 20: Prsentation on functions

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)

Page 21: Prsentation on functions

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

Page 22: Prsentation on functions

#include<iostream.h>#include<conio.h>main(){ void change (int,int); int x=10; int y=20; change(x,y); getch();}}

Page 23: Prsentation on functions

void change (int a , int b)

{

int temp;

temp=a;

a=b;

b=temp;

cout<<a<<endl<<b;

}

Page 24: Prsentation on functions

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.

Page 25: Prsentation on functions

#include<iostream.h>#include<conio.h>main(){ void change (int * , int *); int x=10; int y=20; change( &x , &y); getch();}}

Page 26: Prsentation on functions

void change (int *a , int *b)

{

int *temp;

*temp = *a;

*a = *b;

*b = *temp;

cout<<a<<endl<<b;

}

Page 27: Prsentation on functions

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.

Page 28: Prsentation on functions

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

Page 29: Prsentation on functions

#include<iostream.h>

#include<conio.h>

Inline void sum(int a ,int b)

{

int c;

c=a+b ;

cout<<“The addition is “<<c;

}

Page 30: Prsentation on functions

main()

{

cout<<“We are in main Function”;

sum(10,20);

getch();

}

Page 31: Prsentation on functions

If the functions is recursive.Function contain a static variables.Function contain control structures.Main function can not work as inline.

Page 32: Prsentation on functions