Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 160 Computer Programming for Non-Majors Lecture #2: What are Functions? Prof. Adam M. Wittenstein

Similar presentations


Presentation on theme: "CSC 160 Computer Programming for Non-Majors Lecture #2: What are Functions? Prof. Adam M. Wittenstein"— Presentation transcript:

1 CSC 160 Computer Programming for Non-Majors Lecture #2: What are Functions? Prof. Adam M. Wittenstein Wittenstein@adelphi.eduhttp://www.adelphi.edu/~wittensa/csc160/

2 Programs Are Functions Any Scheme program is made up of one or more functions. There are two types of functions: user defined and pre-defined. Certain “pre-defined” functions, like +, and, string-append, image-width, and so on are already written into DrScheme. We look at some of these today. If you wish to write a program that requires a function that is not pre-defined, then you need to write it yourself. (We will see how to do this in Chapter 4 of Simply Scheme.)

3 Exploring Functions Before we learn how to write functions, we need to understand what they are. In high school, your math teacher wrote f(x) = x + 3 on the board. But what does this mean? And what about functions that involve no numbers or math at all? We will answer these questions today!

4 Downloads for the semester Download DrScheme Version 301: http://download.plt-scheme.org/drscheme/v301.html Download and install the additional file with some predefined functions to practice with: http://hkn.eecs.berkeley.edu/~dyoo/cs3/simply-scheme/ Close and reopen DrScheme. Go to Language>Choose Language>Simply Scheme. Then click Run.

5 Using Someone’s Program Today, we will actually use a computer program that someone else wrote to learn what functions are. (We could just as easily learn topics not even remotely related to Computers with a computer program.) This program is called “functions.scm”. It was written by the author of our textbook for our use. Notice that we can use programs without knowing how they were written.

6 Downloading “functions.scm” Only repeat when changing computers Go to the links page on the class website. Download “functions.scm” and save it to the folder “C:\ProgramFiles\PLT”. Repeat every time you use a computer In DrScheme’s Interactions Window, type: --(load “C:\\Program Files\\PLT\\functions.scm”) & Enter. Be careful of spaces and capital letters! --(functions) & Enter twice.

7 Example 1: The + function Type in the function name, +. Then Enter. Type in the first argument, 3. Then Enter. Type in the second argument, 5. Then Enter. You should get an answer of 8. Try your own example with multiplication, use the * symbol.

8 Example 2: first Type in the function name, first. Enter. Type in the argument, hello. Enter. You should get an answer of h (the first letter). Try your own example with last. Try your own example with butfirst.

9 Data Types We have seen two types of data: numbers and words. There are many other types of data that Scheme officially recognizes. Functions can take arguments of any recognized data type. Try the + function again, with arguments 6 and seven. What happens?

10 Domain and Range The domain is all acceptable inputs (also known as arguments). The range is all acceptable outputs (also known as answers). The domain of + is “all pairs of numbers”. What is the range of +? What is the range of first?

11 Example 3: sentence Type in the function name, sentence. Enter. Type in the first argument, (when I get). Enter. Type in the second argument, home. Enter. You should get the answer: (when I get home). Try the same example, but put parentheses around home. Try butfirst again but use the arguments: (when I get) and (home).

12 Example 4: The < function Type in the function name, <. Enter. Type in the argument, 3. Enter. Type in the argument, 5. Enter. You should get an answer of #t (true). Try your own examples with =, >, >=, and <=.

13 Example 5: number-of-arguments Function name: number-of-arguments. Argument: -. You should get an answer of 2, since - takes in 2 arguments. Try the other functions you have seen today: first, last, sentence, butfirst, and >.

14 So what are functions? The idea of function is at the heart of Computer Science (just as with Math). However, in Computer Science, functions work on all types of data, not just numbers. Functions give the Programmer a way to think about process. That is, a function takes something and does something to it. For example, + takes something (the numbers 3 and 5) and does something to it (adds them).

15 More on Data Types Now we have seen 5 data types: --numbers--booleans --words--functions --sentences While functions are themselves a data type (“function as object”), we will rarely use them as a data type this semester. The first goal of a programmer is to understand “function as process”. That means, takes something and does something to it.

16 Preparing for Next Class

17 In summary… Today, we learned what a function is. Next time… We will work with functions directly in DrScheme. We will see what happens when a function has more than one operation.

18 Reminders… Homework #1 is due SUNDAY at 11:59pm. Send by e-mail to: Wittenstein@adelphi.edu Wittenstein@adelphi.edu Before next class, please read: --Simply Scheme, Chapter 3 (all) --How to Design Programs, Chapter 2 (introduction and Section 1 only) Have a happy Labor Day!


Download ppt "CSC 160 Computer Programming for Non-Majors Lecture #2: What are Functions? Prof. Adam M. Wittenstein"

Similar presentations


Ads by Google