SlideShare una empresa de Scribd logo
1 de 6
Descargar para leer sin conexión
By : Rakesh Kumar                                           D.A.V. Centenary Public School, Chander Nagar


                                              Function
Function is a small sub program , which is designed to perform a particular task in a complete program and
it is designed in this way that it can be coupled with another function.

return type function_name ( argument)
           {
                statement;                             function Syntax
           }

where
        return type           : Data type of the value return by the function
        function_name         : Any Valid identifier
        Statement             : Any Valid C/C++ statement(s)

Our first Function : To print “hello world” on the screen

void print_message(void)                                    1. void at the position of return type shows that
{                                                              the function does not return any value to it’s
    cout<<”n Hello world”;                                    calling function.
    return ;                                                2. Void at the position of argument shows that
}                                                              the function does not accept any argument.

NOTE : return is not compulsory, but good programming skill recommends every function should have a
return.

                           How to implement this function in a C++ program

Method -1                               Method-2
#include<iostream>                      #include<iostream>
#include<conio.h>                       #include<conio.h>
using namespace std;                    using namespace std;
void print_message(void)                void print_message(void);          // function     prototype
{                                       int main()
     cout<<"n Hello world";            {
     return;                                print_message();               // function call
     }                                      getch();
int main()                                  return 0;
{                                       }
    print_message();
    getch();                            void print_message(void)
    return 0;                           {
}                                            cout<<"n Hello world";
                                             return;
                                             }



Function Prototype: A function prototype in C or C++ is a declaration of a function that omits the function
                   body but does specify the function's name, argument types and return type. While a
                    function definition specifies what a function does, a function prototype can be
                    thought of as specifying its interface.
By : Rakesh Kumar                                          D.A.V. Centenary Public School, Chander Nagar



                                                  TYPE-I

        Main function                                        User defined Function


                                                             Input Phase

          USE UDF                                            Processing Phase
        function here
                                                             Output Phase



 Problem : Write a function to read base and height of a triangle and find out area of a right angle
 Triangle ( Area = ½ *b*h). Also implement this function in a C++ program.

Solution
Program                                                Output
#include<iostream>
#include<conio.h>
using namespace std;
void area_triangle(void)
{
    int b,h,ar;
    system("cls"); // clrscr()
    cout<<"n Enter base :";
    cin>>b;
    cout<<"n Enter height :";
    cin>>h;
    ar =0.5*b*h;
    cout<<"n Area of Triangle :"<<ar;
    return;
}
int main()
  {
    area_triangle(); // function Call
    getch();
    return 0;
}



NOTE : main( ) is also a function. It has the following feature
      without main program can not execute.
      A program can have more than one function but can not have more than one main ( )
      Program execution begin from main( )

                                                 TYPE –II
        Main Function                                                User defined function

                                                                        1.      Input
                                                                        2.      Processing
                                                                        3.      result must
                                                                                return to it’s
        Output Here                                                             calling
                                                                                function
By : Rakesh Kumar                                           D.A.V. Centenary Public School, Chander Nagar


Type –II Problem : Write a function in C++ to read base and height of a right angle triangle, calculate
area of triangle using formula area = ½*b*h and return it to it’s calling function. Also implement this
function in a C++ program
Solution
 Program                                           Output
 #include<iostream>
 #include<conio.h>
 using namespace std;
 int area_triangle(void)
   {
     int b,h,ar;
     cout<<"n Enter base :";
     cin>>b;
     cout<<"n Enter Height :";
     cin>>h;
     ar =int(0.5*b*h);
     return (ar);
   }
 int main()
   {
     int res =area_triangle();
     cout<<"n Area of Triangle :"<<res;
     getch();
     return 0;
 }

                                                   TYPE-III


            Main function                                        USER defined function

            1.     Input                                           2. Processing

                                                                    3. Output



Type –III Problem : Define a function in C++ Area_triangle( ) which accept two integer type parameter
(i) int base (ii) int height. This function calculates area of Triangle using formula area = 1/2*base*height and
also display this calculated area on the screen. Also implement this function in C++ program
Solution

 Program                                                  Output
 #include<iostream>
 #include<conio.h>
 using namespace std;
 void area_triangle(int base, int height)
   {
     int ar;
     ar =int(0.5*base*height);
     cout<<"n Area of Triangle :"<<ar;
   }
 int main()
   {
     area_triangle(10,20);
     getch();
     return 0;
 }
