====== DEF ====== /* Remove the comments after creating content for each section.*/ Defines a named function, usually identified by a letter or digit, which may be arithmetic or written in Assembly according to companion keywords /* For the lines below, just create links, separated by commas, to other pages of the BASICs section, like [[:basics:basicTagName]]. "Implemented by" should mention BASICs which implement the keyword like described, in a more usual and standard way. "With variations" should point to BASICs where this keyword is used a bit differently. "Also written as" should list alternative spellings and abbreviations this keyword has. No need to refer to a BASIC, there is a section for that later. */ **Implemented by:** [[:basics:Dartmouth:dartmouth1]], [[:basics:ANSI:ansimin]], [[:basics:ANSI:ansifull]], [[:basics:hptsb:]], [[:basics:Altair:altair8K]], [[:basics:Applesoft:]], [[:basics:TI994:]], [[:basics:CBM:]], [[:basics:msatari:]], [[:basics:trs80:level3]], [[:basics:trscolor:colorext]], [[:basics:Sinclair:spectrum]], [[:basics:MSX:]], [[:basics:bwbasic:]], [[:basics:bwbasic:bw32]], [[:basics:ABasiC:]], [[:basics:VBDOS:]], [[:basics:minibasicjs:]] **With variations:** [[:basics:decbasic:]], [[:basics:Altair:altair12K]] (see FN), [[:basics:Applesoft:tasc]] (does not allow redefinition of a function), [[:basics:trs80:trsdisk]] (with [[FN]] and [[USR]]), [[:basics:BBC:]] (with FN or PROC), [[:basics:GWBASIC:]], [[:basics:msqb:QBasic]] (also with [[SEG]]) **Also written as: ** ===== Usage ===== /* For inline examples of syntax, write the keyword between '' and '' (two single quotes) */ As originally conceived, or at least as described by the 1964 manual of [[:basics:Dartmouth:dartmouth2|Dartmouth BASIC 2]], DEF allowed the programmer to create a short, mathematic function whose name //"must be three letters, the first two of which are FN"//. 20 DEF FNS(R) = SIN(R*3.1415926) 30 PRINT FNS(60) That's why most BASIC manuals list [[DEF FN]] as a keyword, even when [[FN]] will not be recognized as a keyword by itself and the created function must be called just like the pioneers did — but some BASICs allow a space after the FN particle, as you'll see at the //Variations// section below. A user-defined function may use other functions in its formula but usually it can not use itself in //recursion//: most BASICs will shout out a "stack overflow" error. So far, every documentation of BASICs consulted for this page says that DEF can not be used as an interpreter command and the DEFined functions are only valid "program-wise". ==== Variations ==== While part of the original BASIC and kept in line with its purpose, practically everything in DEF has been implemented with different allowances by every BASIC on Earth. === Function name === The [[:basics:Altair:altair8K|8kB version of Altair BASIC]] extended the naming convention to "FN" followed by any valid variable name (first a letter, then a letter or digit): a function could be named like "FNJ3", thus allowing more than 26 user-defined functions. This convention was followed by [[:basics:Applesoft:|Applesoft BASIC]] and [[:basics:CBM:|Commodore BASIC v2]]. In some BASICs, such as [[:basics:CBM:|Commodore BASIC v2]], [[:basics:GWBASIC|Microsoft GW-BASIC]], [[:basics:msx:|MSX BASIC]] and [[:basics:Sinclair:spectrum|Sinclair Spectrum]] //(to be confirmed)//, the function call allowed for a space after [[FN]], making the particle a keyword by itself. Some BASICs do //not// require the usage of the FN particle at all for the function name. This is the case of [[:basics:TI994:|TI BASIC]]. === Parameter(s) === Number, type and scope of parameters in DEF-created functions vary greatly among BASICs. * Quantity * One mandatory parameter: [[:basics:Altair:altair8K|Altair 4K/8K]], [[:basics:applesoft:|Applesoft]] * Multiple parameters allowed: [[:basics:msx:|MSX BASIC]] (up to 9), [[:basics:Altair:altair12K|Altair Extended BASIC]] * Optionally no parameter: [[:basics:applesoft:|Applesoft]], [[:basics:msx:|MSX BASIC]], [[:basics:TI994:|TI BASIC]] * Type * Numerical only: [[:basics:Dartmouth:|Dartmouth BASIC]] * Strings allowed: [[:basics:ABasiC:]], [[:basics:Altair:altair12K|Altair Extended BASIC]], [[:basics:TI994:|TI BASIC]] * Scope: * Limited to the defined function, may have the same name of a variable used elsewhere: [[:basics:TI994:|TI BASIC]], [[:basics:msx:|MSX BASIC]] * Can not have the same name of variables outside the DEFined function: [[:basics:Dartmouth:|Dartmouth BASIC]] === Function length === Default DEF functions are single-line arithmetic expressions. But //multiline// DEF blocks were supported by a number of BASICs. In these, the final delimiter of DEF block is also subject to variations. * [[:basics:msqb:|Microsoft QuickBASIC/QBasic]] has multiline DEFs finishing with ''[[END]] DEF''. === Return type === A type suffix after the function name is allowed in [[:basics:Altair:altair12K|Altair Extended BASIC]]: //"the result of the function will be forced to the function type before the value is substituted"//, says its manual. === Other purposes === Some BASICs support DEF followed by other keywords for some (very) different purposes. Such combinations are briefly described here and more detailed in specific pages: * [[DEF SEG]] allocates and prepares a memory area for binary data. * [[DEFUSR]], or ''DEF USR'', allows the creation of machine-code subroutines. ===== Comments ===== /* Origin of the keyword, opinions, history... */ ===== Related keywords ===== /* Keywords that are often or always used along this one */ * [[FN]] * [[END]] DEF ===== Similar keywords ===== /* Keywords with the same or similar functionality found in other versions of BASIC */ * [[FUNCTION]] * [[SUB]] ===== In other languages... ===== /* For parallels and comparisons with other modern-day languages */ DEF, in lower case of course, is the keyword used for functions and procedures definition in Python — possibly by influence of BASIC. ===== References ===== /* If you used the ((citation)) syntax elsewhere in the text, they will appear under this section. But you can write some recommendations of books, magazines, etc */ /* ===== TAGS ===== Please use the syntax below for adding tags to this page, separated by spaces (use quotes for tags with spaces). The most important tags to be used are, separated by groups: - Statements | Commands | SpecialVariables | Operators {{tag>tagName}} */ {{tag>Statements Functions}}