       Re: New to Mathematica- How to write functions????????

• To: mathgroup at smc.vnet.net
• Subject: [mg58933] Re: New to Mathematica- How to write functions????????
• 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:=
data = Table[Random[Integer],  {100000}];

In:=
Timing[For[j = 100000; sum = 0, j >= 2,
sum += data[[j]], j--];  sum]

Out=
{1.7935680000000005*Second, 50063}

In:=
Timing[Plus @@ sum]

Out=
{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

```

• Prev by Date: Re: limit problem
• Next by Date: Re: limit problem
• Previous by thread: Re: New to Mathematica- How to write functions????????
• Next by thread: LegendreP error (bug?) in Mathematica