I'm a bit confused about, how does Return[], returns result from function. For example, take those two functions:

```
CalcLossTotal = Function[
{data, units},
Clear[i];
Return[Table[
data[[i, 1]]*units,
{i, 1, Length[data]}]
];
];
```

and

```
CalcPremiums = Function[
{data, lossTotal},
Clear[i];
Return[Table[
data[[i, 2]]*lossTotal[[i]],
{i, 1, Length[data]}]
];
];
```

wheres CalcPremiums[] depends upon CalcLossTotal[] and data which is same for both of them. Upon calculating LossTotal (e.g. result from CalcLossTotal[]), result returned from it isn't array of data, but

```
Return[{0,1000,2000,3000,4000,5000,6000,7000,8000,9000,10000}]
```

Is this the way Mathematica works, or there is something that i miss when defining/returning from functions.

Thanks in advance.

The construct you want is this:

```
CalcPremiums[data_, lossTotal_] := (
Return[Table[data[[i, 2]]*lossTotal[[i]], {i, 1, Length[data]}]];)
```

note that return is superflous if you are returning the final result so,

```
CalcPremiums[data_, lossTotal_] := (
Table[data[[i, 2]]*lossTotal[[i]], {i, 1, Length[data]}]);
```

(note no semicolon after Table[] inside the parenthesis) The parenthesis are not needed here either but I left them in assuming you really have a multi line function.

I must say I'm a bit puzzled why your construt returns "return[]". Consider this:

```
g = Function[u, If[u < 0, Return[u], 0]];
f[x_] := (
y = g[x] ;
{x, y})
```

for x<0 the effect of the pure Function (g) is to Return x from the calling function (f), not to set y=x.

```
f[-1]-> -1 , f[1] -> {1,0}
```

I see the logic but it's not obvious to me.

I'd guess the problem is an effect of Function having the attribute `HoldAll`

.

Your functions will work if rewritten like so:-

```
CalcLossTotal = Function[
{data, units},
Catch[
Clear[i];
Throw[Table[
data[[i, 1]]*units,
{i, 1, Length[data]}]
];
]];
CalcPremiums = Function[
{data, lossTotal},
Catch[
Clear[i];
Throw[Table[
data[[i, 2]]*lossTotal[[i]],
{i, 1, Length[data]}]
];
]];
```

Similar Questions

