CSC 160 Computer Programming for Non-Majors Lecture #7: Variables Revisited Prof. Adam M. Wittenstein

Slides:



Advertisements
Similar presentations
First of all – lets look at the windows you are going to use. At the top you have a toolbar, with all your various tools you can use when customising your.
Advertisements

Modifying existing content Adding/Removing content on a page using jQuery.
Sampling Distributions
CSC 160 Computer Programming for Non-Majors Lecture #9: Booleans Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Lecture #4: Defining Variables Prof. Adam M. Wittenstein
Sub Programs To Solve a Problem, First Make It Simpler.
CSC 160 Computer Programming for Non-Majors Lecture #8: Animations I Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Lecture #5: Defining Functions Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Chapter 2: Numbers, Expressions, and Simple Programs Prof. Adam M. Wittenstein
Section 2.4: Errors. Common errors ● Mismatched parentheses ● Omitting space after operator or between numbers ● Putting operator between operands.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
CSC 160 Computer Programming for Non-Majors Lecture #3: Calling Functions Prof. Adam M. Wittenstein
Section 1.1: Flying a UFO (continued). Conversion recipe 1)Expand abbreviations 2)Parenthesize all subexpressions that have an operator. Should end up.
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
CSC 160 Computer Programming for Non-Majors Lecture #10: Conditionals I Prof. Adam M. Wittenstein
CS 201 Functions Debzani Deb.
CSC 160 Computer Programming for Non-Majors Section 1.2: Drawing a UFO Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Lecture #5b: Designing Programs Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Chapter 3: Programs are Functions Plus Variable Definitions Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Lecture #2: What are Functions? Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Lecture #3a: Stepper, Words, Sentences Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Lecture #5a: Defining Functions on Words and Sentences Prof. Adam M. Wittenstein
Code Generation CS 480. Can be complex To do a good job of teaching about code generation I could easily spend ten weeks But, don’t have ten weeks, so.
Introduction Even though the syntax of Scheme is simple, it can be very difficult to determine the semantics of an expression. Hacker’s approach: Run it.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CSC 160 Computer Programming for Non-Majors Lecture #6: Function Composition Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Lecture #3b: Working with Pictures Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Lecture #5 (continued): More on Writing Functions Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Lecture #12: Syntax and Semantics Prof. Adam M. Wittenstein
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
Simplifying Rational Expressions – Part I
Positional Number Systems
Formal Models of Computation Part II The Logic Model
Chapter P Prerequisites: Fundamental Concepts of Algebra
Two Way ANOVA ©2005 Dr. B. C. Paul. ANOVA Application ANOVA allows us to review data and determine whether a particular effect is changing our results.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Python Programming Chapter 6: Iteration Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
SOLIDWORKS: Lesson II – Revolutions, Fillets, & Chamfers UCF Engineering.
CSC 160 Computer Programming for Non-Majors Chapter 8: Scheme Language Review Prof. Adam M. Wittenstein
Formal Semantics Chapter Twenty-ThreeModern Programming Languages, 2nd ed.1.
CPS120: Introduction to Computer Science Decision Making in Programs.
CPS120: Introduction to Computer Science Functions.
MA 1128: Lecture 17 – 6/17/15 Adding Radicals Radical Equations.
David Stotts Computer Science Department UNC Chapel Hill.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Computer Engineering Rabie A. Ramadan Lecture 5.
Albert Gatt LIN3021 Formal Semantics Lecture 4. In this lecture Compositionality in Natural Langauge revisited: The role of types The typed lambda calculus.
BMTRY 789 Lecture 11: Debugging Readings – Chapter 10 (3 rd Ed) from “The Little SAS Book” Lab Problems – None Homework Due – None Final Project Presentations.
1 CS161 Introduction to Computer Science Topic #9.
Sahar Mosleh California State University San MarcosPage 1 The Class String There is no primitive type for strings in Java The class String is a predefined.
CSD 340 (Blum)1 Starting JavaScript Homage to the Homage to the Square.
Sight Words.
Lecture on Set! And Local CS 2135 Copyright Kathi Fisler, 2002 This material requires Advanced Language Level.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
CMSC 330: Organization of Programming Languages Operational Semantics.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Classic Connections: Innovative Methods for Making Education Work.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
Section 10.3 The Elimination Method Section 10.4 Systems of Linear Equations in 3 Variables.
User-Written Functions
User-Defined Functions
CISC101 Reminders Assn 3 due tomorrow, 7pm.
6.001 SICP Further Variations on a Scheme
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 2/23/2019.
Classes, Objects and Methods
CISC101 Reminders Assignment 3 due today.
Presentation transcript:

