A function is a body of code that returns a value.

The value returned may depend on arguments provided to the function.



Task

Write a definition of a function called "multiply" that takes two arguments and returns their product.

(Argument types should be chosen so as not to distract from showing how functions are created and values returned).



Related task

   Function prototype


doMultiply:
;returns HL = HL times A. No overflow protection.
push bc
push de
    rrca                     ;test if A is odd or even by dividing A by 2.
    jr c, isOdd
        ;is even
 
        ld b,a
loop_multiplyByEvenNumber:
        add hl,hl           ;double A until B runs out.
        djnz loop_multiplyByEvenNumber
pop de
pop bc
ret
 
isOdd:
    push hl
    pop de                  ;de contains original HL. We'll need it later.
    ld b,a
loop_multiplyByOddNumber:
    add hl,hl
    djnz loop_multiplyByOddNumber
    add hl,de             ;now add in original HL for the leftover add.
pop de
pop bc
ret