C++ Functions

Function

  • Need to include function prototype before function can be used
    • void DoMagic(Magic name);
  • Passing parameters
    • C++ is pass by value
    • Use pointer to pass by pointer (dah)

      void TestPassByPointer(){
      int num = 10;
      Incr10(&num);
      return;
      }
      // Pass by a copy of pointer
      // so same memory address
      int Incr10(int* num){
      *num += 10;
      return *num;
      }
    • Pass by reference

      void TestPassByRef(){
      int num = 10;
      int& rnum = num;
      Incr_10(rnum);
      return;
      }
      // Pass by reference
      // Changes the original variable directly
      int Incr_10(int& num){
      num += 10;
      return num;
      }
    • Mandate that parameter not be changed
      • int increase10(const int& num);
    • Initialize parameters
      • int myfxn(long arg1 = 10, long arg2 = 20);
  • Return values
    • Return pointer
      • Never return the address of a local variable from a function (memory is gone!)
      • Instead, return a pointer to free store and don’t forget to delete (memory leak!)

        double* treble(double data)
        {
        // Allocate memory in the heap
        double* result = new double(0.0);
        //Check out of memory
        if (!result){
        cout << "Out of memory..." << endl;
        exit(1);
        }
        *result = 3.0*data;
        return result;
        }
        void TestReturnPointer()
        {
        double num = 10.0;
        double* ptr = 0;
        ptr = treble(num);
        cout << "three times " << num << " is " << *ptr << endl;
        delete ptr; // Don't forget to delete!
        return;
        }
    • Return reference

Pointer to function

  • Declare
    • return_type (*pointer_name) (list_of_parameter_types);
    • double (*pfun) (char*, int);
  • Example

    void TestFunctionPointer()
    {
    // Declar a function pointer
    long (*fptr)(long, long);
    // Assign fxn pointer to product
    fptr = product;
    cout << fptr(3, 5) << endl;
    // Assign fxn pointer to sum
    fptr = sum;
    cout << fptr(3, 5) << endl;
    }
    long product(long a, long b)
    {
    return a*b;
    }
    long sum(long a, long b)
    {
    return a+b;
    }
  • Function pointer can be passed as parameter
    • double sumarray(double [] array, int len, double (*pfun)(double));

Function Template

  • Example

    // Define template
    template T max(T x[], int len)
    {
    T max = x[0];
    for (int i = 0; i < len; i++)
    {
    if (max < x[i])
    max = x[i];
    }
    return max;
    }
    void TestFunctionTemplate()
    {
    int small[] = {1, 25, 35, 15};
    int lensmall = sizeof small/sizeof small[0];double medium[] = {25, 250, 2500, 5, 450};
    int lenmedium = sizeof medium/sizeof medium[0];cout << "Max of small: " << max(small, lensmall) << endl;
    cout << "Max of medium: " << max(medium, lenmedium) << endl;
    }
This entry was posted in cpp. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *


*

This site uses Akismet to reduce spam. Learn how your comment data is processed.