1 / 23

CSC 160 Computer Programming for Non-Majors Lecture #3: Calling Functions

CSC 160 Computer Programming for Non-Majors Lecture #3: Calling Functions. Prof. Adam M. Wittenstein Wittenstein@adelphi.edu http://www.adelphi.edu/~wittensa/csc160/. A preview…. Last time we evaluated functions using a program.

jimenezr
Download Presentation

CSC 160 Computer Programming for Non-Majors Lecture #3: Calling Functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSC 160Computer Programmingfor Non-MajorsLecture #3: Calling Functions Prof. Adam M. Wittenstein Wittenstein@adelphi.edu http://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 insideordinary arithmetic?

  5. What's really going on insideordinary arithmetic? • The operation (addition) is circled. • What is being operated on (the numbers) have squares around them.

  6. What's really going on insideordinary arithmetic?

  7. Ambiguity

  8. We resolve the ambiguity with PEMDAS. Ambiguity

  9. The multiplication occurs before the addition. Ambiguity

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

  11. Scheme has a simpler grammar… • All operators go before however many operands they need. • All subexpressions must have parentheses around them (including the operator). • No hidden operators; if you mean *, say it. • 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. (+ 3 4) (+ 3 (* 4 5)) (* (+ 3 4) 5) Arithmetic: Old vs. new notation • 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 • √(32 + 42) hint: 32 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.

More Related