1
$\begingroup$

I would know how to write a function in object orientated language, just I don't know how to do this in mathematica, in an OOP I would define a function like this:

int rss = null;

 int Define_RSS(int a, int b, int c, int d){

  int output = (code functionality)
  rss = (some more functionality)
  return output
}

I would like to know how to do the equivalent in mathematica.

$\endgroup$
  • $\begingroup$ I voted to close. Upon reading the response by @2012rcampion I now see the question might have merit. I still don't understand the subject header though. If this all gets clarified I'd be happy to retract that vote. $\endgroup$ – Daniel Lichtblau Jan 27 '15 at 23:24
  • $\begingroup$ okay, allow me to re-articulate, I have an issue with this, its a form of dyslexia, hold up $\endgroup$ – Jethro Devøn Jan 27 '15 at 23:25
  • $\begingroup$ I notice there is a sibling question on Wolfram Community, and that one does have explanation as to what specifically was being attempted. In any case, this new subject header suffices to make clear what was wanted for the question at hand. $\endgroup$ – Daniel Lichtblau Jan 27 '15 at 23:47
  • $\begingroup$ I still find the title and text not fitting the actual content very well. I don't see anything that would qualify "object orientated" in that function definition. It would probably best fit the term "procedural" as it has a side effect which doesn't make it really fit "functional" as well. As it changes a global variable I wouldn't consider it a felicitous construct in any programming paradigm I've ever heard of... $\endgroup$ – Albert Retey Jan 28 '15 at 13:19
  • $\begingroup$ Ah well im still studying, when I have a better understanding and use of the terminology I will update the title and edit my post a touch more, I meant perhaps "how sdo I go about doing something in mathematica that in OOP language I would do like this..." $\endgroup$ – Jethro Devøn Jan 31 '15 at 17:16
4
$\begingroup$
rss = None;

DefineRSS[a_, b_, c_, d_] := Module[
        {output=(* code functionality *)},
        rss = (* some more functionality *);
        output
    ]

First we initialize the variable rss to None. Then we define (using SetDelayed) a function DefineRSS (Note we can't use _ because Mathematica will interpret that as a pattern). We use Module to define a temporary variable, set rss, and then evaluate the temporary variable.

Note that if you need more than one line to define output (e.g., you define it iteratively in multiple operations) you can use this form:

DefineRSS[a_, b_, c_, d_] := Module[{output},
        output=(* code functionality *);
        output=(* even more functionality *);
        rss = (* some more functionality *);
        output
    ]
$\endgroup$
  • $\begingroup$ Thank you for this simple and concise answer. $\endgroup$ – Jethro Devøn Jan 27 '15 at 23:24
  • 1
    $\begingroup$ Unless this is being used in a way I do not understand you should not use Block here but Module instead. See this comment for more. $\endgroup$ – Mr.Wizard Jan 27 '15 at 23:32
  • 1
    $\begingroup$ @Mr.Wizard Total coincidence, I happened to check just after you posted. I got in the habit of using Block before I learned the difference between it and Module, thanks for the correction. $\endgroup$ – 2012rcampion Jan 27 '15 at 23:35
  • 1
    $\begingroup$ One possible improvement would be to make DefineRSS HoldFirst and place symbols needing modification explicitly in a list for that first arg (or, if only one as above, just pass that alone). That way variables are properly localized, even the ones being treated as "by reference" rather than "by value". $\endgroup$ – Daniel Lichtblau Jan 27 '15 at 23:46
  • 1
    $\begingroup$ @Daniel Good point! Jethro, see this for related information: (18737) $\endgroup$ – Mr.Wizard Jan 27 '15 at 23:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.