The code you posted is used to test for a leap year.
The percentage sign is known as the remainder, or modulo operator and returns the remainder from the division of the second number into the first.
So in this case it checks:
- Is the variable year divisible by four (i.e. there is no remainder)
- and is the variable year divisible by one hundred
- and is the variable year NOT divisible by 400
If these three conditions are met, the function returns true, otherwise it returns false.
Here we see the so-called ternary operator
(?:) at work.
The syntax is as follows:
test ? expression1 : expression2
test is any Boolean expression
expression1 is returned if test is true
expression2 is returned if test is false
So what is happening here is that if the variable month is less than or equal to two, then a value of zero is returned.
Otherwise a second ternary operator checks if the function "leap_gregorian" when called with the parameter "year" returns true.
If so, it returns -1, if not it returns -2
This whole line of the function will just equate to a numeric value, to be added to the return value.
- month <= 2 then the value will be 0
- month > 2 and leap_gregorian(year) returns true then the value will be -1
- month > 2 and leap_gregorian(year) returns falsethen the value will be -2
I hope that all makes sense!