# Stubborn Output: Returning the value from the wrong subroutine [closed]

Problem:

I wanted to program these 2 subroutines shown below and each was to return a function expression:

AnIntegrand[n0_] := Module[{n = N[n0]}, Return[6 t^2 Cos[2*n*t*Pi]];];

BnIntegrand[n0_] := Module[{n = N[n0]}, Return[6 t^2 Sin[2*n*t*Pi]];];

Before everything looked like the code above, however, I first created the first subroutine, then I copied and pasted the first subroutine below the first subroutine and changed the Cos to Sin, but I forgot to change the subroutine name from AnIntegrand to BnIntegrand. So it first looked like this:

AnIntegrand[n0_] := Module[{n = N[n0]}, Return[6 t^2 Cos[2*n*t*Pi]];];

AnIntegrand[n0_] := Module[{n = N[n0]}, Return[6 t^2 Sin[2*n*t*Pi]];];

So when I tested the output of AnIntegrand[1], it gave me the expression in sine function which defined in the second "AnIntegrand" subroutine. As I spotted my mistake, I changed the name of the second subroutine to "BnIntegrand" so that the code finally looked like the one first mentioned. Unfortunately, as I tested the output of AnIntegrand[1], it never went throught the subroutine AnIntegrand to output the cos expression, but instead it kept jumping straightly to the subroutine BnIntegrand gaving the sin expression regardless the page had been refreshed and regardless it was the AnIntegrand being called and not the BnIntegrand being called.

Next, 3 strange things also occurred as I tried to fix this annoyance. First, even with I deleted both subroutines, as I call AnIntegrand[1] again, it still gave the sine expression! I supposed since both subroutines were deleted, there should be an warning or error when I called AnIntegrand[1], but apparently it hadn't. Second, when I only deleted the BnIntegrand subroutine and called AnIntegrand[1] again, I expected to see the cos expression only, but again it gave the sin expression from the BnIntegrand! Third, the subroutine title BnIntegrand remained in blue color while the subroutine title AnIntegrand stayed normally black color and I don't know what it suggested but I believe that blue color was what caused the trouble.

Here, that's what the program can give regardless any changes I have done so far:

In[1]:=AnIntegrand[1]

Out[1]:=6 t^2 Sin[6.28319 t]

Then I opened a new notebook and copied and pasted only the AnIntegrand subroutine and called AnItegrand[1] for testing, and it gave:

6 t$10508^2 Cos[6.28319 t$10508] with $sign after the t! That's even more inexplicable, because for the AnIntegrand subroutine, I saw it did give 6 t^2 Cos[6.28319 t] nicely in the old notebook before I created the mess when doing the second subroutine. Help, please. I want: when I called AnIntegrand[1], it should give 6 t^2 Cos[6.28319 t]; when I called BnIntegrand[1], it should give 6 t^2 Sin[6.28319 t]. - I think the key is in "First, even with I deleted both subroutines" ... You have to perform a Clear[], ClearAll[] or similar to "delete" expressions in Mathematica. Start a fresh session and RTFM, Mathematica is less similar to other computer languages than it seems – belisarius Jan 29 at 7:43 This may help mathematica.stackexchange.com/a/18562/193 – belisarius Jan 29 at 7:53 @belisarius I performed Clear[] and tested AnIntegrand[1], it no longer returns the sin expression but still returned 6 t105082Cos[6.28319t10508] with$ sign after the t; Moreover, the subroutine title BnIntegrand is still flashing blue, what does it try to warn me? – Joyful Sylph Jan 29 at 7:58
You don't need to use Return in your function definitions. Mathematica will automatically return the value of last statement in a function. – image_doctor Jan 29 at 7:59
@image_doctor I believe the OP needs to go through the Help System examples (at least) before we can provide him/her with useful hints. It seems to me s/he hasn't got a grip on the basics yet – belisarius Jan 29 at 8:00

## closed as too localized by Yves Klett, Sjoerd C. de Vries, Oleksandr R., rm -rf♦Jan 30 at 18:49

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, see the FAQ.

You should perhaps skim through the New to Mathematica post linked by belisarius. The main point being that in a notebook if just write a=3, then a is not set to 3 until you run (Shift-Enter) it. When you have run it, a will be equal to 3, even if you delete the command. You can clean out old definitions using Clear or ClearAll, or by quiting the kernel using the menu (Evaluation>Quit Kernel>Local).

As for your example, you are using several unneeded constructs, You don't need Return and that is rarely needed, and the Module is not needed either:

ClearAll["Global*"]
AnIntegrand[n0_] := 6 t^2 Cos[2*n*t*Pi]
BnIntegrand[n0_] := Cos[2*n*t*Pi]

AnIntegrand[1]
(* 6 t^2 Cos[2 n \[Pi] t] *)


Since you don't pass t however the t` that is mentioned from the global scope, this may not be what you want depending on what you are doing.

-