Pass by reference should be preferred in general as it is more efficient. So if this code is working now for you, it's better than old-style.
We just need to treat input and output with more care
1- use input arguments as read-only unless you are explicitly looking to do in-place computation or some other particular side-effect.
2- only pass to output local variables created inside the UDO. There should not be a reason to do any different.
So, for rule 1, avoid
opcode opvar:i:i
var += 1
...
unless you want the side effect of incrementing the input.
For rule 2, this
opcode opvar:i:i
...
xoutvar
endop
should be avoided as it may lead to confusion.
In your earlier example, the global var was also causing an issue I think. Instead, this is the correct way
opcode opvar:i:i
lvar:i = var
...
xoutlvar
endop
With a little bit of care, it should all work well.
Prof. Victor Lazzarini
Maynooth University
Ireland
Hi Victor,
I think I now understand the main difference between the two style approaches, and more generally advantages and limits of the pass by reference concept. Thank you.
The following code works well, but I'm never completely sure of the relevance and the good way in Csound...
After a quick look nothing lets consider future bugs?<CsoundSynthesizer>
<CsOptions>
-odac
</CsOptions>
<CsInstruments>sr = 44100
ksmps = 128
nchnls = 2
0dbfs = 1gisquare ftgen 2, 0, 16384, 10, 1, 0 , .33, 0, .2 , 0, .14, 0 , .11, 0, .09
opcode ctrlpfield:i:k
chnidx:S = sprintf"%\.02f\_p%d", p1, pfield
fieldvalue:i = pindexpfield
printfieldvalue
chnsetfieldvalue,chnidx
value:k init fieldvalue
value:k = chngetchnidx
putschnidx, 1
xoutvalue
endop// ctrl() arguments > 3, last argument give number of pfield
// score instructions, schedule etc. must take into account itinstr 1
k4 = ctrl4
k5 = ctrl5
endin
schedule1,0,0,0,0instr 2
asig = oscilctrl\(5, ctrl4, 2)
outsasig, asig
endin</CsInstruments>
<CsScore>
i 2.01 0 1 60 0.1
e
</CsScore>
</CsoundSynthesizer>Csound mailing list
Csound@listserv.heanet.ie
LISTSERV 16.5 - CSOUND List at LISTSERV.HEANET.IE
Send bugs reports to
Issues · csound/csound · GitHub
Discussions of bugs and features can be posted here
The information contained in this email may be confidential and privileged. It is intended only for the addressees stated above. If you are not an addressee any use, dissemination, distribution, publication or copying of the information contained in this email is strictly prohibited. If you have received this email in error, please immediately notify us by email at dataprotection@mu.ie<mailto:dataprotection@mu.ie> and delete this email from your system.
Please note that Maynooth University is subject to Freedom of Information and Data Protection laws. We may be required to disclose the content of emails under the FOI Act 2014, the Data Protection Act 2018 or GDPR.
Is don seolaí / do na seolaithe thuasluaite amháin an ríomhphost seo. D’fhéadfadh an t-eolas atá ann a bheith rúnda agus faoi phribhléid. Mura seolaí tú, tá cosc iomlán ar aon eolas atá sa ríomhphost seo a úsáid, a scaipeadh, a dháileadh, a fhoilsiú nó a chóipeáil. Má fuair tú an ríomhphost seo trí thimpiste, cuir sin in iúl dúinn láithreach trí ríomhphost a chur chuig dataprotection@mu.ie<mailto:dataprotection@mu.ie> agus scrios an ríomhphost seo ó do chóras.
Tabhair faoi deara go bhfuil Ollscoil Mhá Nuad faoi réir dhlíthe um Shaoráil Faisnéise agus um Chosaint Sonraí. D’fhéadfadh ceangal a bheith orainn ábhar ríomhphoist a nochtadh faoin Acht um Shaoráil Faisnéise 2014, faoin Acht um Chosaint Sonraí 2018 nó faoi GDPR.
Registered charity number 20037130
Csound mailing list
Csound@listserv.heanet.ie
Send bugs reports to
Issues · csound/csound · GitHub
Discussions of bugs and features can be posted here