Function (C++)

From StudyWiki

Jump to: navigation, search


Contents

Overview

  • C++ provides functions as well as object orientated programming
  • entities may exist outside of any object
    • it is possible to define functions outside of any class
  • a function is called by name
    • e.g. functionx();
  • each function consists of a function prototype and function definition


Function Declaration

   <return type> <function name> ( <parameter list> );

where

  • <return type> is a data type
  • <function name> is the name of the function
  • <parameter list> is a comma separated list of parameters in the format:
    • <data type> <parameter name>

There is no function body, and the prototype ends with a semi-colon, no brace block


Default Parameters

  • default values for parameters can be specified in the function declaration
   int printChar(string pString, int position==1); 


Function Overloading

  • several functions can have the same name as long as the signature of the functions are different
signature
the function declaration without the return type


Function Definition

   <return type> <function name> ( <parameter list> ){
   
       <function body>
   }

where

  • <return type> is a data type
  • <function name> is the name of the function
  • <parameter list> is a comma separated list of parameters in the format:
    • <data type> <parameter name>
  • <function body> is the statements that perform the function and return the correct data type.


Invoking Functions in Other Files

  • this is valid so long as the declaration is visible
  • we can define functions using 2 files
    • a .hpp file
      • a header file containing the function declaration
    • a .cpp file
      • contains the function definition
  • any file can use a function by including the the header file


Parameter Passing

Call-By-Value

  • C++ uses call-by-value by default
    • argument values are copied into the parameter
      • hence the original argument is unaffected by changes
    • when objects are passed
      • a copy of the object is made, not a copy of the reference as in Java


Call-By-Reference

  • a reference variable is an alias of an entity and is declared by using &
  • mostly used with functions
    • more efficient
      • just a reference to an object is copied, the object referred to is not copied
    • allows the actual arguments to be changed



Call-By-Constant Reference

  • guarantees the argument is not changed when passed by call-by-reference
  • used when call-by-value is not suitable due to large objects
    • call-by-constant reference is more efficient
  • use both const and & modifiers


Main Function

2 formats:

no parameters

   int main () 
       { return 0; }

2 parameters

       int main (int argc, char *argv[]){
           
           cout << "Run program" << argv[0] << endl;
           return 0; 
       }
argc
the total number of arguments from the command line, including the program name
argv[]
an array of pointers to characters, which stores the arguments as strings
Personal tools