Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 160 Computer Programming for Non-Majors Lecture #3: Calling Functions Prof. Adam M. Wittenstein

Similar presentations


Presentation on theme: "CSC 160 Computer Programming for Non-Majors Lecture #3: Calling Functions Prof. Adam M. Wittenstein"— Presentation transcript:

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

2 A preview… ● Last time we evaluated functions using a program. ● Today, and for the rest of the semester, we will call (aka evaluate) functions using DrScheme directly. ● Since we have not learned how to define our own functions yet, we will just call predefined functions today. ● However, the way you call a user-defined function is the same as the way you call predefined functions.

3 I. Basic Arithmetic: A Programming Perspective

4 What's really going on inside ordinary arithmetic?

5 The operation (addition) is circled. What is being operated on (the numbers) have squares around them.

6 What's really going on inside ordinary arithmetic?

7 Ambiguity

8 We resolve the ambiguity with PEMDAS.

9 Ambiguity The multiplication occurs before the addition.

10 Math grammar can be confusing… 1)Some operators go between two operands; others go before one operand; some require parentheses around operand, some don’t. 2)Need PEMDAS to resolve ambiguity. 3)Sometimes there's no visible operator; defaults to multiplication. 4)(3+4) means the same as 3+4, or even ((3+4)).

11 Scheme has a simpler grammar… 1)All operators go before however many operands they need. 2)All subexpressions must have parentheses around them (including the operator). 3)No hidden operators; if you mean *, say it. 4)No extra parentheses allowed; exactly one pair of parentheses per operator.

12 Syntax Rule #1: Calling a Function ● (function-name expression expression …) ● Example: (+ 1 2 3 4 5)

13 Arithmetic: Old vs. new notation ● 3 + 4 ● 3 + 4 * 5 ● (3 + 4) * 5

14 Arithmetic: Old vs. new notation ● 3 + 4 ● 3 + 4 * 5 ● (3 + 4) * 5 ● (+ 3 4) ● (+ 3 (* 4 5)) ● (* (+ 3 4) 5)

15 A Note on Numbers The online book explanation may confuse some of you. That is okay. Here is what you need to know… ● Pi (3.14…..) and Square Root of 2 (1.41….) are two examples of numbers where the digits continue without a pattern. ● Since we cannot go on writing indefinitely, we round them off to just a few decimal places, say 3.14 or 1.4. ● When Scheme has rounded a number, it puts #i before the number.

16 II. Using DrScheme

17 Using DrScheme

18 Set to “Simply Scheme” For most of the semester, we will use the Simply Scheme language level. If it is changed (e.g., you are using a public computer on campus), use these steps to restore Simply Scheme mode: ● Select Choose Language... from the Languages menu. A dialog box appears with a choice control (a.k.a. ``pop-up menu'') at the top. ● Choose the Simply Scheme language from the hierarchical choice list, then click OK.

19 Set to “Simply Scheme” ● Click the Run button. After clicking Run, the lower window indicates that the current language is Simply Scheme. ● You need to set the language level only once until you (or someone else using DrScheme on your computer) change languages. When you quit and restart DrScheme, the language setting is preserved.

20 Using the two windows ● The top area, the definitions window, is for defining new programs. We'll explain how to use this window next week. ● The bottom area, the interactions window, is for using programs once they are defined. ● Fortunately, Simply Scheme already has some programs written inside of it for us to use. Some examples are +, -, *, /, first, and sentence. ● To find 3 + 4, type it into the interactions window the Scheme way: (+ 3 4). After hitting enter, you should get 7.

21 Example 1: Translation For each mathematical expression given below, a) find its answer using paper and pen (or a calculator). b) translate it into Scheme notation. c) type into DrScheme’s interactions window and verify that the answer is what you expect. ● 3 * 5 ● 8 - 2 ● 8 - (2 * 3) ● (8 - 2) * 3 ● √(5 + 4) hint: sqrt ● √(3 2 + 4 2 ) hint: 3 2 really means 3 * 3

22 III. Preparing for Next Class

23 In summary… ● Today, we saw how to call functions involving numbers. ● However, what we learned today is not peculiar to numbers. Next time… ● We will see how Scheme steps through multi-step expressions. ● We will call functions involving other types of data. ● Please read all of Chapter 5 in Simply Scheme before next class.


Download ppt "CSC 160 Computer Programming for Non-Majors Lecture #3: Calling Functions Prof. Adam M. Wittenstein"

Similar presentations


Ads by Google