+ Reply to Thread
Results 1 to 6 of 6

Thread: Pointers to functions

  1. #1
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Pointers to functions

    Function pointers are one of those odd elements of C++ that don't always have an obvious application. Since I am of a math background, I'm going to look at some ways that function pointers can be useful. One of the peculiar properties of math is that, when you advance far enough, you start looking at functions as things to be worked on. Some practical examples include:

    Composition of functions: f(g(h(x)))
    Derivatives of functions
    Integrals of functions
    Arrays of functions

    In physics, there are also a variety of applications of the above, plus various other applications of principles where the exact function is not known in advance. One of the basic ideas that appears in physics is the concept of an average rate of change. The basic idea is this: average velocity is the average rate of change of position as a function of time: (pos(final)-pos(start))/(final-start). Average acceleration is the average rate of change of velocity (same formula, use vel instead of pos). Calculus is obsessed with this concept.

    In C++, we can use a pointer to a function to implement this concept with a single function. You declare a pointer to a function like this:
    Code:
    double f(double);  //function that takes a double and returns a double
    double (*fptr)(double); //pointer to a function that takes a double and returns a double
    double *g(double); //function that takes a double and returns a pointer to a double
    
    fptr = &f //valid
    g = &f //error, g is not a pointer
    fptr = &g //error g and fptr return different types
    An example of a program using this concept is avevel.cpp:
    Code:
    #include <iostream>
    #include <cmath>
    
    const double pi = 3.14159265358979323846264338327950288419716939937510;
    
    typedef double(* FCT_PNTR)(double);
    
    double avechange(FCT_PNTR f, double start,double end)
    {
      if (start == end) 
        return 0.0;
      else
        return (f(end) - f(start))/(end - start);
    }
    
    double square(double x)
    {
      return x*x;
    }
    
    double cube(double x)
    {
      return x*x*x;
    }
    
    int main()
    {
      std::cout<<"sin from 0 to pi: "<<avechange(&sin,0.0,pi)<<"\n";
      std::cout<<"cos from 0 to pi: "<<avechange(&cos,0.0,pi)<<"\n";
      std::cout<<"square from 0 to 2: "<<avechange(&square,0.0,2.0)<<"\n";
      std::cout<<"square from 2 to 0: "<<avechange(&square,2.0,0.0)<<"\n";
      std::cout<<"square from -2 to 0: "<<avechange(&square,-2.0,0.0)<<"\n";
      std::cout<<"square from 0 to -2: "<<avechange(&square,0.0,-2.0)<<"\n";
      std::cout<<"square from -2 to 2: "<<avechange(&square,-2.0,2.0)<<"\n";
      std::cout<<"square from 0 to 0: "<<avechange(&square,0.0,0.0)<<"\n";
      std::cout<<"cube from 0 to 2: "<<avechange(&cube,0.0,2.0)<<"\n";
      std::cout<<"cube from 2 to 0: "<<avechange(&cube,2.0,0.0)<<"\n";
      std::cout<<"cube from -2 to 0: "<<avechange(&cube,-2.0,0.0)<<"\n";
      std::cout<<"cube from 0 to -2: "<<avechange(&cube,0.0,-2.0)<<"\n";
      std::cout<<"cube from -2 to 2: "<<avechange(&cube,-2.0,2.0)<<"\n";
      std::cout<<"cube from 0 to 0: "<<avechange(&cube,0.0,0.0)<<"\n";
    }
    The output is:
    Code:
    sin from 0 to pi: 3.89804e-17
    cos from 0 to pi: -0.63662
    square from 0 to 2: 2
    square from 2 to 0: 2
    square from -2 to 0: -2
    square from 0 to -2: -2
    square from -2 to 2: 0
    square from 0 to 0: 0
    cube from 0 to 2: 4
    cube from 2 to 0: 4
    cube from -2 to 0: 4
    cube from 0 to -2: 4
    cube from -2 to 2: 4
    cube from 0 to 0: 0
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Sep 2008
    Location
    Australia
    Posts
    4,834
    Blog Entries
    10
    Rep Power
    51

    Re: Pointers to functions

    Wow! I am just starting to learn C++, haven't even really started the programming part of it but I know some things. Maybe this might make more sense to me when actually I understand more C++.

    The thing I don't understand is the output? How do they work out?
    jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation
    Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!

  4. #3
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Pointers to functions

    std::cout is the output stream. << sends the following object into the output stream, and the << operator can be chained. Finally, avechange has a return value that << knows how to pass to the output stream, so life is good
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  5. #4
    Join Date
    Sep 2008
    Location
    Australia
    Posts
    4,834
    Blog Entries
    10
    Rep Power
    51

    Re: Pointers to functions

    Oh, I sort of understand that part Thanks Winged, I knew that std::cout was the output system and I know << puts it to the output system
    jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation
    Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!

  6. #5
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Pointers to functions

    Looking at one line:
    Code:
    std::cout<<"sin from 0 to pi: "<<avechange(&sin,0.0,pi)<<"\n";
    std::cout<<"sin from 0 to pi: " pushes the string into the output stream and returns std::cout, this brings the output to "sin from 0 to pi: " and the code to be executed to
    Code:
    std::cout<<avechange(&sin,0.0,pi)<<"\n";
    std::cout<<avechange(&sin,0.0,pi) calls avechange which returns a value that is approximate 0, pushes that value as a string into the output stream and returns std::cout, this brings the output to "sin from 0 to pi: 3.89804e-17
    " and the code to be executed to
    Code:
    std::cout<<"\n";
    std::cout<<"\n" adds a newline to the output and returns std::cout, which brings the code to be executed to:
    Code:
    std::cout;
    This evaluates to something (nobody really cares) and terminates the statement.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  7. #6
    Join Date
    Sep 2008
    Location
    Australia
    Posts
    4,834
    Blog Entries
    10
    Rep Power
    51

    Re: Pointers to functions

    Oh that makes much more sense now Thanks Winged, I will start to read the book now. Done most of my browsing for the day.
    jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation
    Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 4
    Last Post: 02-06-2011, 01:37 PM
  2. Array pointers and functions with the same name
    By ThemePark in forum C and C++
    Replies: 7
    Last Post: 11-29-2010, 08:28 PM
  3. Using pointers to pointers to arrays as parameters
    By ThemePark in forum C and C++
    Replies: 6
    Last Post: 02-05-2010, 05:21 AM
  4. SQL Functions - SQL Encryption Functions
    By chili5 in forum Tutorials
    Replies: 8
    Last Post: 09-04-2009, 09:40 AM
  5. SQL Functions - Math Functions
    By chili5 in forum Tutorials
    Replies: 6
    Last Post: 09-02-2009, 02:11 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts