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

How to create a function in Julia that can accept an array or a scalar as input argument?

$
0
0

@josimar wrote:

Hi, when creating new functions in Julia, do I need to specify the input type of the arguments ? Is this required for speed or only for organization ?

For example, I would like to create a function that takes as input either an array or a scalar number. How would this be possible within the scope of Julia function definitions and code organization ?

I would prefer to specify the input type of my function for easy read of what it expect as input.

function printNumber(x::Array{Number})
    println("value = ", x)
end

inputValue = 10.0
printNumber([inputValue])  # This works

inputValue = 10
printNumber(inputValue)  ## This fails

Posts: 4

Participants: 3

Read full topic


Viewing all articles
Browse latest Browse all 2795

Trending Articles