This is my code (it's an example of a larger piece of code): from scipy.optimize import curve_fit def func(x, a, b): return a + b*x xlist = [10, 30, 50, 70, 90, 110, 130, 150, 170, 190, 210, 230] ylis

How do I return the index of the maximum element in an indexed object in Mathematica? For example, I have an indexed object numbers[i] that contains an array of randomly generated numbers. number[1]=3

I'm using reactive extensions to call a async method and I want to cache the result and return this for subsequent calls to the method. How can I create an Observable instance, return it and provided

I'm new to Mathlink, and before integrating it in my code I tried to write a small REPL to get accustomed to it. The code is as follows (irrelevent parts omitted, and sorry for the horrible blend of C

Unlike most programming languages, every value in Mathematica is an expression. Applying any operation to any expressions always yields another expression. Consequently, Mathematica effectively has on

I got very strange result with texture mapping. I used 128*128 RGBA bmp image to do texture mapping of a GL_QUADS, then I got the following strange result, test with other images are OK, but for this

Question: How to do both: handle exception in outer method and return result of inner method? I have: two methods which return List: import java.util.List; import java.util.LinkedList; public class He

I have several expressions I have generated in Mathematica that I would like to export into the source code of an external C program. CForm almost does what I want except that exponentiation is expr

Given a zip file and the python ZipFile library I'm expriencing a strange output when extracting : the files which are located at the root of the archive get extracted to a sub-directory named with th

I got an array that contains the date for my currently displayed article archives and looks like this: <?php // Code: echo '<pre>'; print_r($date_result); echo '</pre>'; ?> // output

Is there anyway to have those lines automatically Black in Mathematica ?

Is there any way to return result froma PhantomJS webserver as binary? To be more specific, If I render a screenshot of a page as base64, can I then transform this base64 string into binary and return

I am trying to link Mathlink libraries in mathematica. I am using a x86_64-suse-linux OS, but when I check $SystemID in Mathematica, it tells me I am using 'Linux' instead of 'Linux-x86-64'. This is a

I randomly plotted a Sin[x] function in Mathematica 7 and this is what it shows: Note the visible defect at approximately x = -100. Here is a zoom of the defect part, clearly showing that Mathematica

if you have a select LINQ query that should only return one result, do you have to have a foreach loop to get the result? Or is there a better way?

I am using mvc 4. When I use bellow code in script new Date(2012, 12, 16, 03, 20, 00) it return as Wed Jan 16 2013 03:20:00 GMT+0530 (India Standard Time) Please help me, I am not able to understa

public int postToTwitter(String msg) { String url = null; new ImageSender().execute(); // Twitlink I have to pass in next line but it showing null twitter.updateStatus(msg+ +twitlink); return 1; }

So I'm trying to build a layer on top of the Twitter API (among others) for a project and I need to find a way to return the result of the Twitter actions to the layer of abstraction. Right now my set

While exploring a recent Linq question i noticed that the algorithm seemed rather slow. Digging deeper i noticed that it wasn't the linq code but the output of the result afterwards that took the long

I have a AMPL code and i need an web-based solver that return me results in a JSON/XML format. Can i do this with Wolfram Alpha API?

In mysql, I'm trying to create a function that will return the result of a bash command. I've created a simplified version of what I'm trying to do to make it easier to understand. I have a file calle

I am trying to build a basic search but am getting a small code issue here, i want to paginate my searched result set and this is what i have 1) an input field named input, a and within a form of po

How can I use Java within Mathematica? I have two files,GRASP.nb and GRASP.java, in the same folder (BTW: GRASP=greedy randomized adaptive search procedure). The GRASP.java file contains a method pub

This is my code CREATE OR REPLACE FUNCTION test_excep (arg INTEGER) RETURNS INTEGER AS $$ DECLARE res INTEGER; BEGIN res := 100 / arg; BEGIN EXCEPTION WHEN division_by_zero THEN RETURN 999; END; RETUR

I use this function for get result by snmp snmp2_walk(localhost, private, OID); All work fine, but result returns in this form: STRING: ... STRING: second Is there a function to convert this

I got a NSMutableArray* that looks like below and am trying to use a NSPredicate to filter it to return only those that am interested in base upon there status. After it execute the result variable is

let's say i have a simple function like that: foo -> User.findById someId, (err, user) -> return hello #{user.name} coffeescript translate it to that: foo(function() { return User.findById(so

I have to port a lot of files from Mathematica to Octave. I found a Lisp Mathematica parser from ~1991 but I am not really familiar with Lisp, so I was wondering if anyone has any experience with port

i want to translate my C++ code to wolfram, to improve my calcs. C++ code for(int i = 0; i < N - 1; ++i){ matrix[i][i] += L / 3 * uCoef - duCoef / 2 - (double)du2Coef/L; matrix[i][i+1] += L / 6 * u

I'd like to use a where clause to return some data. However I'd like to some pieces of data to be returned more than once based on the contents of a particular property. To explain further - assume da

I have really strange problem. Here is the sample code: class SomeClass(object): a = [] b = [] def __init__(self, *args, **kwargs): self.a = [(1,2), (3,4)] self.b = self.a self.a.append((5,6)) print s

I have a function similar to the following: def check return 2 == 2 || 3 != 2 || 4 != 5 end My question is, will Ruby perform all the comparisons even though the first is true, and thus the function

I'm puzzled by this behavior of mathematica. The two following expressions should return the same result: Simplify[(1 - w)^2 Sum[w^(k+kp) Sum[If[l == lp, 1, 0], {l, 0, k}, {lp, 0, kp}], {k,0, \[Infini

I'm making a phonegap app for iphone and android. my code works fine in browsers but when i tried with phonegap, the phone displays the json result and do not excute the code in the submit fonction, h

Im trying to return a json result (array); If I do it manually it works resources:[ { name: 'Resource 1', id: 1, color:'red' },{ name: 'Resource 2', id: 2 }], but im having issues rendering by passi

wolfram alpha accepts latex, but it does not export to latex, it exports plaintext,rendered images and mathematica sytax. Is there any javascript library that converts the output to latex? Mathematica

Why do I get strange results when subtracting 2.99999... from 3? scala> math.sqrt(3) res0: Double = 1.7320508075688772 scala> math.pow(res0, 2) res1: Double = 2.9999999999999996 scala> 3 - r

Can you do something like Python's yield statement in Mathematica, in order to create generators? See e.g. here for the concept. Update Here's an example of what I mean, to iterate over all Permutatio

I am new to WCF. I am trying to create List with LINQ result by using Entity Framework. I want to return JSON data. But i am not able to get it. I am getting error like Notsupportedexception was unhan

I am writing my end term report on Mathematica. Can anyone explain or point me to a website/article on Related Work to Mathematica? I am asking in this forum because i can find that information nowh

I have faced difficulties trying to return a result combined with the set of errors from function call. I have started from the following approach List<String> errors; bool result = Obj.GetResul

I'm trying to return a result set from a 11g Oracle table, after I've received the set back I need to set all the fetched rows as updated. The below script returns only one row at a time, and I can't

Heey When I'm trying to return a array I'm always getting this strange waring message but it does not interrupt my App Returning 'ABRecordRef' (aka 'const void *') from a function with result type

How do I return a result from a function? For example: Public Function test() As Integer return 1 End Function This gives a compile error. How do I make this function return an integer?

This question already has an answer here: Why negative value comes for long after multiplication? [duplicate] 1 answer I'm sure it's not strange, rather I didn't understand the docs, but why do

You can have an async API like this std::future<int> GetAsync() There are cases when for example you already have the result cached and would like to return the future initialized with the resu

I make this request: http://localhost:8983/solr/collection1/select/?indent=on&q=*:*&fl=*&facet=true&facet.field=id&rows=99 and see next result: <response> <lst name=res

I am new with Mathematica and I have one more task to figure out, but I can't find the answer. I have two lists of numbers (b,u): b = {8.734059001373602`, 8.330508824111284`, 5.620669156438947`, 1

I've got a function that returns float from JNI native to Java. This should be a straightforward call but I'm getting strange result. In my test case I'm returning value 1.61863e+010 into java. The co

I am writing query like this in zend but I am not getting return result in controller file, then what I have to do for this. $result=$db->query(insert into geofences (`name`,`description`,`polygo