0
$\begingroup$

I am making a function that will evaluate several other functions, using the same inputs, in my program and give me the results. I am using Print to do this (see Picture.) The problem is that one of them is printing Null along with the answer. Is there a way to get this Null to not print out? Maybe another way to write this function of functions?

Poly = {w1, w2, w3, w4, d} \[Function] Module[{},

Print[FFFF[w1, w2, w3, w4, d]];
Print[FFC2[w1, w2, w3, w4, d]]];

And it prints out: Here is what it prints out

Here is the function that is giving the null...

FFC2 = {w1, w2, w3, w4, d} \[Function] 
Module[{weights, allsol, dup, sol, rsol},
weights = {w1, w2, w3, w4};
allsol = DeleteCases[DeleteDuplicates[Flatten[Table[
     If[
      l != i != j != 
        k &&
              {d/weights[[l]], 
         d/weights[[i]]} \[Element] 
        Integers &&

       Chain2[weights[[j]], weights[[k]], d] != {},

      Flatten[{d/weights[[l]], d/weights[[i]],    
        Take[Chain2[weights[[j]], weights[[k]], d], 2], 
        weights[[l]],
        weights[[i]], 
        Drop[Chain2[weights[[j]], weights[[k]], d], 2]}, 1],
                         {}] , {l, 4}, {i, 4}, {j, 4}, {k, 4}], 
    3]], {}]; 
dup = DeleteCases[
  Flatten[Table[
    If[i < j && 
      Sort[Take[Drop[allsol[[i]], 4], 2]] == 
       Sort[Take[Drop[allsol[[j]], 4], 2]], allsol[[i]], {}], {i, 
     Length[allsol]}, {j, Length[allsol]}], 1], {}];
sol = Complement[allsol, dup];
If[sol == {{}} \[Or] sol == {}, "No Possible FFC2", 
 For[k = 1, k < Length[sol] + 1, k++,

  Print[StringTemplate[
   "FFC2: w^``+x^``+y^``z+z^`` with weights: {``,``,``,``}"][
  Sequence @@ Take[sol, {k}][[1]]]]]]];

And it prints out: Here is what this prints out The reason Null is printing is because the answer that is given by the function in the second picture is a result of Print, but I don't know how to have the answer print without it.

$\endgroup$
5
  • 6
    $\begingroup$ It would be far more useful to have actual cut-and-pastable code, rather than an image. $\endgroup$ – Daniel Lichtblau Mar 11 '18 at 21:16
  • 2
    $\begingroup$ The Null is the return value of the function FFC2. It is probably the value of the expression with head For... Note that Print is not the right way to return the result of a function. $\endgroup$ – Henrik Schumacher Mar 11 '18 at 21:18
  • $\begingroup$ What is the right way to return the result of a function? $\endgroup$ – Kenneth Eaves Mar 12 '18 at 1:26
  • $\begingroup$ We'll be able to show you how if you can edit your question to include code the rest of us can copy. $\endgroup$ – J. M.'s ennui Mar 12 '18 at 1:51
  • $\begingroup$ I have updated the post $\endgroup$ – Kenneth Eaves Mar 12 '18 at 2:00
3
$\begingroup$

Here are two similar functions:

f1[x_] := Module[{t = x^2},
    Print[t]
]

f2[x_] := Module[{t = x^2},
    t
]

The first function prints something, and returns the output of a Print statement, which is Null. The second function returns t. The second function is the usual way of defining a function using Module in Mathematica. The very last statement is the return value (note: it is possible to wrap the last statement in Return, but this is discouraged because it is not always clear what data structure a Return statement is returning from). Let's compare f1 and f2:

In[36]:= f1[2] // InputForm

During evaluation of In[36]:= 4

Out[36]//InputForm=
Null

Notice how 4 is printed in a Print cell, and the output is simply Null (the InputForm is needed because in StandardForm, Null outputs are suppressed). Now, compare this with:

In[37]:= f2[2]

Out[37]= 4

The above explains what you're seeing. The return value of FFC2 is Null, as the last statement of FFC2 is a Print statement. Printing the output of FFC2 thus prints Null. It is similar to:

In[41]:= Module[{},
    Print[f1[2]]
  ] // InputForm

During evaluation of In[41]:= 4

During evaluation of In[41]:= Null

Out[41]//InputForm=
Null

I would discourage the usage of Print, unless you are using it for diagnostic purposes.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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