Mod funtion?

Post Reply
DrChip
Posts: 167
Joined: Wed Oct 22, 2014 3:26 pm
My devices: iPhone 4 to 6+,iPad mini to iPad air 2

Mod funtion?

Post by DrChip »

Hi veteran smartbasic coders!

Is there a mod function or a way to add one?
a mod b
a - (b * (a / b))

Eg. 35 mod 3 = 2

Thanks

User avatar
Mr. Kibernetik
Site Admin
Posts: 4786
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPhone, iPad, MacBook
Location: Russia
Flag: Russia

Re: Mod funtion?

Post by Mr. Kibernetik »

Code: Select all

PRINT 35 % 3

DrChip
Posts: 167
Joined: Wed Oct 22, 2014 3:26 pm
My devices: iPhone 4 to 6+,iPad mini to iPad air 2

Re: Mod funtion?

Post by DrChip »

Thanks!

Henko
Posts: 814
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Re: Mod funtion?

Post by Henko »

Is there a mod function or a way to add one?
a mod b
a - (b * (a / b))

Eg. 35 mod 3 = 2
If there weren't a % operator, you would provide for your own function like:

Code: Select all

print mod(35,3)

def mod(a,b)
  return a-b*floor(a/b)
end def
And add that function to your own "utility library" wich can be included in your programs using the {} inclusion.

The huge advantage of functions over subroutines is that they use local variables, so there is no danger of unwanted modifications of varibles in the calling program. On the other hand, if you want to adress "outside" variables in a function, you can use the ".<varname>" construct.

I have begged for a function mechanism in Basic! several times when i was active there (as "Oldie"), but Misoft never even reacted.

DrChip
Posts: 167
Joined: Wed Oct 22, 2014 3:26 pm
My devices: iPhone 4 to 6+,iPad mini to iPad air 2

Re: Mod funtion?

Post by DrChip »

Thanks!! :)

Post Reply