# How can I make a calculating procedure into a function

I'm sure this is a pretty common question, but I have been unable to find anything that explains what I am trying to do. Say I have some code I use quite frequently:

Clear[σDATA, ΔσDATA];
σDATA = {};
ΔσDATA = {};

Do[
PartitionedData = Partition[DATA, j];
Clear[MeanParitionedData];
MeanParitionedData = {};
Clear[MeanDiffList];
MeanDiffList = {};
Do[
AppendTo[MeanParitionedData, Mean[PartitionedData[[i]]]];,
{i, 1, Length[PartitionedData]}
];
Do[
AppendTo[
MeanDiffList, (MeanParitionedData[[k + 1]] -
MeanParitionedData[[k]])^2];,
{k, 1, Length[MeanParitionedData] - 1}
];
σ = (1/(Length[MeanParitionedData] - 1)*
Total[MeanDiffList[[1 ;;]]])^(1/2);
AppendTo[σDATA, σ];
AppendTo[ΔσDATA, σ/(2 (Length[
MeanParitionedData] - 1))^(1/2)];,
{j, 1, NumberPoints/4}
]


Where DATA is my input and σDATA,ΔσDATA are my outputs.

How can I save this routine such that I can call it with a short line like

RoutineName[DATA]


and this would output my two lists, σDATA and ΔσDATA, which I can then plot or do whatever I want with.

Note: I am aware that the routine is probably not so optimised in Mathematica but I need to keep it in this structure for a few reasons.

• How can I save this routine such that I can call it with a short line E.g. RoutineName[DATA] one way might be to make it a Module and then call it each time you need to use it. And better not use UpperCaseFirstLetter for function names, as it confuses these names with Mathematica own functions. – Nasser Dec 7 '17 at 15:00
• @Nasser Thanks for the reply. Can I store Modules in a separate notebook or file and call them, rather than having to put it in the same notebook each time. Essentially is there a user library or something? – Q.P. Dec 7 '17 at 15:15
• Check out packages. – chuy Dec 7 '17 at 15:19
• Can I store Modules in a separate notebook or file and call them Yes. That is what I do. Another option it to use packages. Here is a question on first option. code-reuse-between-files-in-the-same-directory please see if this question answers what you want. Just put all the functions (i.e. definitions) in one notebook, and evaluate that notebook. Then put the calls to these functions in a separate notebook. This keeps definitions separate from actual use. – Nasser Dec 7 '17 at 15:21
• There is one more option. You may store just your data and/or the output in a separate notebook. The data may be large. In such a case in order to make it comfortable I make a notebook with a Section and eventually a Subsection containing an input cell in which I write Data= ; and then between = and ; I copy-paste the list Data. The same I make with [Sigma]DATA and [CapitalDelta][Sigma]DATA. Then I select this cell, go to Menu/Cell/Cell Properties/Initialization Cell. Then Save the notebook. – Alexei Boulbitch Dec 7 '17 at 15:33

You might try something like this:

sigmaizer[data_] :=
Module[
{nPts, σDATA, ΔσDATA, PartitionedData, MeanParitionedData, MeanDiffList, σ},
nPts = Length[data]; (* this is a guess *)
σDATA = {};
ΔσDATA = {};
Do[
PartitionedData = Partition[DATA, j];
MeanParitionedData = {};
MeanDiffList = {};
Do[
AppendTo[MeanParitionedData, Mean[PartitionedData[[i]]]],
{i, 1, Length[PartitionedData]}];
Do[
AppendTo[
MeanDiffList,
(MeanParitionedData[[k + 1]] - MeanParitionedData[[k]])^2],
{k, 1, Length[MeanParitionedData] - 1}];
σ = (1/(Length[MeanParitionedData] - 1)*Total[MeanDiffList])^(1/2);
AppendTo[σDATA, σ];
AppendTo[ΔσDATA, σ/(2 (Length[MeanParitionedData] - 1))^(1/2)],
{j, 1, nPts/4}];
{σDATA, ΔσDATA}]


You would call it like this:

{σ, Δσ} = sigmaizer[DATA]


Notes

• I have made a guess concerning your undefined variable NumberPoints. If my guess is wrong, the code will need to be modified.
• There may be bugs — I could not test the code because you did not supply sample data.
• The code localizes all your intermediate variables so you don't have to worry about clearing values from them.
• Perfect, yes you were correct about the NumberPoints. This is exactly what I was after. – Q.P. Dec 7 '17 at 21:40