Re: New to Mathematica- How to write functions????????
- To: mathgroup at smc.vnet.net
- Subject: [mg58933] Re: New to Mathematica- How to write functions????????
- From: Bill Rowe <readnewsciv at earthlink.net>
- Date: Sun, 24 Jul 2005 01:21:53 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
On 7/23/05 at 5:32 AM, dave at Remove_Thisdbailey.co.uk (David Bailey) wrote: >Someonekicked ha scritto: >>new to mathematica, looked at the help, but did not get the help >>i needed.. >>I want to write a function that does multiple calculations in its >>body, then it returns a value. I am talking about functions >>similar to functions that you can declare in C++. can anyone give >>me an example? >>in C++, a function would be like, (note int means integer) >>int zzz(int s, int y) { >>int Q; Q = s*y; return Q; >>} >While you are at it, look up While, Which, and For (there is even a >Goto, but I would leave that alone!). A lot of people prefer a >functional style, but if you have a C background and want to get >started, these are constructs you will recognise. Just be very >careful to get the commas and semicolons in the right place. David is correct that it is possible to write code that is quite similar to C code in Mathematica to do what you need. But, writing code in this manner entails a hefty performance penalty over the functional approach. For example, consider In[45]:= data = Table[Random[Integer], {100000}]; In[46]:= Timing[For[j = 100000; sum = 0, j >= 2, sum += data[[j]], j--]; sum] Out[46]= {1.7935680000000005*Second, 50063} In[47]:= Timing[Plus @@ sum] Out[47]= {0.00006599999999679085*Second, 50063} As you can see, both result in the identical sum but the functional approach is significantly faster. -- To reply via email subtract one hundred and four