a)
function days = daysInMonth(monthNumber) switch ( monthNumber ) case {1, 3, 5, 7, 8, 10, 12} days = 31; case {4, 6, 9, 11} days = 30; case 2 days = 28; otherwise days = 0; end end
b)
function bool = isLeapYear(year) bool = (mod(year, 4) == 0 & (~mod(year, 100) == 0 || mod(year, 400) == 0)); end
Da blir den oppdaterte daysInMonth-funksjonen slik:
function days = daysInMonth(monthNumber, year) switch (monthNumber) case {1, 3, 5, 7, 8, 10, 12} days = 31; case {4, 6, 9, 11} days = 30; case 2 days = 28 + isLeapYear(year); otherwise days = 0; end end