By : Rakesh Kumar                                         D.A.V. Centenary Public School, Chander Nagar



Type –IV                               Input Travels from main to UDF

               Main Function                                 User defined Function

                  1.   Input
                                                                 2.    Processing
                  3. Output                                            Phase




                                        Result Travels from UDF to Main and used inside main


TYPE –IV Problem : Write a user defined function in C++ Area_Triangle( ) which accept two integer type
parameter (i) int base (ii) int height. This function calculates area of triangle using formula area =
½*base*height and return it to it’s calling function. Also implement this function in C++ program
Solution
Problem                                                             Output
#include<iostream>
#include<conio.h>
using namespace std;
int area_triangle(int base, int height)
  {
    int ar;
    ar =int(0.5*base*height);
    return(ar);
  }
int main()
  {
    int res =area_triangle(10,20);
    cout<<"n Area of Triangle :"<<res;
    getch();
    return 0;
}

Parameter Types

   • Call by Value method
   • Call By reference method
Call By Value Method: In this method actual parameter make it’s copy and send to formal parameter.
The processing inside function use this copy of actual parameter. So the changes made inside function does
not automatically available to it’s calling function.

Program                                                    Output
#include<iostream>
#include<conio.h>
using namespace std;
void add ( int a) // formal Parameter
   {
       a = a+10;
   }
int main()
 {
     int x=10;
       cout<<"nBefore function call x :"<<x;
       add(x);
       cout<<"nafter function call x :"<<x;
       getch();
By : Rakesh Kumar                                          D.A.V. Centenary Public School, Chander Nagar

        return 0;
}

Call By reference Method : In this method actual parameter pass the address of actual parameter. So the
changes made inside function is automatically available to it’s calling function.
Program                                                   output
#include<iostream>
#include<conio.h>
using namespace std;
void add ( int &a) // Call by reference
    {
        a = a+10;
    }
int main()
  {
      int x=10;
        cout<<"nBefore function call x :"<<x;
        add(x);
        cout<<"nafter function call x :"<<x;
        getch();
        return 0;
}

NOTE : in case of reference type parameter, actual
parameter must be of variable type


Scope of Variable
   • Auto / Local Variable : The variable whose life begins within opening curly braces and it dies at
       the position of it’s corresponding curly braces, is called local variable
   • Global Variable : The variable whose scope is whole program , and is defined outside function ,is
      called global variable
Program                                                  Output
#include<iostream>
#include<conio.h>
using namespace std;
int a=20;             // Global Variable
int main()
  {
     int a=10;       // Local Variable
        {
             int a=30;
             cout<<"n Value of a :"<<::a;
          }
       cout<<"n Value of a :"<<a;
       getch();
       return 0;
  }
    •Static Variable     : These are local variable to that function where it is defind , but does not loose
     their values between the function call.
Example Program                                      Output
#include<iostream>
#include<conio.h>
using namespace std;
void show(void)
  {
    static int a=0;
    a++;
    cout<<"n Value of a :"<<a;
}
int main()
    {
By : Rakesh Kumar                                        D.A.V. Centenary Public School, Chander Nagar

      show();
      show();
      show();
        getch();
        return 0;
  }



Parameter with default Values
Exmaple Program                                       Output
#include<iostream>
#include<conio.h>
using namespace std;
void show(int a=10)
  {
    cout<<"n Value of a :"<<a;
  }
int main()
  {
    show();
    show(30);
    getch();
    return 0;
}



Some Additional Definition_______________________________________________________________

Formal Parameter : The parameter which appears with function prototype is called formal parameter.
Actual parameter : The Parameter which is used at the time of call function , is called actual parameter
Example
       #include<iostream>
       #include<conio.h>
       void show ( int a) // here a is formal parameter
        {
             ………….
            ………….
         }
       int main( )
          {
              int x =10;
               Show (x) ; // x is here actual parameter.
               ………….
               …………
            }

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

C and C++ functions
C and C++ functionsC and C++ functions
C and C++ functions
 
C++ Functions
C++ FunctionsC++ Functions
C++ Functions
 
C++
C++C++
C++
 
46630497 fun-pointer-1
46630497 fun-pointer-146630497 fun-pointer-1
46630497 fun-pointer-1
 
Le langage rust
Le langage rustLe langage rust
Le langage rust
 
Function lecture
Function lectureFunction lecture
Function lecture
 
C++ functions presentation by DHEERAJ KATARIA
C++ functions presentation by DHEERAJ KATARIAC++ functions presentation by DHEERAJ KATARIA
C++ functions presentation by DHEERAJ KATARIA
 
