Re: New to Mathematica- How to write functions????????
- To: mathgroup at smc.vnet.net
- Subject: [mg58899] Re: New to Mathematica- How to write functions????????
- From: David Bailey <dave at Remove_Thisdbailey.co.uk>
- Date: Sat, 23 Jul 2005 05:32:06 -0400 (EDT)
- References: <dboufp$sse$1@smc.vnet.net> <dbq26s$6su$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
OT 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;
>>}
>>
>>
>
>
> this function in Mathematica would be:
>
> xxx[s_Integer, y_Integer]:=Module[{Q},
> Q = s*y;
> Q
> ]
>
>
> more generally:
>
> type_t function( some_input )
> {
> int local_var_1;
> double local_var_2;
>
> local_var_1=somefunction(some_input);
> local_var_2=somefunction(some_input);
> ...
> ...
>
> return(local_var_2);
> }
>
> wolud be:
>
> function[someinput_]:=Module[{local1,local2),
> local1=somefunction[someInput];
> local2=somefunction[someInput];
> ...
> ...
>
> local2
> }
>
> bye
>
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 Bailey
http://www.dbaileyconsultancy.co.uk