Top Banner
CSC 160 CSC 160 Computer Programming Computer Programming for Non-Majors for Non-Majors Lecture #3: Calling Functions Lecture #3: Calling Functions Prof. Adam M. Wittenstein Prof. Adam M. Wittenstein [email protected] [email protected] http://www.adelphi.edu/~wittensa/csc160/ http://www.adelphi.edu/~wittensa/csc160/
23

CSC 160 Computer Programming for Non-Majors Lecture #3: Calling Functions Prof. Adam M. Wittenstein [email protected]/csc160

Dec 20, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CSC 160 Computer Programming for Non-Majors Lecture #3: Calling Functions Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160/

CSC 160CSC 160Computer ProgrammingComputer Programming

for Non-Majorsfor Non-Majors

Lecture #3: Calling FunctionsLecture #3: Calling Functions

Prof. Adam M. WittensteinProf. Adam M. Wittenstein

[email protected]@adelphi.edu

http://www.adelphi.edu/~wittensa/csc160/http://www.adelphi.edu/~wittensa/csc160/

Page 2: CSC 160 Computer Programming for Non-Majors Lecture #3: Calling Functions Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160/

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.

Page 3: CSC 160 Computer Programming for Non-Majors Lecture #3: Calling Functions Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160/

I. Basic Arithmetic: I. Basic Arithmetic:

A Programming PerspectiveA Programming Perspective

Page 4: CSC 160 Computer Programming for Non-Majors Lecture #3: Calling Functions Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160/

What's really going on insideordinary arithmetic?

Page 5: CSC 160 Computer Programming for Non-Majors Lecture #3: Calling Functions Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160/

What's really going on insideordinary arithmetic?

•The operation (addition) is circled.

•What is being operated on (the numbers) have squares around them.

Page 6: CSC 160 Computer Programming for Non-Majors Lecture #3: Calling Functions Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160/

What's really going on insideordinary arithmetic?

Page 7: CSC 160 Computer Programming for Non-Majors Lecture #3: Calling Functions Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160/

Ambiguity

Page 8: CSC 160 Computer Programming for Non-Majors Lecture #3: Calling Functions Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160/

Ambiguity

We resolve the ambiguity with PEMDAS.

Page 9: CSC 160 Computer Programming for Non-Majors Lecture #3: Calling Functions Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160/

Ambiguity

The multiplication occurs before the addition.

Page 10: CSC 160 Computer Programming for Non-Majors Lecture #3: Calling Functions Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160/

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)).

Page 11: CSC 160 Computer Programming for Non-Majors Lecture #3: Calling Functions Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160/

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.

Page 12: CSC 160 Computer Programming for Non-Majors Lecture #3: Calling Functions Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160/

Syntax Rule #1: Syntax Rule #1: Calling a FunctionCalling a Function

● ((function-name expression function-name expression expressionexpression …) …)

● Example: (+ 1 2 3 4 5)Example: (+ 1 2 3 4 5)

Page 13: CSC 160 Computer Programming for Non-Majors Lecture #3: Calling Functions Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160/

Arithmetic: Old vs. new notation

● 3 + 4● 3 + 4 * 5● (3 + 4) * 5

Page 14: CSC 160 Computer Programming for Non-Majors Lecture #3: Calling Functions Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160/

Arithmetic: Old vs. new notation

● 3 + 4● 3 + 4 * 5● (3 + 4) * 5

● (+ 3 4)● (+ 3 (* 4 5))● (* (+ 3 4) 5)

Page 15: CSC 160 Computer Programming for Non-Majors Lecture #3: Calling Functions Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160/

A Note on Numbers

The online book explanation may confuse some ofyou. 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.

Page 16: CSC 160 Computer Programming for Non-Majors Lecture #3: Calling Functions Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160/

II. Using DrSchemeII. Using DrScheme

Page 17: CSC 160 Computer Programming for Non-Majors Lecture #3: Calling Functions Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160/

Using DrScheme

Page 18: CSC 160 Computer Programming for Non-Majors Lecture #3: Calling Functions Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160/

Set to “Simply Scheme”

For most of the semester, we will use the SimplyScheme language level. If it is changed (e.g., you areusing a public computer on campus), use these stepsto 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.

Page 19: CSC 160 Computer Programming for Non-Majors Lecture #3: Calling Functions Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160/

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.

Page 20: CSC 160 Computer Programming for Non-Majors Lecture #3: Calling Functions Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160/

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.

Page 21: CSC 160 Computer Programming for Non-Majors Lecture #3: Calling Functions Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160/

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

Page 22: CSC 160 Computer Programming for Non-Majors Lecture #3: Calling Functions Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160/

III. Preparing for Next ClassIII. Preparing for Next Class

Page 23: CSC 160 Computer Programming for Non-Majors Lecture #3: Calling Functions Prof. Adam M. Wittenstein Wittenstein@adelphi.eduwittensa/csc160/

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.