SlideShare uma empresa Scribd logo
1 de 6
Baixar para ler offline
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.
               ………….
               …………
            }

Mais conteúdo relacionado

Mais procurados (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)
 

Destaque

Estructura tc mtto_equipos_computo_839312
Estructura tc mtto_equipos_computo_839312Estructura tc mtto_equipos_computo_839312
Estructura tc mtto_equipos_computo_839312jeferpc
 
An Integrated Framework for Parameter-based Optimization of Scientific Workflows
An Integrated Framework for Parameter-based Optimization of Scientific WorkflowsAn Integrated Framework for Parameter-based Optimization of Scientific Workflows
An Integrated Framework for Parameter-based Optimization of Scientific Workflowsvijayskumar
 
Açıköğretimde e-Öğrenme
Açıköğretimde e-ÖğrenmeAçıköğretimde e-Öğrenme
Açıköğretimde e-ÖğrenmeMehmet Emin Mutlu
 
Kādēļ vērts veicināt tehnisko jaunradi? Darba devēju skatījums
Kādēļ vērts veicināt tehnisko jaunradi? Darba devēju skatījumsKādēļ vērts veicināt tehnisko jaunradi? Darba devēju skatījums
Kādēļ vērts veicināt tehnisko jaunradi? Darba devēju skatījumsanitaliice
 
Harnessing the cloud for securely outsourcing large scale systems of linear e...
Harnessing the cloud for securely outsourcing large scale systems of linear e...Harnessing the cloud for securely outsourcing large scale systems of linear e...
Harnessing the cloud for securely outsourcing large scale systems of linear e...Muthu Samy
 
JPN1413 An Energy-Balanced Routing Method Based on Forward-Aware Factor for...
JPN1413   An Energy-Balanced Routing Method Based on Forward-Aware Factor for...JPN1413   An Energy-Balanced Routing Method Based on Forward-Aware Factor for...
JPN1413 An Energy-Balanced Routing Method Based on Forward-Aware Factor for...chennaijp
 

Destaque (9)

50120140503013
5012014050301350120140503013
50120140503013
 
Estructura tc mtto_equipos_computo_839312
Estructura tc mtto_equipos_computo_839312Estructura tc mtto_equipos_computo_839312
Estructura tc mtto_equipos_computo_839312
 
An Integrated Framework for Parameter-based Optimization of Scientific Workflows
An Integrated Framework for Parameter-based Optimization of Scientific WorkflowsAn Integrated Framework for Parameter-based Optimization of Scientific Workflows
An Integrated Framework for Parameter-based Optimization of Scientific Workflows
 
Açıköğretimde e-Öğrenme
Açıköğretimde e-ÖğrenmeAçıköğretimde e-Öğrenme
Açıköğretimde e-Öğrenme
 
Kādēļ vērts veicināt tehnisko jaunradi? Darba devēju skatījums
Kādēļ vērts veicināt tehnisko jaunradi? Darba devēju skatījumsKādēļ vērts veicināt tehnisko jaunradi? Darba devēju skatījums
Kādēļ vērts veicināt tehnisko jaunradi? Darba devēju skatījums
 
Harnessing the cloud for securely outsourcing large scale systems of linear e...
Harnessing the cloud for securely outsourcing large scale systems of linear e...Harnessing the cloud for securely outsourcing large scale systems of linear e...
Harnessing the cloud for securely outsourcing large scale systems of linear e...
 
JPN1413 An Energy-Balanced Routing Method Based on Forward-Aware Factor for...
JPN1413   An Energy-Balanced Routing Method Based on Forward-Aware Factor for...JPN1413   An Energy-Balanced Routing Method Based on Forward-Aware Factor for...
JPN1413 An Energy-Balanced Routing Method Based on Forward-Aware Factor for...
 
Achm corporaciones(1)
Achm corporaciones(1)Achm corporaciones(1)
Achm corporaciones(1)
 
ME2011 presentation by Faci
ME2011 presentation by FaciME2011 presentation by Faci
ME2011 presentation by Faci
 

Semelhante a Function notes 2

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
 

