Quantcast
Channel: First steps - JuliaLang
Viewing all articles
Browse latest Browse all 2795

Preprocessing function arguments

$
0
0

I have a working code like the following:

function test(a,b)  
        c = a + b
        d = a - b
        e = c + 2 * d 
end

but I now want to sometimes make b a constant s.t.

const b = 1.5
function test(a)
        c = a + b
        d = a - b
        e = c + 2 * d 
end

My experience with C makes me want to do something like

#ifdef USECONST
const b = 1.5
function test(a)
#else
function test(a,b)
#endif
        c = a + b
        d = a - b
        e = c + 2 * d 
end

Is there a Julia syntax that can do this? I’m basically looking for an easy way for me to reuse the function body (c=…, d=…, e=…) while still have the option of using b as either a constant or an input parameter in different applications. Thanks!

7 posts - 4 participants

Read full topic


Viewing all articles
Browse latest Browse all 2795

Trending Articles