Functions

This is a discussion on Functions within the A Brief History of Cprogramming.com forums, part of the Community Boards category; I'm struggling to think up a good way to define a function in C++. Currently I'm thinking of defining a ...

  1. #1
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493

    Functions

    I'm struggling to think up a good way to define a function in C++. Currently I'm thinking of defining a function as a set of Expressions, and a domain for each.

    Ie: 3x for x > 2
    -x for x < 2

    Which would imply that x=2 is undefined

    Are there any holes in this argument? I want my function class to be upgradable.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Eh, I assume that the obvious answer is:
    Code:
    int MyFunc(int Arg)
    {
       if(Arg > 2)
       {
          return (3 * Arg);
       }
       else if(Arg < 2)
       {
          return (-Arg);
       }
       else
       {
          return UNDEFINED;
       }
    }
    But since you posted in GD and I guess you could've come up with that solution yourself, I guess I didn't quite get your question.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    I don't mean a C/C++ function in that sense. I mean a purely mathematical function, written as a class in C++.

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by ygfperson
    I don't mean a C/C++ function in that sense. I mean a purely mathematical function, written as a class in C++.
    Now I'm even more confused.

    Do you mean a class that can handle a general (mathematical) function for different domains, not just a predefined one (like mine above)?
    Code:
    typedef struct
    {
       int Start;
       int End;
    }DOMAIN;
    
    typedef struct
    {
       char* ExpressionInStringFormOrWhatever;
    }EXPRESSION;
    
    typedef struct _FUNCTION_NODE
    {
       _FUNCTION_NODE* NextNode;
       DOMAIN Domain;
       EXPRESSION Expression;
    }FUNCTION_NODE;
    
    class FUNCTION
    {
       LinkedList<FUNCTION_NODE> AllDomain'sExpressions;
    };
    (A general idea)
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    So, are functions basically just expressions and domains, or am I missing something?

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by ygfperson
    So, are functions basically just expressions and domains, or am I missing something?
    Currently I'm thinking of defining a function as a set of Expressions, and a domain for each
    Since you said you would define them like that I assumed that would be enough...

    And that should be enough to define a (mathematical) function. You could add if the intervals are open or closed though.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    You could do something like this:
    Code:
    double function1(double x)
    {
      return 3*x;
    }
    bool condition1(double x)
    {
      return x > 2;
    }
    
    double function2(double x)
    {
      return -x;
    }
    bool condition2(double x)
    {
      return x < 2;
    }
    
    typedef double (*SingleFunction)(double);
    typedef bool (*Condition)(double);
    
    typedef std::list< std::pair<SingleFunction , Condition> > Function;
    And then when the function is called traverse the list for a matching condition.

    It's very easy to do in Omicron:
    Code:
    function f(x) = 3x   if x > 2
    function f(x) = -x   if x < 2
    Last edited by Sang-drax; 04-18-2003 at 05:54 AM.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  8. #8
    Registered User rahaydenuk's Avatar
    Join Date
    Jul 2002
    Posts
    69
    A function (in mathematics) is a relation which uniquely associates members of one set with members of another set. More formally, a function from A to B is an object f such that every a of the set A is uniquely associated with an object f(a) of the set B. A function is therefore a many-to-one (or sometimes one-to-one) relation. The set A of values at which a function is defined is called its domain, while the set B of values that the function can produce is called its range.

    So, in order to fully define a function you need to provide a definition of the set A (the domain), along with the mapping between it and the set B (the range), which is f (the function). You don't really need to explicitly provide a definition of B (the range) as it is defined implicitly by A and f. This is what you have already come up with yourself anyway.

    So, in your class, you need some kind of expression syntax to define the domain of your function, which could be for example x < 4, or 'x of the set of Z+'. Does whatever you're programming support just real-valued functions, or does it support complex functions also? Does it support vector functions, matrix functions, tensor functions? You need to consider this before you design some sort of syntax for defining the domain of your functions and for defining the functions themselves.

    Good Luck,
    Richard Hayden. (rahaydenuk@yahoo.co.uk)
    Webmaster: http://www.dx-dev.com
    DXOS (My Operating System): http://www.dx-dev.com/dxos

    PGP: 0x779D0625

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 02:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21