Semelhante a Function notes 2 (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++
 

Mais 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
 

Mais 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

Buy gmail accounts.pdf Buy Old Gmail Accounts
Buy gmail accounts.pdf Buy Old Gmail AccountsBuy gmail accounts.pdf Buy Old Gmail Accounts
Buy gmail accounts.pdf Buy Old Gmail AccountsBuy Verified Accounts
 
India Consumer 2024 Redacted Sample Report
India Consumer 2024 Redacted Sample ReportIndia Consumer 2024 Redacted Sample Report
India Consumer 2024 Redacted Sample ReportMintel Group
 
Innovation Conference 5th March 2024.pdf
Innovation Conference 5th March 2024.pdfInnovation Conference 5th March 2024.pdf
Innovation Conference 5th March 2024.pdfrichard876048
 
Annual General Meeting Presentation Slides
Annual General Meeting Presentation SlidesAnnual General Meeting Presentation Slides
Annual General Meeting Presentation SlidesKeppelCorporation
 
Traction part 2 - EOS Model JAX Bridges.
Traction part 2 - EOS Model JAX Bridges.Traction part 2 - EOS Model JAX Bridges.
Traction part 2 - EOS Model JAX Bridges.Anamaria Contreras
 
8447779800, Low rate Call girls in Tughlakabad Delhi NCR
8447779800, Low rate Call girls in Tughlakabad Delhi NCR8447779800, Low rate Call girls in Tughlakabad Delhi NCR
8447779800, Low rate Call girls in Tughlakabad Delhi NCRashishs7044
 
8447779800, Low rate Call girls in Rohini Delhi NCR
8447779800, Low rate Call girls in Rohini Delhi NCR8447779800, Low rate Call girls in Rohini Delhi NCR
8447779800, Low rate Call girls in Rohini Delhi NCRashishs7044
 
APRIL2024_UKRAINE_xml_0000000000000 .pdf
APRIL2024_UKRAINE_xml_0000000000000 .pdfAPRIL2024_UKRAINE_xml_0000000000000 .pdf
APRIL2024_UKRAINE_xml_0000000000000 .pdfRbc Rbcua
 
8447779800, Low rate Call girls in Kotla Mubarakpur Delhi NCR
8447779800, Low rate Call girls in Kotla Mubarakpur Delhi NCR8447779800, Low rate Call girls in Kotla Mubarakpur Delhi NCR
8447779800, Low rate Call girls in Kotla Mubarakpur Delhi NCRashishs7044
 
FULL ENJOY Call girls in Paharganj Delhi | 8377087607
FULL ENJOY Call girls in Paharganj Delhi | 8377087607FULL ENJOY Call girls in Paharganj Delhi | 8377087607
FULL ENJOY Call girls in Paharganj Delhi | 8377087607dollysharma2066
 
2024 Numerator Consumer Study of Cannabis Usage
2024 Numerator Consumer Study of Cannabis Usage2024 Numerator Consumer Study of Cannabis Usage
2024 Numerator Consumer Study of Cannabis UsageNeil Kimberley
 
Independent Call Girls Andheri Nightlaila 9967584737
Independent Call Girls Andheri Nightlaila 9967584737Independent Call Girls Andheri Nightlaila 9967584737
Independent Call Girls Andheri Nightlaila 9967584737Riya Pathan
 
Case study on tata clothing brand zudio in detail
Case study on tata clothing brand zudio in detailCase study on tata clothing brand zudio in detail
Case study on tata clothing brand zudio in detailAriel592675
 
IoT Insurance Observatory: summary 2024
IoT Insurance Observatory:  summary 2024IoT Insurance Observatory:  summary 2024
IoT Insurance Observatory: summary 2024Matteo Carbone
 
8447779800, Low rate Call girls in New Ashok Nagar Delhi NCR
8447779800, Low rate Call girls in New Ashok Nagar Delhi NCR8447779800, Low rate Call girls in New Ashok Nagar Delhi NCR
8447779800, Low rate Call girls in New Ashok Nagar Delhi NCRashishs7044
 
Investment in The Coconut Industry by Nancy Cheruiyot
Investment in The Coconut Industry by Nancy CheruiyotInvestment in The Coconut Industry by Nancy Cheruiyot
Investment in The Coconut Industry by Nancy Cheruiyotictsugar
 
Memorándum de Entendimiento (MoU) entre Codelco y SQM
Memorándum de Entendimiento (MoU) entre Codelco y SQMMemorándum de Entendimiento (MoU) entre Codelco y SQM
Memorándum de Entendimiento (MoU) entre Codelco y SQMVoces Mineras
 
Call US-88OO1O2216 Call Girls In Mahipalpur Female Escort Service
Call US-88OO1O2216 Call Girls In Mahipalpur Female Escort ServiceCall US-88OO1O2216 Call Girls In Mahipalpur Female Escort Service
Call US-88OO1O2216 Call Girls In Mahipalpur Female Escort Servicecallgirls2057
 

Último (20)

Buy gmail accounts.pdf Buy Old Gmail Accounts
Buy gmail accounts.pdf Buy Old Gmail AccountsBuy gmail accounts.pdf Buy Old Gmail Accounts
Buy gmail accounts.pdf Buy Old Gmail Accounts
 
India Consumer 2024 Redacted Sample Report
India Consumer 2024 Redacted Sample ReportIndia Consumer 2024 Redacted Sample Report
India Consumer 2024 Redacted Sample Report
 
Innovation Conference 5th March 2024.pdf
Innovation Conference 5th March 2024.pdfInnovation Conference 5th March 2024.pdf
Innovation Conference 5th March 2024.pdf
 
Annual General Meeting Presentation Slides
Annual General Meeting Presentation SlidesAnnual General Meeting Presentation Slides
Annual General Meeting Presentation Slides
 
Traction part 2 - EOS Model JAX Bridges.
Traction part 2 - EOS Model JAX Bridges.Traction part 2 - EOS Model JAX Bridges.
Traction part 2 - EOS Model JAX Bridges.
 
Enjoy ➥8448380779▻ Call Girls In Sector 18 Noida Escorts Delhi NCR
Enjoy ➥8448380779▻ Call Girls In Sector 18 Noida Escorts Delhi NCREnjoy ➥8448380779▻ Call Girls In Sector 18 Noida Escorts Delhi NCR
Enjoy ➥8448380779▻ Call Girls In Sector 18 Noida Escorts Delhi NCR
 
8447779800, Low rate Call girls in Tughlakabad Delhi NCR
8447779800, Low rate Call girls in Tughlakabad Delhi NCR8447779800, Low rate Call girls in Tughlakabad Delhi NCR
8447779800, Low rate Call girls in Tughlakabad Delhi NCR
 
8447779800, Low rate Call girls in Rohini Delhi NCR
8447779800, Low rate Call girls in Rohini Delhi NCR8447779800, Low rate Call girls in Rohini Delhi NCR
8447779800, Low rate Call girls in Rohini Delhi NCR
 
APRIL2024_UKRAINE_xml_0000000000000 .pdf
APRIL2024_UKRAINE_xml_0000000000000 .pdfAPRIL2024_UKRAINE_xml_0000000000000 .pdf
APRIL2024_UKRAINE_xml_0000000000000 .pdf
 
8447779800, Low rate Call girls in Kotla Mubarakpur Delhi NCR
8447779800, Low rate Call girls in Kotla Mubarakpur Delhi NCR8447779800, Low rate Call girls in Kotla Mubarakpur Delhi NCR
8447779800, Low rate Call girls in Kotla Mubarakpur Delhi NCR
 
FULL ENJOY Call girls in Paharganj Delhi | 8377087607
FULL ENJOY Call girls in Paharganj Delhi | 8377087607FULL ENJOY Call girls in Paharganj Delhi | 8377087607
FULL ENJOY Call girls in Paharganj Delhi | 8377087607
 
2024 Numerator Consumer Study of Cannabis Usage
2024 Numerator Consumer Study of Cannabis Usage2024 Numerator Consumer Study of Cannabis Usage
2024 Numerator Consumer Study of Cannabis Usage
 
Japan IT Week 2024 Brochure by 47Billion (English)
Japan IT Week 2024 Brochure by 47Billion (English)Japan IT Week 2024 Brochure by 47Billion (English)
Japan IT Week 2024 Brochure by 47Billion (English)
 
Independent Call Girls Andheri Nightlaila 9967584737
Independent Call Girls Andheri Nightlaila 9967584737Independent Call Girls Andheri Nightlaila 9967584737
Independent Call Girls Andheri Nightlaila 9967584737
 
Case study on tata clothing brand zudio in detail
Case study on tata clothing brand zudio in detailCase study on tata clothing brand zudio in detail
Case study on tata clothing brand zudio in detail
 
IoT Insurance Observatory: summary 2024
IoT Insurance Observatory:  summary 2024IoT Insurance Observatory:  summary 2024
IoT Insurance Observatory: summary 2024
 
8447779800, Low rate Call girls in New Ashok Nagar Delhi NCR
8447779800, Low rate Call girls in New Ashok Nagar Delhi NCR8447779800, Low rate Call girls in New Ashok Nagar Delhi NCR
8447779800, Low rate Call girls in New Ashok Nagar Delhi NCR
 
Investment in The Coconut Industry by Nancy Cheruiyot
Investment in The Coconut Industry by Nancy CheruiyotInvestment in The Coconut Industry by Nancy Cheruiyot
Investment in The Coconut Industry by Nancy Cheruiyot
 
Memorándum de Entendimiento (MoU) entre Codelco y SQM
Memorándum de Entendimiento (MoU) entre Codelco y SQMMemorándum de Entendimiento (MoU) entre Codelco y SQM
Memorándum de Entendimiento (MoU) entre Codelco y SQM
 
Call US-88OO1O2216 Call Girls In Mahipalpur Female Escort Service
Call US-88OO1O2216 Call Girls In Mahipalpur Female Escort ServiceCall US-88OO1O2216 Call Girls In Mahipalpur Female Escort Service
Call US-88OO1O2216 Call Girls In Mahipalpur Female Escort Service
 

Function notes 2

  • 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. …………. ………… }