functIONS PROGRAMMING CIII II DJDJKASDJKJASD.pptx

nandemprasanna 57 views 31 slides Sep 25, 2024
Slide 1
Slide 1 of 31
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31

About This Presentation

DFSDFFSFSD


Slide Content

Functions in c++ 9/25/2024 By MRI 1

Functions in C++ Functions are used to provide modularity to a program. Creating an application using function makes it easier to understand, edit, check errors etc. Functions allow to structure programs in segments of code to perform individual tasks. In C++, a function is a group of statements that is given a name, and which can be called from some point of the program. 9/25/2024 By MRI 2

Functions in C++ Depending on whether a function is predefined or created by programmer; there are two types of function: Library Function OR Built in function User-defined Function 9/25/2024 By MRI 3

9/25/2024 T ypes 1.Built in functions :- are part of compiler package. Part of standard library made available by compiler. Can be used in any program by including respective header file. 2. User defined functions:- Created by user or programmer. Created as per requirement of the program.

User Defined Functions in C++ Syntax of Function return-type function-name ( parameters ) { // function-body } return-type : suggests what the function will return. It can be int , char, some pointer or even a class object. There can be functions which does not return anything, they are mentioned with void . Function Name : is the name of the function, using the function name it is called. Parameters : are variables to hold values of arguments passed while function is called. A function may or may not contain parameter list. 9/25/2024 By MRI 5

9/25/2024 6 Parts of a function main function { function prototype declaration function call -------; } function declaratory/definition { ------; return statement }

9/25/2024 By MRI 7 Function prototype 4 parts Return type Function name Argument list Terminating semicolon Variable declaration Data_type variable_name ; int x=5; float marks; int price; A function prototype is a declaration of a function that tells the program about the type of value returned by the function, name of function , number and type of arguments. Syntax: Return_type function_name (parameter list/argument); EX: int add( int,int ); void add(void); int add(float,int);

Declaring, Defining and Calling Function #include < iostream > using namespace std ; int sum ( int x, int y); //declaring function int main() { int a = 10; int b = 20; int c = sum (a, b); //calling function cout << c; } int sum ( int x, int y) //defining function { return (X + y); } 9/25/2024 By MRI 8

Function definition Syntax: function_type function_name(parameter list) { Local variable declaration; Function body statement; Return statement; } Funct i on body 2 parts Funct i on header Note: no semicolon in header Exa m p l e: i n t add ( i n t,i nt ); Z=add(x,y); //prototype //function call //function definition int add(int a,int b) { body statement; Return(a+b); P . P . K ris h na r a } j R SET

P . P . K ris h na r aj R SET Function categories Function with no return value and no argument. void add(void); Function with arguments passed and no return value. void add( int,int ); Function with no arguments but returns a value. int add(void); Function with arguments and returns a value. int add(int,int);

I. Function with no return value and no argument void main() { v oid disp(v o id); / / pr o toty p e //caller function disp(); return 0; } void disp() //calle function { cout<<“--------”<<endl; } No arguments passed from caller to calle No value returned from calle to caller function P . P . K ris h na r aj R SET

P . P . K ris h na r aj R SET //program to print square of a number using functions. void main() { void sqr(void); sqr(); getch(); return 0; } void sqr() { int no; cout<<“enter a no.”; cin>>no; cout<<“square of”<<no<<“is”<<no*no; }

ii. Function will not return any value but passes argument #inc l u d e< i os tr ea m . h > #include<conio.h> void add(int,int); int main() { int a,b; cout<<“enter values of a and b”<<endl; cin>>a>>b; add(a,b); getch(); return 0; } void add(int x,int y) { int c; c =x+ y ; cout<<“addition is”<<c; } add ( a,b ); a b void add(int x,int y); P . P . K ris h na r aj R SET

iii) Function with arguments and return value main function { int sqr(int); int a,ans ; cout<<“enter a number”; cin>> a ; ans=sqr(a); cout<<“square of number is”<<ans; getch(); return 0; } //function prototype //function call int sqr(int X) { return(X*X); } //function declaratory/definition P . P . K ris h na r aj R SET

iv) Function with no arguments but returns a value int main() { int add(void); int z; z=add(); cout<<sum of 2 nos is”<<z; getch(); return 0; } int add(void); { int a,b; cout<<“enter 2 nos”; cin>>a>>b; return(a+b); } Function call add(x,y); i.e z=add(x,y); P . P . K ris h na r aj R SET

Calling a Function Functions are called by their names. If the function is without argument, it can be called directly using its name. But for functions with arguments, we have two ways to call them: Call by Value Call by Reference 9/25/2024 By MRI 16

Call by value A function can be invoked in two manners (i)call by value (ii)call by reference The call by value method copies the value of actual parameters into formal parameters i.e the function creates its own copy of arguments and uses them. add(a, b ); Values of variables a and b are passed to X,Y Now if you change the value of X and Y, those changes are not seen in a and b Call by value where the values of variable are passed to functions } void add(int x,int y); { --------; P . P . K ri s } h na r aj R SET

