Function with more parameters which are not always set when function is called.

Is something like this

function res(valPx, offset = 0) { ...}

possible in monkey c like in javascript? parameter offset is not always set in function call because I don't need it in many cases, but there are situations when I need it.

  • Unfortunately, Monkey C doesn't support neither method overloading, nor default param assignment. The best you can do here is param null checking or passing params as a dictionary

  • Javascript is loosely typed, Monkey C is based on C# which is a strict type language. 

    Strict type takes more time, but is prone to less errors then loosely typed programming languages. 

    So if you define a variable you have to pass a value or null, but you can't just not give it nothing. 

  • One other thing about Monkey C is there is a limit to the number of parameters that can be passed to a function.  If I recall, the limit is 9.  If you hit that limit you may want to pass them as an array

  • thank you all for the answers. Anyway, I really don't like monkey c or the whole garmin (connect iq) system at this moment. too many restrictions, too many simple things have to be done complicatedly. but of course from the point of view of a complete beginner with no deep experience in this system. with other platforms, I am used to much, much more freedom, especially with regard to graphics and the possibilities of handling them. but the truth is that I have never been in contact with strong object-oriented programming in my life. oh and the monkey c vscode extension... I won't even mention that.

    thank you all Slight smile

  • No no your seeing it wrong.

    the difference between javascript and any strict type language. Is that a strict type language forces you to do it the correct way, while javascript might still run even your code is not correct.

    for example in javascript I can just

    variable1=“coool”

    without declaring the variable or setting its type. Javascript will run it anyway. But this is definitely not the correct way and things like these can get you into trouble in the long run specially if you are working in bigger teams. Just because your javascript runs, doesn’t mean you wrote the code correctly. It’s a weakness of javascript that was recognized and thats why they made Typescript. Typescript is essentially just a strict type version of javascript made because specially bigger teams where just running into problems because javascript is too forgiving.

    There is nothing javascript can do that Monkey C cannot, but Monkey C forces you to do it the correct way.

    If your a beginner it can actually be very good for you to code in a strict language, because then you will learn actually how to do things correctly and you can take these practices back to your Javascript coding as well. 

    it might feel limited at first because your code will only run when you did everything right, but you will learn over time that this is actually an advantage and will help keep your code structured, optimized and high performance over time. 

    Another thing you should realize is that you are coding for a watch, with very limited amount of ram and a very limited cpu. This in itself is what is causing limitations, not monkey C. Your now dealing with embedded device programming where every byte of memory counts. That is a whole different ballgame then just creating some browser scripts and don’t even bother about performance and battery life and such. 

    Dont give up it can actually teach you a couple of things you can take back to your javascript programming to make much better performing and organized code there too Slight smile