# How in Mathematica, would I define a function that takes arguments much like one would in an object orientated language

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)
return output
}


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

• 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. – Daniel Lichtblau Jan 27 '15 at 23:24
• okay, allow me to re-articulate, I have an issue with this, its a form of dyslexia, hold up – Jethro Devøn Jan 27 '15 at 23:25
• 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. – Daniel Lichtblau Jan 27 '15 at 23:47
• 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... – Albert Retey Jan 28 '15 at 13:19
• 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..." – Jethro Devøn Jan 31 '15 at 17:16

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
]

• Thank you for this simple and concise answer. – Jethro Devøn Jan 27 '15 at 23:24
• 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. – Mr.Wizard Jan 27 '15 at 23:32
• @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. – 2012rcampion Jan 27 '15 at 23:35
• 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". – Daniel Lichtblau Jan 27 '15 at 23:46
• @Daniel Good point! Jethro, see this for related information: (18737) – Mr.Wizard Jan 27 '15 at 23:48