/* program to illustrate the concept of call by value */ # include < iostre a m . h #include<conio.h> void add(int,int); int main() { int a,b; cout<<“enter values of a and b”<<endl; cin>>a>>b; add(a,b); getch(); return 0; } void add(int x,int y) { int c; c = x+ y cout<<“addition is”<<c; }

Call by reference In call by reference method in place of calling a value to the function being called , a reference to the original variable is passed .i.e the same variable value can be accessed by any of the two names. add(a,b); } P . P . K ris h na r aj R SET void add(int &x,int &y); { --------; } In function call W e write refer e nce variable for formal arguments &X,&Y will be the reference variable for a and b. if we change X a nd Y , V a l ue of a and b are changed accordingly No need for return statement

Program to illustrate call by reference #include<iostream.h> #include<conio.h> void swap(int &,int &); int main() { int a,b; cout<<“enter the values of a and b”; cin>>a>>b; cout<<“before swaping”; cout<<“A”<<a; cout<<“B”<<b; swap(a,b); cout<<“after swaping”; cout<<“A”<<a; cout<<“B”<<b; getch(); } void swap(int &X,&Y) { int temp; te m p=X; X=Y; Y=X; } P . P . K ris h na r aj R SET

Inline function Calling a function generally causes a certain overhead (stacking arguments, jumps, etc...), and thus for very short functions, it may be more efficient to simply insert the code of the function where it is called, instead of performing the process of formally calling a function. 9/25/2024 By MRI 21

Inline function Preceding a function declaration with the inline specifier informs the compiler that inline expansion is preferred over the usual function call mechanism for a specific function. This does not change at all the behavior of a function, but is merely used to suggest the compiler that the code generated by the function body shall be inserted at each point the function is called, instead of being invoked with a regular function call. 9/25/2024 By MRI 22

Inline function C++  inline  function is powerful concept that is commonly used with classes. If a function is inline, the compiler places a copy of the code of that function at each point where the function is called at compile time. To inline a function, place the keyword  inline  before the function name and define the function before any calls are made to the function. The compiler can ignore the inline qualifier in case defined function is more than a line. 9/25/2024 By MRI 23

Inline function Following is an example, which makes use of inline function to return max of two numbers: #include < iostream > using namespace std; inline int Max( int x, int y) { return (x > y)? x : y; } // Main function for the program int main( ) { cout << "Max (20,10): " << Max(20,10) << endl ; cout << "Max (0,200): " << Max(0,200) << endl ; cout << "Max (100,1010): " << Max(100,1010) << endl ; return 0; } 9/25/2024 By MRI 24

Friend function A friend function of a class is defined outside that class' scope but it has the right to access all private and protected members of the class. Even though the prototypes for friend functions appear in the class definition, friends are not member functions. A friend can be a function, function template, or member function, or a class or class template, in which case the entire class and all of its members are friends. 9/25/2024 By MRI 25

Friend function To declare a function as a friend of a class, precede the function prototype in the class definition with keyword  friend  as follows: class Box { double width; public: double length; friend void printWidth ( Box box ); void setWidth ( double wid ); }; 9/25/2024 By MRI 26

Friend function #include < iostream > using namespace std ; // forward declaration class B; class A { private: int numA ; public: A(): numA (12) { } // friend function declaration friend int add(A, B); }; class B { private: int numB ; public: B(): numB (1) { } // friend function declaration friend int add(A , B); }; // Function add() is the friend function of classes A and B // that accesses the member variables numA and numB int add(A objectA , B objectB ) { return ( objectA.numA + objectB.numB ); } int main() { A objectA ; B objectB ; cout <<"Sum: "<< add( objectA , objectB ); return 0; } 9/25/2024 By MRI 27

Friend function In this program, classes A and B have declared add() as a friend function . Thus , this function can access private data of both class. Here, add() function adds the private data numA and numB of two objects objectA and objectB , and returns it to the main function. To make this program work properly, a forward declaration of a class class B should be made as shown in the above example. This is because class B is referenced within the class A using code: friend int add(A , B);. 9/25/2024 By MRI 28

Recursive function A function that calls itself is known as recursive function. And, this technique is known as recursion. void recurse () { ... .. ... recurse (); ... .. ... } int main() { ... .. ... recurse (); ... .. ... } 9/25/2024 By MRI 29

9/25/2024 By MRI 30

Examples of Recursive Function // Factorial of n = 1*2*3*...*n #include < iostream > using namespace std ; int factorial( int ); int main() { int n; cout <<"Enter a number to find factorial: "; cin >> n; cout << "Factorial of " << n <<" = " << factorial(n); return 0; } int factorial( int n) { if (n > 1) { return n*factorial(n-1); } else { return 1; } } 9/25/2024 By MRI 31
Tags