Loops and subroutines [duplicate]

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, nz++,
For [ny = 0, ny < initCell, ny++,
For[nx = 0, nx < initCell, 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] += 0.5*gap;];
If[j != 1, r[n] += 0.5*gap;];
If[j != 2, r[n] += 0.5*gap;];
];
++ 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.

marked as duplicate by halirutan♦, Jens, Simon Woods, rm -rf♦Dec 16 '12 at 14:02

• What do VDiv, VSet, VMul, and VSAdd do? – rcollyer Dec 16 '12 at 6:44
• My own defined functions: VDiv[v1_, v2_, v3_] := { v1 = v2/v3; v1 = v2/v3; v1 = v2/v3; } Others are just similar like addition and multiplication. Now I just learn to use the list function to replace all those functions. – Gosere 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 = v2/v3; v1 = v2/v3; v1 = v2/v3; } VSet[v_, sx_, sy_, sz_] := { v = sx; v = sy; v = sz; } VMul[v1_, v2_, v3_] := { v1 = v2*v3; v1 = v2*v3; v1 = v2*v3; } VSAdd[v1_, v2_, s3_, v3_] := { v1 = v2 + s3*v3; v1 = v2 + s3*v3; v1 = v2 + s3*v3; } (sorry it seems that I don't know the comment formatting) – Gosere 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

f[]

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[]

• Thanks! Then is the Do loop more efficient than For loop? – Gosere Dec 16 '12 at 11:10