Wiki

Clone wiki

Super-Mon / Short_making_functions

SHORT: MAKING FUNCTIONS

A FOUNDATIONAL TOOL IN WRITING A STRUC-
TURED PROGRAM IS BREAKING DOWN TASKS TO
"FUNCTIONS", EACH DOING A SMALLER TASK.

FUNCTIONS IN SHORT BEGIN WITH A DEFINI-
TION DECLARING HOW MANY PARAMETERS IT
TAKES AS INPUT AND WHERE TO PUT THEM.

#8000: DEF SQUARE(NUMBER)
# RETURN(NUMBER*NUMBER)

THIS FUNCTION TAKES ONE PARAMETER, WHICH
WILL BE STORED IN THE VARIABLE "NUMBER".
IT DOES ONLY ONE THING: RETURN THE
SQUARE OF THE NUMBER.

TO CALL IT:

#8010: PRINTF("ANSWER: %D", SQUARE(16))
# RTS
#8010G
ANSWER: 256

ALSO IMPORTANT IS KEEPING A FUNCTION'S
OPERATION ISOLATED FROM OTHER FUNCTIONS.

LOCAL VARIABLES FACILITATE THIS:

#8000: DEF FUNC()
# LOCAL(TMP,8)
# TMP=TMP*TMP
# RETURN(TMP)
#8020: PRDEC(FUNC())
# RTS
#8020G
64

THE "LOCAL" FUNCTION DECLARES SAVES THE
VALUE OF THE GIVEN VARIABLE AND SETS IT
TO A NEW VALUE. MOST IMPORTANT, IT
ARRANGES SO THE VARIABLE'S PREVIOUS
VALUE WILL BE RESTORED WHEN THE FUNCTION
RETURNS.

A FUNCTION IN SHORT CAN ACCEPT 0, 1, 2
OR 3 PARAMETERS. ALL PARAMETERS MUST BE
ON ZERO PAGE (I.E. ADDRESS $00..FF).

NOTE THAT LOCAL VARIABLES CAN BE ANY-
WHERE IN MEMORY, NOT JUST ZERO PAGE.

IF YOU DISASSEMBLE A SHORT FUNCTION
OUTSIDE OF SUPER-MON YOU'LL SEE IT
BEGINS SIMPLY WITH A JSR TO ONE OF THE
SHORT ENTRY POINTS. SHORT SAVES THE
ORIGINAL VALUES OF THE PARAMETER
VARIABLES, STORES THE NEW VALUES AND
RETURNS.

THUS, THE CONTENTS OF A FUNCTION CAN BE
ALL 6502 CODE, OR ALL SHORT CODE, OR
ANY MIX OF THE TWO.

This page is from Super-mon's built-in help system.

Updated