Higher-order functions: Difference between revisions

Content added Content deleted
m (Updated description and link for Fōrmulæ solution)
No edit summary
Line 25: Line 25:
second
second
</pre>
</pre>
=={{header|6502 Assembly}}==
For the most part, it's easier to call two functions back to back. However passing functions as arguments can still be done.

Code is called using the following macro, e.g. <code>PrintOutput #$FF,foo</code>. The printing routine is left unimplemented.
<lang 6502asm>macro PrintOutput,input,addr
; input: desired function's input
; addr: function you wish to call
LDA #<\addr ;#< represents this number's low byte
STA z_L
LDA #>\addr ;#> represents this number's high byte
STA z_H
LDA \input
JSR doPrintOutput
endm</lang>

<lang 6502asm>PrintOutput:
; prints the output of the function "foo" to the screen.
; input:
; A = input for the function "foo".
; z_L = contains the low byte of the memory address of "foo"
; z_H = contains the high byte of the memory address of "foo"

pha

LDA z_L
STA smc+1 ;store in the low byte of the operand
LDA z_H
STA smc+2 ;store in the high byte of the operand

pla

smc:
JSR $1234
;uses self-modifying code to overwrite the destination with the address of the passed function.
;assuming that function ends in an RTS, execution will return to this line after the function is done.
JSR PrintAccumulator

rts</lang>






=={{header|8th}}==
=={{header|8th}}==