CSC 160 Computer Programming for Non-Majors Lecture #7: Variables Revisited Prof. Adam M. Wittenstein

REVIEW: What are variables? A variable is a connection between a name and a value. For example, in (define PI 3.14), we say PI is a name and 3.14 is a value. In this course, we will use variables in only one way, as a named constant. This means that the name X will always correspond to one specific value within a single Scheme file. Often, we even state this, at the top of the Definitions Window, as in (define X 7).

I. What’s going on when you define variables?

Order of Variable Definitions When you are defining only functions, the order of definitions does not matter. However, the order for variable definitions is significant. This is because DrScheme evaluates the right-hand side immediately, without looking at the remaining definitions.

This is allowed! (define RADIUS 5) (define DIAMETER (* 2 RADIUS)) This is perfectly legal. RADIUS is set equal to the number 5. Then DIAMETER is equal to 2 times a known number, RADIUS.

This is not allowed! (define DIAMETER (* 2 RADIUS)) (define RADIUS 5) This produces the error “reference to undefined identifier: RADIUS”. DrScheme does not yet know the definition of RADIUS when it tries to evaluate (* 2 RADIUS).

What happens with variables in variable definitions? The fact that X = 7, as in (define X 7), is meaningless until we call upon that name, as in (+ X 2). Then, the X is assigned its value (7), and added to 2, for a total of 9.

What happens with parameters in function definitions? Suppose, you define the function: (define (square num) (* num num)) When we invoke the square function, with a particular argument like 6, as in: (square 6) the parameter num is replaced by the number 6, and it has that value until the function is done working, and produces the answer of 36.

What happens with parameters in function definitions? If after getting the answer, you invoke the square function again as in: (square 2) then now you are assigning a new value to num, that is num = 2. However, from the time you type an example into the Interactions Window, until you get its answer, the parameter (in this case num) has the same value.

A Word About Other Programming Languages… In some other programming languages like Java or BASIC, you usually change the value of the same variable frequently, even for fairly small programs. Changing the value of a variable can be done in Scheme too, but we can write many varied functions in Scheme without doing this.

So what is functional programming? During the first week of class, the readings mentioned “functional programming”. We have now seen that writing programs is just writing functions. Short programs, like most of the ones we have done so far, consist of one function. Normally, programs consist of many functions.

So what is functional programming? In high school, many of you evaluating numeric functions, as in f(x) = x + 3. To find f(5), you replaced x with 5 and added 3 to get 8. The value of x does not change. It is still 5. The number 8 is given a different name, like f(x) of y.

II. How Little People Do Variables

Parameters and Arguments Here is the square function with a test case, (define (square x) (* x x)) (square 3) Here x is a parameter. And in the test case, x is replaced by the argument 3. A variable is the association in the little person’s mind between a parameter (like x) and an actual argument (like 3).

Variables vs. Parameters We started today by saying a variable is a name associated with a value. In the case of a function definition, the “name” is the parameter. The value is what we associate with it. So one variable is the parameter r set equal to the number 5. And another variable is the parameter r set equal to the number 2. (This second parameter can just as easily be called num, or anything else.)

A Word about Slang We now know that a variable is technically a name (sometimes a parameter) associated with a value. Often people get lazy and call the parameter by itself a variable, which is technically wrong. (I have even slipped a couple of times this semester. Try and catch me if I do it again!)

