Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Possible Duplicate:
Alternatives to procedural loops and iterating over lists in Mathematica

I am new to Mathematica and not familiar with functional programming. In particular, I have no idea on how to replace the usual for loops in C++. Here is an example of the way I am writing code now:

InitCoords[] := {
  VDiv[gap, region, initCell];
  n = 0;
  For[nz = 0, nz < initCell[3], nz++,
   For [ny = 0, ny < initCell[2], ny++,
     For[nx = 0, nx < initCell[1], nx++,
       VSet[c, nx + 0.25, ny + 0.25, nz + 0.25];
       VMul[c, c, gap];
       VSAdd[c, c, -0.5, region];
       For[j = 0, j < 4, j++, 
        Do[r[n][i] = c[i], {i, 1, NDIM}];
        If[j != 3,
         If[j != 0, r[n][1] += 0.5*gap[1];];
         If[j != 1, r[n][2] += 0.5*gap[2];];
         If[j != 2, r[n][3] += 0.5*gap[3];];
       ++ n;

I know that this is a very bad way to code in Mathematica. What are the alternatives then?

Another question. Is there is a Mathematica convention for defining a subroutine with no input? I want to partition my big program into smaller parts. Currently, I am doing it by writing a function with no input.

share|improve this question

marked as duplicate by halirutan, Jens, Simon Woods, R. M. Dec 16 '12 at 14:02

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

What do VDiv, VSet, VMul, and VSAdd do? – rcollyer Dec 16 '12 at 6:44
My own defined functions: VDiv[v1_, v2_, v3_] := { v1[1] = v2[1]/v3[1]; v1[2] = v2[2]/v3[2]; v1[3] = v2[3]/v3[3]; } Others are just similar like addition and multiplication. Now I just learn to use the list function to replace all those functions. – Sheng Dec 16 '12 at 7:14
If you give the definitions of all functions used we can try to write a simpler version for you to illustrate. – Mr.Wizard Dec 16 '12 at 8:13
VDiv[v1_, v2_, v3_] := { v1[1] = v2[1]/v3[1]; v1[2] = v2[2]/v3[2]; v1[3] = v2[3]/v3[3]; } VSet[v_, sx_, sy_, sz_] := { v[1] = sx; v[2] = sy; v[3] = sz; } VMul[v1_, v2_, v3_] := { v1[1] = v2[1]*v3[1]; v1[2] = v2[2]*v3[2]; v1[3] = v2[3]*v3[3]; } VSAdd[v1_, v2_, s3_, v3_] := { v1[1] = v2[1] + s3*v3[1]; v1[2] = v2[2] + s3*v3[2]; v1[3] = v2[3] + s3*v3[3]; } (sorry it seems that I don't know the comment formatting) – Sheng Dec 16 '12 at 9:32
Like C++, in Mathematica there is really no distinction between functions and subroutines, so using functions with not arguments to organize your program is fine. – m_goldberg Dec 16 '12 at 11:30

Of course you can make no argument functions in mathematica, for example:

f[] := Plot[x^2 - 1, {x, -2, 2}]

This function will always make the same plot, whenever


is called.

To make a real C/C++ function (in the meaning of logically encapsulated piece of code) jus use Module[] function. For example, the function that takes no arguments, makes a plot, and prints a few lines can be written as follows:

f[] := Module[
               {i}, (* variables to be treated as f[]'s local variables *)

Do[Print["Line" + Text[i]], {i, 1, 5}] (*loop for(int i=1;i<=5;i++) std::cout<<"Line "<<IntToStr(i)<<std::endl; *) Plot[x^2 - 1, {x, -2, 2}] (* make also a plot *) ] (* bracket ending the Module[] *)

Hope that helped a little :]

PS.: Don't ever give up C/C++ programming. It will become useful in future to combine faster functions with mathematica.

PS.: Also take a look at Compile[]

share|improve this answer
Thanks! Then is the Do loop more efficient than For loop? – Sheng Dec 16 '12 at 11:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.