How to pass parameter list to a function in Julia

后端 未结 5 701
名媛妹妹
名媛妹妹 2021-01-25 07:27

I am newbie in Julia language, and the tutorial is not very deep yet and I didn\'t understand what is the best way to pass a parameter list of a function. My function looks like

5条回答
  •  隐瞒了意图╮
    2021-01-25 07:40

    What you want to do really is passing an instance of a data structure (composite data type) to your function. to do this, first design your data type:

    type MyType
      x::Vector
      a
      b
      c
    end
    

    and implement dxtd function:

    function dxdt(val::MyType)
      return val.a*val.x^2 + val.b*val.x + val.c
    end
    

    then some where in your code you make an instance of MyType like this:

    myinstance = MyType([],0.0,0.0,0.0)
    

    you can update myinstance

    myinstance.x = [1.0,2.8,9.0]
    myinstance.a = 5
    

    and at the end when myinstance get ready for dxxt

    dxdt(myinstance)
    

提交回复
热议问题