Function in c language(defination and declaration)
Function in c language(defination and declaration)Function in c language(defination and declaration)
Function in c language(defination and declaration)
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Lecture17
Lecture17Lecture17
Lecture17
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Computer Programming- Lecture 8
Computer Programming- Lecture 8Computer Programming- Lecture 8
Computer Programming- Lecture 8
 
C++ Function
C++ FunctionC++ Function
C++ Function
 
Virtual Functions
Virtual FunctionsVirtual Functions
Virtual Functions
 
Virtual function
Virtual functionVirtual function
Virtual function
 
C function
C functionC function
C function
 
C++11
C++11C++11
C++11
 
Function in c
Function in cFunction in c
Function in c
 
Function in c program
Function in c programFunction in c program
Function in c program
 
Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)
 

Similar a Function notes

Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C ProgrammingShuvongkor Barman
 
Functions in C++ programming language.pptx
Functions in  C++ programming language.pptxFunctions in  C++ programming language.pptx
Functions in C++ programming language.pptxrebin5725
 
Programming Fundamentals Functions in C and types
Programming Fundamentals  Functions in C  and typesProgramming Fundamentals  Functions in C  and types
Programming Fundamentals Functions in C and typesimtiazalijoono
 
UNIT3.pptx
UNIT3.pptxUNIT3.pptx
UNIT3.pptxNagasaiT
 
function in in thi pdf you will learn what is fu...
function in  in thi pdf you will learn   what                           is fu...function in  in thi pdf you will learn   what                           is fu...
function in in thi pdf you will learn what is fu...kushwahashivam413
 
(Polymorphism-OperatorOverLoadingUsingFriendFunction).pdf
(Polymorphism-OperatorOverLoadingUsingFriendFunction).pdf(Polymorphism-OperatorOverLoadingUsingFriendFunction).pdf
(Polymorphism-OperatorOverLoadingUsingFriendFunction).pdfAakashBerlia1
 
Lab 9 sem ii_12_13
Lab 9 sem ii_12_13Lab 9 sem ii_12_13
Lab 9 sem ii_12_13alish sha
 
Cs2312 OOPS LAB MANUAL
Cs2312 OOPS LAB MANUALCs2312 OOPS LAB MANUAL
Cs2312 OOPS LAB MANUALPrabhu D
 

Similar a Function notes (20)

Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Functions in C++ programming language.pptx
Functions in  C++ programming language.pptxFunctions in  C++ programming language.pptx
Functions in C++ programming language.pptx
 
Function in C program
Function in C programFunction in C program
Function in C program
 
Programming Fundamentals Functions in C and types
Programming Fundamentals  Functions in C  and typesProgramming Fundamentals  Functions in C  and types
Programming Fundamentals Functions in C and types
 
UNIT3.pptx
UNIT3.pptxUNIT3.pptx
UNIT3.pptx
 
function in in thi pdf you will learn what is fu...
function in  in thi pdf you will learn   what                           is fu...function in  in thi pdf you will learn   what                           is fu...
function in in thi pdf you will learn what is fu...
 
C++ manual Report Full
C++ manual Report FullC++ manual Report Full
C++ manual Report Full
 
Fp201 unit5 1
Fp201 unit5 1Fp201 unit5 1
Fp201 unit5 1
 
(Polymorphism-OperatorOverLoadingUsingFriendFunction).pdf
(Polymorphism-OperatorOverLoadingUsingFriendFunction).pdf(Polymorphism-OperatorOverLoadingUsingFriendFunction).pdf
(Polymorphism-OperatorOverLoadingUsingFriendFunction).pdf
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
Functionincprogram
FunctionincprogramFunctionincprogram
Functionincprogram
 
Lecture2.ppt
Lecture2.pptLecture2.ppt
Lecture2.ppt
 
function_v1.ppt
function_v1.pptfunction_v1.ppt
function_v1.ppt
 
function_v1.ppt
function_v1.pptfunction_v1.ppt
function_v1.ppt
 
Lab 9 sem ii_12_13
Lab 9 sem ii_12_13Lab 9 sem ii_12_13
Lab 9 sem ii_12_13
 
Unit 3 (1)
Unit 3 (1)Unit 3 (1)
Unit 3 (1)
 
Functions
FunctionsFunctions
Functions
 
