# Passing parameters back into macro or function (or help improving spaghetti code)

**URL:** <https://root-forum.cern.ch/t/passing-parameters-back-into-macro-or-function-or-help-improving-spaghetti-code/36855>\
**Category:** Newbie\
**Created:** [November 16, 2019, 1:41pm UTC](https://root-forum.cern.ch/t/passing-parameters-back-into-macro-or-function-or-help-improving-spaghetti-code/36855 "2019-11-16T13:41:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![luiz\_iniciante](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/b9e5f3/32.png) [@luiz\_iniciante](https://root-forum.cern.ch/u/luiz_iniciante)\
**Post date:** [November 16, 2019, 1:41pm UTC](https://root-forum.cern.ch/t/passing-parameters-back-into-macro-or-function-or-help-improving-spaghetti-code/36855/1 "2019-11-16T13:41:47Z")

</div>

Hello Colleagues,  
One weak-old Root user here. I’ve a written macro that does a fit to a histogram of the exponential decay of the dose (or intensity) of a photon beam traversing a water target in the presence of a magnetic field. The fit at 0 teslas gives me the parameters of the exponential (offset, amplitude and decay constant). The dose at higher magnetic field strengths will be a fraction of the dose at 0 teslas and I then use my parameters of the 0 tesla fit in the fits of the remaining histograms to obtain that fraction.

I have 2 questions, the first general and the second related to the above problem:

1. The main macro is called ‘dedao.C.’ Within it there is another macro called ‘dedo.C’. I notice that in order to get ‘dedao.C’ to work, I have to first run ‘dedo.C’ so that the order of my commands at the prompt is:

Otherwise I get the error message “Unknown function: dedo.” This seems odd to me, since there is no objection to the function within my macro. Can someone kindly explain?

1. How can I improve my code? Ideally, I should be able to pass the parameters I have obtained from my 0 tesla fit into a new function or macro which I can then use to fit the remaining histograms. The only way I can think of how to do this is to write the parameters of the 0 tesla fit to a file which I can then open inside the new macro/function, which seems like a clunky work around. Is there a better way?

I have attached my workman-like code. It’s functional, so it’s really just in the interest of learning that I am reaching out for help.

Regards,  
L[test1.zip](https://root-forum.cern.ch/uploads/short-url/c6VEaKeCUSJGCB1usTA8GwPybo6.zip) (146.7 KB)

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [November 18, 2019, 8:39am UTC](https://root-forum.cern.ch/t/passing-parameters-back-into-macro-or-function-or-help-improving-spaghetti-code/36855/2 "2019-11-18T08:39:00Z")

</div>

Your macro did not compile.  
I fixed the mistakes:

[dedao.C](https://root-forum.cern.ch/uploads/short-url/jMeU3zGSWm6jhIrxDeyyHwQsG4V.C) (4.1 KB)

---

<div class="post-metadata">

**Author:** ![luiz\_iniciante](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/b9e5f3/32.png) [@luiz\_iniciante](https://root-forum.cern.ch/u/luiz_iniciante)\
**Post date:** [November 18, 2019, 9:03am UTC](https://root-forum.cern.ch/t/passing-parameters-back-into-macro-or-function-or-help-improving-spaghetti-code/36855/3 "2019-11-18T09:03:03Z")

</div>

Thanks so much, Couet. But is there any answer to my above question? Did you find that you have to run ‘dedo.C’ first? By the way, I’m using Root version 5.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [November 18, 2019, 9:41am UTC](https://root-forum.cern.ch/t/passing-parameters-back-into-macro-or-function-or-help-improving-spaghetti-code/36855/4 "2019-11-18T09:41:40Z")

</div>

Somewhere in the beginning of your “dedao” function, add:  
`if (!(gROOT->GetFunction("dedo"))) dedo();`

---

<div class="post-metadata">

**Author:** ![luiz\_iniciante](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/b9e5f3/32.png) [@luiz\_iniciante](https://root-forum.cern.ch/u/luiz_iniciante)\
**Post date:** [November 18, 2019, 9:56am UTC](https://root-forum.cern.ch/t/passing-parameters-back-into-macro-or-function-or-help-improving-spaghetti-code/36855/5 "2019-11-18T09:56:30Z")

</div>

Thanks Wile\_E\_Coyote. That’s exactly what I wanted. I take it then that if I have a function that calls another function, then I always need to add that line with the appropriate function as an argument.
