Easy Date Converter Advanced Version
Defined Functions
 

There are more than twelve functions which are built in to the EDC programming language, as described in the Functions section. This page explains how to write and call "defined" functions, which are functions defined in terms of EDC statements.

A function definition is any set of contiguous program statements (one per line as usual, possibly with intervening blank lines) which begins with a statement of the form:

function {function name}([{arg1}[, {arg2}[, ...]]])

and ends with endfunction. Here is an example of a function which returns the maximum of two numbers:

function max(a,b)
if a > b then
    return: a
else
    return: b
endif
endfunction

Note the terminal colon in return:, which can occur anywhere in the function definition.

Properties of defined functions:

There are some restrictions on the use of defined functions:

Now follow some sample programs illustrating the use of defined functions.


Example D1

Here is an example of a defined function which calls another defined function.

Input file Result in output window
//  Example D1

x = g(1)
output: x =;
output: val(x)

function g(x)
y = h(x)
return: y
endfunction

function h(z)
z += 1
return: z
endfunction
x = 2


Example D2

This example shows that the keywords today and result can be used as function arguments.

Input file Result in output window
//  Example D2

function h(x)
x += 1
return: x
endfunction

date format = M/Y/D

output: DateVal(today,CE)
y = h(today)
output: DateVal(y,CE)
bl

today #
y = h(result)
output: DateVal(y,CE)
03/16/2007 CE
03/17/2007 CE

03/16/2007 CE, Friday
03/17/2007 CE


Example D3

This example shows that a function can be called recursively.

Input file Result in output window
//  Example D3

a = 1
do
    fa = factorial(a)
    output: val(a);;
    output: ! =;
    output: val(fa)
    if a = 12 then
        exitdo
    else
        a += 1
    endif
enddo

function factorial(y)
if y < 0 then
    return: 0
elseif y < 2 then
    return: 1
else
    x = Subtract(1,y)
    z = factorial(x)
    y = x
    x = z
    do y
        x += z
    enddo
    return: x
endif
endfunction

1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40,320
9! = 362,880
10! = 3,628,800
11! = 39,916,800
12! = 479,001,600


Example D4

Now for a somewhat more complex example, with two defined functions: NumDaysInMonthCE(m,y) returns the number of days in month m in year y in the CE Calendar, and DateOfNthSundayInMonthCE(n,m,y) returns the date of the nth Sunday in month m in year y, with n=0 meaning "last Sunday". The second function calls the first. This program uses the input: statement to obtain a year number, then prints the CE dates of the first and last Sundays in each month of that year.

Input file Result in output window
//  Example D4

//  First defined function.
function NumDaysInMonthCE(m,y)
date1 = Date(CE,y,m,1)
if m < 12 then
    m += 1
    date2 = Date(CE,y,m,1)
else
    y += 1
    date2 = Date(CE,y,1,1)
endif
return: Subtract(date1,date2)
endfunction

//  Execution begins here.

year = input: This program calculates first and last
Sundays in all months of a year.\n\nWhich year?

output: For year;
output: val(year);;
output: :
bl

m = 1
do
    date = DateOfNthSundayInMonthCE(1,m,year)
    output: dateval(date,CE);
    output: is the first;
    output: DoW(date);
    output: in;
    output: MonthName(m,CE)
    date = DateOfNthSundayInMonthCE(0,m,year)
    output: and;
    output: dateval(date,CE);
    output: is the last.
    bl

    m += 1
    if m > 12 then
        exitdo
    endif
enddo

//  Second defined function.
function DateOfNthSundayInMonthCE(n,m,y)
num_days_in_month = NumDaysInMonthCE(m,y)
if n >= 1 then
    k = 0
    date1 = Date(CE,y,m,1)
    do
        if DoW(date1) = Sunday then
            k += 1
            if k = n then
                return: date1
            endif
        endif
        if DayOf(date1,CE) = num_days_in_month then
            return: 0
            //  n too large.
        endif
        date1 += 1
    enddo
elseif n = 0 then
    //  Seeking last Sunday in the month.
    date1 = Date(CE,y,m,num_days_in_month)
    do
        if DoW(date1) = Sunday then
            return: date1
        endif
        date1 -= 1
    enddo
else
    return: 0
    //  n < 0
endif
return: date1
endfunction
For year 2007:

2007-01-07 CE is the first Sunday in January
and 2007-01-28 CE is the last.

2007-02-04 CE is the first Sunday in February
and 2007-02-25 CE is the last.

2007-03-04 CE is the first Sunday in March
and 2007-03-25 CE is the last.

2007-04-01 CE is the first Sunday in April
and 2007-04-29 CE is the last.

2007-05-06 CE is the first Sunday in May
and 2007-05-27 CE is the last.

2007-06-03 CE is the first Sunday in June
and 2007-06-24 CE is the last.

2007-07-01 CE is the first Sunday in July
and 2007-07-29 CE is the last.

2007-08-05 CE is the first Sunday in August
and 2007-08-26 CE is the last.

2007-09-02 CE is the first Sunday in September
and 2007-09-30 CE is the last.

2007-10-07 CE is the first Sunday in October
and 2007-10-28 CE is the last.

2007-11-04 CE is the first Sunday in November
and 2007-11-25 CE is the last.

2007-12-02 CE is the first Sunday in December
and 2007-12-30 CE is the last.

Easy Date Converter Advanced Version Main Page
Date/Calendar Software Hermetic Systems Home Page