Little people are not mindreaders! Here are two Scheme functions: (define (f x) (g 6)) (define (g y) (+ x y)) Suppose you clicked Run and then type (f 4) into the Interactions Window. What will happen?

Little people are not mindreaders! In Simply Scheme mode: The function f indicates that (f x) = (g 6). The Stepper will replace the x with the 4, so it becomes (f 4) = (g 6). The function g indicates that (g y) = (+ x y). The Stepper will replace the y’s with 6’s, so it becomes: (g 6) = (+ x 6). You will then get an error message like: ERROR – VARIABLE X IS UNBOUND. Why are you getting this error message?

Little people are not mindreaders! In Beginning Student mode: The function g will return a Syntax Error because there is a parameter, x, used in the function body that is not named in the function header.

Little people are not mindreaders! A parameter, like x, is what we call a local variable. It only has meaning within the function whose header it is part of. Once we leave the f function, and go to work on the g function, x is no longer a paramter. So the g function has no idea what x is. How can we adjust the two-function program consisting of f and g to make it work?

Spelling it out for the little people! We hire two little people Franz and Gloria. We saw that Gloria ran into the problem of not knowing what x is. To fix this, x needs to be one of the parameters in Gloria’s function, g. So we adjust g to become: (define (g x y) (+ x y))

It is very important to remember that to Scheme, Gloria’s parameter x and Franz’s parameter x are often two different things. Franz’s x can be 3 and Gloria’s x can be 5. They may both be 3 in a particular example, but that is nothing more than coincidence. Spelling it out for the little people!

Franz and Gloria Working Together (define (f x) (g x 6)) (define (g x y) (+ x y)) ; 1) Notice the body of Franz’s function changed so ; that g has two arguments. ; 2) Since Franz uses the g function in his function ; body, he must adhere to the g function’s header. ; 3) Otherwise, the g function would be confused and ; return an error message.

(define (f x) (g x 6)) (define (g x y) (+ x y)) (f 4) The function f indicates that (f x) = (g x 6). The Stepper will replace the x with the 4, so it becomes (f 4) = (g 4 6). The function g indicates that (g x y) = (+ x y). The Stepper will replace the x’s with 4’s and the y’s with 6’s, so it becomes: (g 4 6) = (+ 4 6). This gives the answer of 10. Franz and Gloria Working Together

III. Global and Local Variables

Two types of naming When defining functions, we have: –a function name global variable (permanent) used throughout the Scheme file –one or more parameter names local variable (temporary) applies only to the specific function it is in

Local Variables We have already seen this. In the square function, (define (square x) (* x x)) x is a local variable (a.k.a. parameter). When we test the square function, we type something like (square 3). In this test of this function only, the x is replaced by a 3.

Local Variables A local variable is: the association of a formal parameter (name) with an actual argument (value). Technically, we “associate the value 3 with the name x”. It is easier to just say “x has the value 3”, but make sure you are aware that this only applies within one test of one function, and not throughout the program or Scheme file.

Global Variables Apply throughout the Scheme file anywhere below their definition. In (define NUM 7), NUM will permanently represent the number 7. In (define (square x) (* x x)), the function name square will permanently refer to the function body (* x x).

Global Variables If we have (define X 7), then every X in the entire Scheme file is replaced with 7. If we have (define (square x) (* x x)), then every (square x) in the Scheme file is replaced with (* x x).

A warning… Technically, if a global variable and local variable have the same (exact) name, the name will apply to the local variable within its specific function, and the name will apply to the global variable everyone else. It is very confusing to someone using your program to have different things named the same, so don’t do it!

IV. Preparing for Next Class

In summary… Variables are the association of a name and a value. The order of variable definitions is very important. There are two types of variables – local and global. When writing functions with more than one program, be aware that global variable names apply everywhere, but local variable names only apply within a specific function.

Next time… Animations (which are multi-function programs)