Cs2312 OOPS LAB MANUAL
Cs2312 OOPS LAB MANUALCs2312 OOPS LAB MANUAL
Cs2312 OOPS LAB MANUAL
 
functions of C++
functions of C++functions of C++
functions of C++
 

Más de Hitesh Wagle

48695528 the-sulphur-system
48695528 the-sulphur-system48695528 the-sulphur-system
48695528 the-sulphur-systemHitesh Wagle
 
Fundamentals of data structures ellis horowitz & sartaj sahni
Fundamentals of data structures   ellis horowitz & sartaj sahniFundamentals of data structures   ellis horowitz & sartaj sahni
Fundamentals of data structures ellis horowitz & sartaj sahniHitesh Wagle
 
Applicationof datastructures
Applicationof datastructuresApplicationof datastructures
Applicationof datastructuresHitesh Wagle
 
Google search tips
Google search tipsGoogle search tips
Google search tipsHitesh Wagle
 
Applicationof datastructures
Applicationof datastructuresApplicationof datastructures
Applicationof datastructuresHitesh Wagle
 
Lecture notes on infinite sequences and series
Lecture notes on infinite sequences and seriesLecture notes on infinite sequences and series
Lecture notes on infinite sequences and seriesHitesh Wagle
 
Switkes01200543268
Switkes01200543268Switkes01200543268
Switkes01200543268Hitesh Wagle
 
Quote i2 cns_cnr_25064966
Quote i2 cns_cnr_25064966Quote i2 cns_cnr_25064966
Quote i2 cns_cnr_25064966Hitesh Wagle
 

Más de Hitesh Wagle (20)

Zinkprinter
ZinkprinterZinkprinter
Zinkprinter
 
48695528 the-sulphur-system
48695528 the-sulphur-system48695528 the-sulphur-system
48695528 the-sulphur-system
 
Fundamentals of data structures ellis horowitz & sartaj sahni
Fundamentals of data structures   ellis horowitz & sartaj sahniFundamentals of data structures   ellis horowitz & sartaj sahni
Fundamentals of data structures ellis horowitz & sartaj sahni
 
Diode logic crkts
Diode logic crktsDiode logic crkts
Diode logic crkts
 
Applicationof datastructures
Applicationof datastructuresApplicationof datastructures
Applicationof datastructures
 
Oops index
Oops indexOops index
Oops index
 
Google search tips
Google search tipsGoogle search tips
Google search tips
 
Diode logic crkts
Diode logic crktsDiode logic crkts
Diode logic crkts
 
Computer
ComputerComputer
Computer
 
Applicationof datastructures
Applicationof datastructuresApplicationof datastructures
Applicationof datastructures
 
Green chem 2
Green chem 2Green chem 2
Green chem 2
 
Convergence tests
Convergence testsConvergence tests
Convergence tests
 
Lecture notes on infinite sequences and series
Lecture notes on infinite sequences and seriesLecture notes on infinite sequences and series
Lecture notes on infinite sequences and series
 
Switkes01200543268
Switkes01200543268Switkes01200543268
Switkes01200543268
 
Cryptoghraphy
CryptoghraphyCryptoghraphy
Cryptoghraphy
 
Quote i2 cns_cnr_25064966
Quote i2 cns_cnr_25064966Quote i2 cns_cnr_25064966
Quote i2 cns_cnr_25064966
 
Pointers
PointersPointers
Pointers
 
P1
P1P1
P1
 
Notes
NotesNotes
Notes
 
Inheritance
InheritanceInheritance
Inheritance
 

Último

It will be International Nurses' Day on 12 May
It will be International Nurses' Day on 12 MayIt will be International Nurses' Day on 12 May
It will be International Nurses' Day on 12 MayNZSG
 
Pharma Works Profile of Karan Communications
Pharma Works Profile of Karan CommunicationsPharma Works Profile of Karan Communications
Pharma Works Profile of Karan Communicationskarancommunications
 
