SFTW120-LectureNotes-14

SFTW120-LectureNotes-14 - SFTW120 Programming Science...

This preview shows pages 1–4. Sign up to view the full content.

1 SFTW120 Programming Science Lecture Notes 14: Lisp: Functions © Robert P. Biuk-Aghai SFTW120 - Programming Science 2 Lecture Notes 14 Functions s The format of a function call in Lisp is: ( function-name arg1 arg2 …) s This is a prefix notation s Some functions take no arguments s Some functions take a variable number of arguments (e.g. subseq ) s Functions usually return one value, but possibly more (e.g. round )

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 SFTW120 - Programming Science 3 Lecture Notes 14 Defining Functions s Functions can be user-defined like this: (defun function-name ( arguments ) ( commands )) s function-name : name of the new function s ( arguments ) : list of zero or more arguments to the function s ( commands ) : list of zero or more commands s User-defined functions can be called just like Lisp built-in functions, e.g.: ( function-name ) SFTW120 - Programming Science 4 Lecture Notes 14 Function Definitions: square s Function for squaring a number: (defun square (x) (* x x)) s Using the new function: >(square 4) 16 >(square 0) 0 >(square (sqrt 2)) 2.0000000000000004 (rounding error!)
3 SFTW120 - Programming Science 5 Lecture Notes 14 Function Definitions: plus s Function for adding two numbers: (defun plus (x y) (+ x y)) s Using the new function: >(plus 2 3) 5 >(plus 2 -2) 0 >(plus (plus 1 2) (plus 3 4)) 10 SFTW120 - Programming Science 6 Lecture Notes 14 Returning Data from Functions s Each Lisp expression produces a return value s Each Lisp function also produces a return value b The return value of a function is the return value of the

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 10

SFTW120-LectureNotes-14 - SFTW120 Programming Science...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online