# How to turn an algorithm into a function

I have written a script to help me find the several local minima of a dataset. It takes two arguments: the dataset list and an x-coordinate value that tells the script from where to start looking for the minima. I would like, however, to make this into a function so I can call it several times in the same notebook without having to write it again and again. Is it possible to do that? I tried to define it as I would a regular function

f[x_,y_]:= script...


but it didn't work. Also... as a bonus... is it possible to store this function in a personal directory from where I can call it in several notebooks, without having to write it in each one of them?

Here is my script, if it is of any help.

Thank you very much for your help.

MinCoordinates0 = {{0, 0}};
SweepStartingPoint = 1;
MinPointsCounter = 0;

i = Position[list,Nearest[list[[All, 1]],SweepStartingPoint][]][][];

While[i < Length[list] - 1,
If[list[[i + 1, 2]] <= list[[i + 2, 2]] &&
list[[i + 1, 2]] <= list[[i, 2]], {MinPointsCounter=MinPointsCounter + 1,
MinCoordinates0 =
Join[MinCoordinates0, {{list[[i + 1, 1]], list[[i + 1, 2]]}}]}];
i++]

MinCoordinates =
Table[{MinCoordinates0[[k, 1]], MinCoordinates0[[k, 2]]}, {k, 2,
Length[MinCoordinates0]}];

Print[MinCoordinates // MatrixForm]
MinPoints = Range[MinPointsCounter];

j = 1;

While[j <= Length[MinPoints],
MinPoints[[j]] =
Graphics[{Red,
Disk[{MinCoordinates[[j, 1]], MinCoordinates[[j, 2]]}, 0.04]}]; j++]

Show[ListLinePlot[list], MinPoints]


The two arguments of my function-to-be in this script are "SweepStartingPoint" and "list", where the first is the point where the search starts and list is the nx2 data set, where n is the number of rows.

UPDATE:

I tried to define it as a function as

f[SweepStartingPoint_, list_]:=   MinCoordinates0 = {{0, 0}};
SweepStartingPoint = 1;
MinPointsCounter = 0;

i = Position[list,Nearest[list[[All, 1]],SweepStartingPoint][]][][];

While[i < Length[list] - 1,
If[list[[i + 1, 2]] <= list[[i + 2, 2]] &&
list[[i + 1, 2]] <= list[[i, 2]], {MinPointsCounter=MinPointsCounter + 1,
MinCoordinates0 =
Join[MinCoordinates0, {{list[[i + 1, 1]], list[[i + 1, 2]]}}]}];
i++];

MinCoordinates =
Table[{MinCoordinates0[[k, 1]], MinCoordinates0[[k, 2]]}, {k, 2,
Length[MinCoordinates0]}];

Print[MinCoordinates // MatrixForm];
MinPoints = Range[MinPointsCounter];

j = 1;

While[j <= Length[MinPoints],
MinPoints[[j]] =
Graphics[{Red,
Disk[{MinCoordinates[[j, 1]], MinCoordinates[[j, 2]]}, 0.04]}]; j++];

Show[ListLinePlot[list], MinPoints];


but the output of this is just {{0,0}}

• "I tried to define it as I would a regular function but it didn't work." How did you try? Notice you can not start a new line inside a function e.g. Print[MinCoordinates // MatrixForm] should be Print[MinCoordinates // MatrixForm]; – xzczd Aug 9 '19 at 5:51
• I made this correction and I am not getting any error, but the output is just {{0,0}}. I defined f[SweepStartingPoint_, list_] := script, with ";" finishing every line of command. But in the definition of the function, when I press Enter, it outputs the minima coordinates list and the graph. I need Print[ZeroCoordinates // MatrixForm] and Show[ListLinePlot[list], ZeroPoints] to be outputs. – NeonGabu Aug 9 '19 at 6:01
• Wrap what you want to include in the function in parentheses? – lirtosiast Aug 9 '19 at 6:15
• As mentioned by @lirtosiast , you need parentheses e.g. example[x_]:=(y=x+1;y+2). Define function in a good way is more than this, but parentheses should be enough for you at the moment. "is it possible to store this function in a personal directory from where I can call it in several notebooks, without having to write it in each one of them?" You don't need to write it in each one of them, even if you don't store this function in a special directory. Every notebook is linking to the same kernel by default. Just execute e.g. x=1 and open a new notebook and check x. – xzczd Aug 9 '19 at 6:30
• You might want to consider wrapping everything in a Module[] (see docs). That will make it a bit cleaner. – Julius Aug 9 '19 at 13:48