B.COM Unit – 4 ( CORPORATE SOCIAL RESPONSIBILITY ( CSR ).pptx
B.COM Unit – 4 ( CORPORATE SOCIAL RESPONSIBILITY ( CSR ).pptxB.COM Unit – 4 ( CORPORATE SOCIAL RESPONSIBILITY ( CSR ).pptx
B.COM Unit – 4 ( CORPORATE SOCIAL RESPONSIBILITY ( CSR ).pptxpriyanshujha201
 
Mysore Call Girls 8617370543 WhatsApp Number 24x7 Best Services
Mysore Call Girls 8617370543 WhatsApp Number 24x7 Best ServicesMysore Call Girls 8617370543 WhatsApp Number 24x7 Best Services
Mysore Call Girls 8617370543 WhatsApp Number 24x7 Best ServicesDipal Arora
 
A DAY IN THE LIFE OF A SALESMAN / WOMAN
A DAY IN THE LIFE OF A  SALESMAN / WOMANA DAY IN THE LIFE OF A  SALESMAN / WOMAN
A DAY IN THE LIFE OF A SALESMAN / WOMANIlamathiKannappan
 
KYC-Verified Accounts: Helping Companies Handle Challenging Regulatory Enviro...
KYC-Verified Accounts: Helping Companies Handle Challenging Regulatory Enviro...KYC-Verified Accounts: Helping Companies Handle Challenging Regulatory Enviro...
KYC-Verified Accounts: Helping Companies Handle Challenging Regulatory Enviro...Any kyc Account
 
MONA 98765-12871 CALL GIRLS IN LUDHIANA LUDHIANA CALL GIRL
MONA 98765-12871 CALL GIRLS IN LUDHIANA LUDHIANA CALL GIRLMONA 98765-12871 CALL GIRLS IN LUDHIANA LUDHIANA CALL GIRL
MONA 98765-12871 CALL GIRLS IN LUDHIANA LUDHIANA CALL GIRLSeo
 
Mondelez State of Snacking and Future Trends 2023
Mondelez State of Snacking and Future Trends 2023Mondelez State of Snacking and Future Trends 2023
Mondelez State of Snacking and Future Trends 2023Neil Kimberley
 
Understanding the Pakistan Budgeting Process: Basics and Key Insights
Understanding the Pakistan Budgeting Process: Basics and Key InsightsUnderstanding the Pakistan Budgeting Process: Basics and Key Insights
Understanding the Pakistan Budgeting Process: Basics and Key Insightsseri bangash
 
VIP Call Girls Gandi Maisamma ( Hyderabad ) Phone 8250192130 | ₹5k To 25k Wit...
VIP Call Girls Gandi Maisamma ( Hyderabad ) Phone 8250192130 | ₹5k To 25k Wit...VIP Call Girls Gandi Maisamma ( Hyderabad ) Phone 8250192130 | ₹5k To 25k Wit...
VIP Call Girls Gandi Maisamma ( Hyderabad ) Phone 8250192130 | ₹5k To 25k Wit...Suhani Kapoor
 
Call Girls In DLf Gurgaon ➥99902@11544 ( Best price)100% Genuine Escort In 24...
Call Girls In DLf Gurgaon ➥99902@11544 ( Best price)100% Genuine Escort In 24...Call Girls In DLf Gurgaon ➥99902@11544 ( Best price)100% Genuine Escort In 24...
Call Girls In DLf Gurgaon ➥99902@11544 ( Best price)100% Genuine Escort In 24...lizamodels9
 
Famous Olympic Siblings from the 21st Century
Famous Olympic Siblings from the 21st CenturyFamous Olympic Siblings from the 21st Century
Famous Olympic Siblings from the 21st Centuryrwgiffor
 
The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...
The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...
The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...Aggregage
 
Call Girls In Holiday Inn Express Gurugram➥99902@11544 ( Best price)100% Genu...
Call Girls In Holiday Inn Express Gurugram➥99902@11544 ( Best price)100% Genu...Call Girls In Holiday Inn Express Gurugram➥99902@11544 ( Best price)100% Genu...
Call Girls In Holiday Inn Express Gurugram➥99902@11544 ( Best price)100% Genu...lizamodels9
 
0183760ssssssssssssssssssssssssssss00101011 (27).pdf
0183760ssssssssssssssssssssssssssss00101011 (27).pdf0183760ssssssssssssssssssssssssssss00101011 (27).pdf
0183760ssssssssssssssssssssssssssss00101011 (27).pdfRenandantas16
 
Event mailer assignment progress report .pdf
Event mailer assignment progress report .pdfEvent mailer assignment progress report .pdf
Event mailer assignment progress report .pdftbatkhuu1
 
Cracking the Cultural Competence Code.pptx
Cracking the Cultural Competence Code.pptxCracking the Cultural Competence Code.pptx
Cracking the Cultural Competence Code.pptxWorkforce Group
 
Best Basmati Rice Manufacturers in India
Best Basmati Rice Manufacturers in IndiaBest Basmati Rice Manufacturers in India
Best Basmati Rice Manufacturers in IndiaShree Krishna Exports
 
Call Girls In Panjim North Goa 9971646499 Genuine Service
Call Girls In Panjim North Goa 9971646499 Genuine ServiceCall Girls In Panjim North Goa 9971646499 Genuine Service
Call Girls In Panjim North Goa 9971646499 Genuine Serviceritikaroy0888
 
9599632723 Top Call Girls in Delhi at your Door Step Available 24x7 Delhi
9599632723 Top Call Girls in Delhi at your Door Step Available 24x7 Delhi9599632723 Top Call Girls in Delhi at your Door Step Available 24x7 Delhi
9599632723 Top Call Girls in Delhi at your Door Step Available 24x7 DelhiCall Girls in Delhi
 

Último (20)

It will be International Nurses' Day on 12 May
It will be International Nurses' Day on 12 MayIt will be International Nurses' Day on 12 May
It will be International Nurses' Day on 12 May
 
Pharma Works Profile of Karan Communications
Pharma Works Profile of Karan CommunicationsPharma Works Profile of Karan Communications
Pharma Works Profile of Karan Communications
 
B.COM Unit – 4 ( CORPORATE SOCIAL RESPONSIBILITY ( CSR ).pptx
B.COM Unit – 4 ( CORPORATE SOCIAL RESPONSIBILITY ( CSR ).pptxB.COM Unit – 4 ( CORPORATE SOCIAL RESPONSIBILITY ( CSR ).pptx
B.COM Unit – 4 ( CORPORATE SOCIAL RESPONSIBILITY ( CSR ).pptx
 
Mysore Call Girls 8617370543 WhatsApp Number 24x7 Best Services
Mysore Call Girls 8617370543 WhatsApp Number 24x7 Best ServicesMysore Call Girls 8617370543 WhatsApp Number 24x7 Best Services
Mysore Call Girls 8617370543 WhatsApp Number 24x7 Best Services
 
A DAY IN THE LIFE OF A SALESMAN / WOMAN
A DAY IN THE LIFE OF A  SALESMAN / WOMANA DAY IN THE LIFE OF A  SALESMAN / WOMAN
A DAY IN THE LIFE OF A SALESMAN / WOMAN
 
KYC-Verified Accounts: Helping Companies Handle Challenging Regulatory Enviro...
KYC-Verified Accounts: Helping Companies Handle Challenging Regulatory Enviro...KYC-Verified Accounts: Helping Companies Handle Challenging Regulatory Enviro...
KYC-Verified Accounts: Helping Companies Handle Challenging Regulatory Enviro...
 
MONA 98765-12871 CALL GIRLS IN LUDHIANA LUDHIANA CALL GIRL
MONA 98765-12871 CALL GIRLS IN LUDHIANA LUDHIANA CALL GIRLMONA 98765-12871 CALL GIRLS IN LUDHIANA LUDHIANA CALL GIRL
MONA 98765-12871 CALL GIRLS IN LUDHIANA LUDHIANA CALL GIRL
 
Mondelez State of Snacking and Future Trends 2023
Mondelez State of Snacking and Future Trends 2023Mondelez State of Snacking and Future Trends 2023
Mondelez State of Snacking and Future Trends 2023
 
Understanding the Pakistan Budgeting Process: Basics and Key Insights
Understanding the Pakistan Budgeting Process: Basics and Key InsightsUnderstanding the Pakistan Budgeting Process: Basics and Key Insights
Understanding the Pakistan Budgeting Process: Basics and Key Insights
 
VIP Call Girls Gandi Maisamma ( Hyderabad ) Phone 8250192130 | ₹5k To 25k Wit...
VIP Call Girls Gandi Maisamma ( Hyderabad ) Phone 8250192130 | ₹5k To 25k Wit...VIP Call Girls Gandi Maisamma ( Hyderabad ) Phone 8250192130 | ₹5k To 25k Wit...
VIP Call Girls Gandi Maisamma ( Hyderabad ) Phone 8250192130 | ₹5k To 25k Wit...
 
Call Girls In DLf Gurgaon ➥99902@11544 ( Best price)100% Genuine Escort In 24...
Call Girls In DLf Gurgaon ➥99902@11544 ( Best price)100% Genuine Escort In 24...Call Girls In DLf Gurgaon ➥99902@11544 ( Best price)100% Genuine Escort In 24...
Call Girls In DLf Gurgaon ➥99902@11544 ( Best price)100% Genuine Escort In 24...
 
Famous Olympic Siblings from the 21st Century
Famous Olympic Siblings from the 21st CenturyFamous Olympic Siblings from the 21st Century
Famous Olympic Siblings from the 21st Century
 
The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...
The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...
The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...
 
Call Girls In Holiday Inn Express Gurugram➥99902@11544 ( Best price)100% Genu...
Call Girls In Holiday Inn Express Gurugram➥99902@11544 ( Best price)100% Genu...Call Girls In Holiday Inn Express Gurugram➥99902@11544 ( Best price)100% Genu...
Call Girls In Holiday Inn Express Gurugram➥99902@11544 ( Best price)100% Genu...
 
0183760ssssssssssssssssssssssssssss00101011 (27).pdf
0183760ssssssssssssssssssssssssssss00101011 (27).pdf0183760ssssssssssssssssssssssssssss00101011 (27).pdf
0183760ssssssssssssssssssssssssssss00101011 (27).pdf
 
Event mailer assignment progress report .pdf
Event mailer assignment progress report .pdfEvent mailer assignment progress report .pdf
Event mailer assignment progress report .pdf
 
Cracking the Cultural Competence Code.pptx
Cracking the Cultural Competence Code.pptxCracking the Cultural Competence Code.pptx
Cracking the Cultural Competence Code.pptx
 
Best Basmati Rice Manufacturers in India
Best Basmati Rice Manufacturers in IndiaBest Basmati Rice Manufacturers in India
Best Basmati Rice Manufacturers in India
 
Call Girls In Panjim North Goa 9971646499 Genuine Service
Call Girls In Panjim North Goa 9971646499 Genuine ServiceCall Girls In Panjim North Goa 9971646499 Genuine Service
Call Girls In Panjim North Goa 9971646499 Genuine Service
 
9599632723 Top Call Girls in Delhi at your Door Step Available 24x7 Delhi
9599632723 Top Call Girls in Delhi at your Door Step Available 24x7 Delhi9599632723 Top Call Girls in Delhi at your Door Step Available 24x7 Delhi
9599632723 Top Call Girls in Delhi at your Door Step Available 24x7 Delhi
 

Function notes

  • 1. By : Rakesh Kumar D.A.V. Centenary Public School, Chander Nagar Function Function is a small sub program , which is designed to perform a particular task in a complete program and it is designed in this way that it can be coupled with another function. return type function_name ( argument) { statement; function Syntax } where return type : Data type of the value return by the function function_name : Any Valid identifier Statement : Any Valid C/C++ statement(s) Our first Function : To print “hello world” on the screen void print_message(void) 1. void at the position of return type shows that { the function does not return any value to it’s cout<<”n Hello world”; calling function. return ; 2. Void at the position of argument shows that } the function does not accept any argument. NOTE : return is not compulsory, but good programming skill recommends every function should have a return. How to implement this function in a C++ program Method -1 Method-2 #include<iostream> #include<iostream> #include<conio.h> #include<conio.h> using namespace std; using namespace std; void print_message(void) void print_message(void); // function prototype { int main() cout<<"n Hello world"; { return; print_message(); // function call } getch(); int main() return 0; { } print_message(); getch(); void print_message(void) return 0; { } cout<<"n Hello world"; return; } Function Prototype: A function prototype in C or C++ is a declaration of a function that omits the function body but does specify the function's name, argument types and return type. While a function definition specifies what a function does, a function prototype can be thought of as specifying its interface.
  • 2. By : Rakesh Kumar D.A.V. Centenary Public School, Chander Nagar TYPE-I Main function User defined Function Input Phase USE UDF Processing Phase function here Output Phase Problem : Write a function to read base and height of a triangle and find out area of a right angle Triangle ( Area = ½ *b*h). Also implement this function in a C++ program. Solution Program Output #include<iostream> #include<conio.h> using namespace std; void area_triangle(void) { int b,h,ar; system("cls"); // clrscr() cout<<"n Enter base :"; cin>>b; cout<<"n Enter height :"; cin>>h; ar =0.5*b*h; cout<<"n Area of Triangle :"<<ar; return; } int main() { area_triangle(); // function Call getch(); return 0; } NOTE : main( ) is also a function. It has the following feature without main program can not execute. A program can have more than one function but can not have more than one main ( ) Program execution begin from main( ) TYPE –II Main Function User defined function 1. Input 2. Processing 3. result must return to it’s Output Here calling function
  • 3. By : Rakesh Kumar D.A.V. Centenary Public School, Chander Nagar Type –II Problem : Write a function in C++ to read base and height of a right angle triangle, calculate area of triangle using formula area = ½*b*h and return it to it’s calling function. Also implement this function in a C++ program Solution Program Output #include<iostream> #include<conio.h> using namespace std; int area_triangle(void) { int b,h,ar; cout<<"n Enter base :"; cin>>b; cout<<"n Enter Height :"; cin>>h; ar =int(0.5*b*h); return (ar); } int main() { int res =area_triangle(); cout<<"n Area of Triangle :"<<res; getch(); return 0; } TYPE-III Main function USER defined function 1. Input 2. Processing 3. Output Type –III Problem : Define a function in C++ Area_triangle( ) which accept two integer type parameter (i) int base (ii) int height. This function calculates area of Triangle using formula area = 1/2*base*height and also display this calculated area on the screen. Also implement this function in C++ program Solution Program Output #include<iostream> #include<conio.h> using namespace std; void area_triangle(int base, int height) { int ar; ar =int(0.5*base*height); cout<<"n Area of Triangle :"<<ar; } int main() { area_triangle(10,20); getch(); return 0; }
  • 4. By : Rakesh Kumar D.A.V. Centenary Public School, Chander Nagar Type –IV Input Travels from main to UDF Main Function User defined Function 1. Input 2. Processing 3. Output Phase Result Travels from UDF to Main and used inside main TYPE –IV Problem : Write a user defined function in C++ Area_Triangle( ) which accept two integer type parameter (i) int base (ii) int height. This function calculates area of triangle using formula area = ½*base*height and return it to it’s calling function. Also implement this function in C++ program Solution Problem Output #include<iostream> #include<conio.h> using namespace std; int area_triangle(int base, int height) { int ar; ar =int(0.5*base*height); return(ar); } int main() { int res =area_triangle(10,20); cout<<"n Area of Triangle :"<<res; getch(); return 0; } Parameter Types • Call by Value method • Call By reference method Call By Value Method: In this method actual parameter make it’s copy and send to formal parameter. The processing inside function use this copy of actual parameter. So the changes made inside function does not automatically available to it’s calling function. Program Output #include<iostream> #include<conio.h> using namespace std; void add ( int a) // formal Parameter { a = a+10; } int main() { int x=10; cout<<"nBefore function call x :"<<x; add(x); cout<<"nafter function call x :"<<x; getch();
  • 5. By : Rakesh Kumar D.A.V. Centenary Public School, Chander Nagar return 0; } Call By reference Method : In this method actual parameter pass the address of actual parameter. So the changes made inside function is automatically available to it’s calling function. Program output #include<iostream> #include<conio.h> using namespace std; void add ( int &a) // Call by reference { a = a+10; } int main() { int x=10; cout<<"nBefore function call x :"<<x; add(x); cout<<"nafter function call x :"<<x; getch(); return 0; } NOTE : in case of reference type parameter, actual parameter must be of variable type Scope of Variable • Auto / Local Variable : The variable whose life begins within opening curly braces and it dies at the position of it’s corresponding curly braces, is called local variable • Global Variable : The variable whose scope is whole program , and is defined outside function ,is called global variable Program Output #include<iostream> #include<conio.h> using namespace std; int a=20; // Global Variable int main() { int a=10; // Local Variable { int a=30; cout<<"n Value of a :"<<::a; } cout<<"n Value of a :"<<a; getch(); return 0; } •Static Variable : These are local variable to that function where it is defind , but does not loose their values between the function call. Example Program Output #include<iostream> #include<conio.h> using namespace std; void show(void) { static int a=0; a++; cout<<"n Value of a :"<<a; } int main() {
  • 6. By : Rakesh Kumar D.A.V. Centenary Public School, Chander Nagar show(); show(); show(); getch(); return 0; } Parameter with default Values Exmaple Program Output #include<iostream> #include<conio.h> using namespace std; void show(int a=10) { cout<<"n Value of a :"<<a; } int main() { show(); show(30); getch(); return 0; } Some Additional Definition_______________________________________________________________ Formal Parameter : The parameter which appears with function prototype is called formal parameter. Actual parameter : The Parameter which is used at the time of call function , is called actual parameter Example #include<iostream> #include<conio.h> void show ( int a) // here a is formal parameter { …………. …………. } int main( ) { int x =10; Show (x) ; // x is here actual parameter. …………. ………… }