0

i have this code:

Minimum[numbers_] :=
 {
  minimum = numbers[[1]];
  For[i = 1, i <= Length[numbers], i++,
    If[minimum > numbers[[i]],
     minimum = numbers[[i]],
     minimum = minimum;
     ]
    ]
   Return[minimum];
  }

Minimum[{1, 2, 3, 4, 5, 6, 7}]

Why i always get a NULL value from this function?(Out[] = {Null})

Thank you very much.

user26963
  • 3
  • 1
  • Welcome to Mathematica.SE! I suggest that: 1) You take the introductory Tour now! 2) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! 3) As you receive help, try to give it too, by answering questions in your area of expertise. – bbgodfrey Mar 12 '15 at 18:50
  • The semicolon represents CompoundExpression and suppresses the output if it is not followed by another command, because it then assumes that the "other command" is None. – Jens Mar 12 '15 at 18:50
  • the curly brackets {} wrapping your "function" code are a basic error (unless you intend to output a one element list..). Make those () and terminate the For with a semicolon and this works (not pretty but it works.. ) – george2079 Mar 12 '15 at 19:29
  • Incidentally: Fold[If[# < #2, ##] &, {4, 8, 1, 5, 3, 9, 5}] – Mr.Wizard Mar 12 '15 at 20:57

1 Answers1

1

Beside the built-in Min function, maybe this?

 Minimum[numbers_] := Block[{minimum = numbers[[1]]},
        For[i = 1, i <= Length[numbers], i++, 
             If[minimum > numbers[[i]], minimum = numbers[[i]], 
             minimum = minimum;
               ]
          ];
        minimum]

But if you really want your function, this works fine:

  Minimum[numbers_] := {minimum = numbers[[1]];
     For[i = 1, i <= Length[numbers], i++, 
      If[minimum > numbers[[i]], minimum = numbers[[i]], 
      minimum = minimum;]]; minimum}
Mahdi
  • 1,619
  • 10
  • 23