Program State and Program Execution Version 2 – Discussing Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University.

Slides:



Advertisements
Similar presentations
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs Data-flow Analysis – today’s class –Classic analyses.
Advertisements

Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Lecture 2 Introduction to C Programming
Introduction to C Programming
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Computer Science 1620 Loops.
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
Computer Science 1620 Other Data Types. Quick Review: checklist for performing user input: 1) Be sure variable is declared 2) Prompt the user for input.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Miscellaneous topicsCS-2301 B-term Miscellaneous Topics CS-2301, System Programming for Non-majors (Slides include materials from The C Programming.
Introduction to C Programming
Macro & Function. Function consumes more time When a function is called, the copy of the arguments are passed to the parameters in the function. After.
CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);
Methods (Functions) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
A First Program CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits: a significant part of.
The College of Saint Rose CIS 433 – Programming Languages David Goldschmidt, Ph.D. from Concepts of Programming Languages, 9th edition by Robert W. Sebesta,
Discussion of Assignment 9 1 CSE 2312 Computer Organization and Assembly Language Programming Vassilis Athitsos University of Texas at Arlington.
Recursion.  A recursive function contains a call to itself Example: the factorial n!=n*(n-1)! for n>1 n!=1 for n=1 int factorial (int n) { if (n == 0)
Analysis of Algorithms
Algorithms and Algorithm Analysis The “fun” stuff.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
First Programs CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
1 CS161 Introduction to Computer Science Topic #9.
CSCI 125 & 161 / ENGR 144 Lecture 8 Martin van Bommel.
Introduction to Computing Using Python Namespaces – Local and Global  The Purpose of Functions  Global versus Local Namespaces  The Program Stack 
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Introduction to Functions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
C Language 1 Program Looping. C Language2 Topics Program looping Program looping Relational operators / expressions Relational operators / expressions.
Program State and Program Execution CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Today… Modularity, or Writing Functions. Winter 2016CISC101 - Prof. McLeod1.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Functions and Libraries. The idea of a function Functions in programming A function is a block of code that has been given a name. To invoke that code.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
CS 1110 Introduction to Programming Spring 2017
Methods Chapter 6.
Presented By S.Yamuna AP/IT
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Maximization and Minimization Problems
Control Structures (Structured Programming) for controlling the procedural aspects of programming CS1110 – Kaminski.
Topics Introduction to File Input and Output
Hidden Markov Models Part 2: Algorithms
Coding Concepts (Basics)
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Control Structures (Structured Programming) for controlling the procedural aspects of programming CS1110 – Kaminski.
The structure of programming
Topics Introduction to File Input and Output
Thinking procedurally
Chapter 15 Functional Programming 6/1/2019.
Introduction to C Programming
Looping and Repetition
Presentation transcript:

Program State and Program Execution Version 2 – Discussing Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1

Program State vs. Program History The state of the program contains all the information that we need to determine what the program will do next. The state of the program is typically much more simple than the history of the program, which describes everything than the program did from the beginning till now. The computer always keeps track of program state. As a rule (with rare exceptions) the computer does NOT keep track of program history. 2

Understanding Program States Understanding any piece of code (whether it is an if statement, while loop, function call) means understanding how that piece of code changes program state. – Code that does not change the program state is useless. Conversely, if you do not understand precisely how some piece of code changes program state, you do not understand that piece of code. 3

Defining a Program State A program state consists of: – Namespaces, that associate variable names with values. Only one namespace is visible at each moment in the program execution, but multiple other namespaces may still be in memory, and can become visible later. – A calling stack, which describes what line(s) of code we are currently executing. 4

The Calling Stack The calling stack describes what line(s) of code we are executing right now. The calling stack contains a sequence of program lines: – The current line L_1 we are executing. – The line L_2 that made the last function call. – The line L_3 that made the second-to-last function call. – And so on, until some line L_N that belongs to the main code. Important: for every line in the calling stack, additional information is needed: – Exactly which function call is being evaluated (lines of code may include multiple function calls). – Which subexpressions have already been evaluated and what values they returned. 5

The Calling Stack The calling stack describes what line(s) of code we are executing right now. The calling stack contains a sequence of program lines: – The current line L_1 we are executing. – The line L_2 that made the last function call. – The line L_3 that made the second-to-last function call. – And so on, until some line L_N that belongs to the main code. Important: for every line in the calling stack, additional information is needed: – Exactly which function call is being evaluated (lines of code may include multiple function calls). – Which subexpressions have already been evaluated and what values they returned. WE WILL GET BACK TO THIS TOPIC, IT IS IMPORTANT. 6

Correspondences between Namespaces and Calling Stack At any point in the program, the number of namespaces is equal to the number of lines in the calling stack. Each line in the calling stack corresponds to a different namespace. – Why? Because each line in the calling stack corresponds to a different function call. 7

The Order of Evaluating Subexpressions For every line in the calling stack, we process it by evaluating its subexpressions, and using the resulting values. In what order do we evaluate subexpressions? – Evaluate simpler expressions before larger expressions that contain the simple ones. Does this specify a complete order? NO The order in which subexpressions are evaluated may matter (in bad code), but should never matter in good code. 8

An Example of Program Execution def cube(n): result = n*n*n return result # start of main code x = 3 result = cube(x) + cube(x+1) print("result =", result) 9

An Example – Numbering Lines 10 To make it easy to refer to lines of code, we assign numbers to each line Line 1: def cube(n): Line 2: result = n*n*n Line 3: return result # start of main code Line 4: x = 3 Line 5: result = cube(x) + cube(x+1) Line 6: print("result =", result)

Program Execution 11 Where do we start from? How do we initialize the program state? Line 1: def cube(n): Line 2: result = n*n*n Line 3: return result # start of main code Line 4: x = 3 Line 5: result = cube(x) + cube(x+1) Line 6: print("result =", result)

Program Execution 12 Main Namespace: Calling Stack: Initializing main Line 1: def cube(n): Line 2: result = n*n*n Line 3: return result # start of main code Line 4: x = 3 Line 5: result = cube(x) + cube(x+1) Line 6: print("result =", result)

Program Execution 13 Main Namespace: x = 3 Calling Stack: Line 4 Line 1: def cube(n): Line 2: result = n*n*n Line 3: return result # start of main code Line 4: x = 3 Line 5: result = cube(x) + cube(x+1) Line 6: print("result =", result)

Program Execution 14 Main Namespace: x = 3 Calling Stack: Line 5: result = cube(x) + cube(x+1) Line 1: def cube(n): Line 2: result = n*n*n Line 3: return result # start of main code Line 4: x = 3 Line 5: result = cube(x) + cube(x+1) Line 6: print("result =", result)

Program Execution Line 1: def cube(n): Line 2: result = n*n*n Line 3: return result # start of main code Line 4: x = 3 Line 5: result = cube(x) + cube(x+1) Line 6: print("result =", result) 15 Main Namespace: x = 3 Calling Stack: Line 5: result = cube(x) + cube(x+1) Note: it is not sufficient to just show Line 8 in the calling stack. We need to specify which subexpression we will work on. Which subexpression should we choose?

Program Execution Line 1: def cube(n): Line 2: result = n*n*n Line 3: return result # start of main code Line 4: x = 3 Line 5: result = cube(x) + cube(x+1) Line 6: print("result =", result) 16 Note: it is not sufficient to just show Line 8 in the calling stack. We need to specify which subexpression we will work on. Which subexpression should we choose? As long as the code follows good guidelines, the order does not matter. Main Namespace: x = 3 Calling Stack: Line 5: result = cube(x) + cube(x+1)

Program Execution Line 1: def cube(n): Line 2: result = n*n*n Line 3: return result # start of main code Line 4: x = 3 Line 5: result = cube(x) + cube(x+1) Line 6: print("result =", result) 17 Main Namespace: x = 3 Calling Stack: Line 5: result = cube(x) + cube(x+1)

Program Execution Line 1: def cube(n): Line 2: result = n*n*n Line 3: return result # start of main code Line 4: x = 3 Line 5: result = cube(x) + cube(x+1) Line 6: print("result =", result) 18 Main Namespace: x = 3 Calling Stack: Line 1: def cube(n): Line 5: result = cube(x) + cube(x+1) cube Namespace: n = 3

Program Execution Line 1: def cube(n): Line 2: result = n*n*n Line 3: return result # start of main code Line 4: x = 3 Line 5: result = cube(x) + cube(x+1) Line 6: print("result =", result) 19 Main Namespace: x = 3 Calling Stack: Line 2: result = n*n*n Line 5: result = cube(x) + cube(x+1) cube Namespace: n = 3 result = 27

Program Execution Line 1: def cube(n): Line 2: result = n*n*n Line 3: return result # start of main code Line 4: x = 3 Line 5: result = cube(x) + cube(x+1) Line 6: print("result =", result) 20 Main Namespace: x = 3 Calling Stack: Line 3: return result Line 5: result = cube(x) + cube(x+1) cube Namespace: n = 3 result = 27

Program Execution Line 1: def cube(n): Line 2: result = n*n*n Line 3: return result # start of main code Line 4: x = 3 Line 5: result = cube(x) + cube(x+1) Line 6: print("result =", result) 21 Main Namespace: x = 3 Calling Stack: Line 5: result = 27 + cube(x+1) Note: - the namespace for cube disappears. - in the calling stack, the returned value replaces the function call in the calling line.

Program Execution Line 1: def cube(n): Line 2: result = n*n*n Line 3: return result # start of main code Line 4: x = 3 Line 5: result = cube(x) + cube(x+1) Line 6: print("result =", result) 22 Main Namespace: x = 3 Calling Stack: Line 5: result = 27 + cube(x+1) Next subexpression to evaluate in current line: x+1

Program Execution Line 1: def cube(n): Line 2: result = n*n*n Line 3: return result # start of main code Line 4: x = 3 Line 5: result = cube(x) + cube(x+1) Line 6: print("result =", result) 23 Main Namespace: x = 3 Calling Stack: Line 5: result = 27 + cube(4)

Program Execution Line 1: def cube(n): Line 2: result = n*n*n Line 3: return result # start of main code Line 4: x = 3 Line 5: result = cube(x) + cube(x+1) Line 6: print("result =", result) 24 Main Namespace: x = 3 Calling Stack: Line 5: result = 27 + cube(4) Next subexpression to evaluate in current line: cube(4)

Program Execution Line 1: def cube(n): Line 2: result = n*n*n Line 3: return result # start of main code Line 4: x = 3 Line 5: result = cube(x) + cube(x+1) Line 6: print("result =", result) 25 Main Namespace: x = 3 Calling Stack: Line 1: def cube(n): Line 5: result = 27 + cube(4) cube Namespace: n = 4

Program Execution Line 1: def cube(n): Line 2: result = n*n*n Line 3: return result # start of main code Line 4: x = 3 Line 5: result = cube(x) + cube(x+1) Line 6: print("result =", result) 26 Main Namespace: x = 3 Calling Stack: Line 2: result = n*n*n Line 5: result = 27 + cube(4) cube Namespace: n = 4 result = 64

Program Execution Line 1: def cube(n): Line 2: result = n*n*n Line 3: return result # start of main code Line 4: x = 3 Line 5: result = cube(x) + cube(x+1) Line 6: print("result =", result) 27 Main Namespace: x = 3 Calling Stack: Line 3: return result Line 5: result = 27 + cube(4) cube Namespace: n = 4 result = 64

Program Execution Line 1: def cube(n): Line 2: result = n*n*n Line 3: return result # start of main code Line 4: x = 3 Line 5: result = cube(x) + cube(x+1) Line 6: print("result =", result) 28 Main Namespace: x = 3 Calling Stack: Line 5: result = Note: in the calling stack, the returned value replaces the function call in the calling line.

Program Execution Line 1: def cube(n): Line 2: result = n*n*n Line 3: return result # start of main code Line 4: x = 3 Line 5: result = cube(x) + cube(x+1) Line 6: print("result =", result) 29 Main Namespace: x = 3 Calling Stack: Line 5: result = Next subexpression to evaluate in current line:

Program Execution Line 1: def cube(n): Line 2: result = n*n*n Line 3: return result # start of main code Line 4: x = 3 Line 5: result = cube(x) + cube(x+1) Line 6: print("result =", result) 30 Main Namespace: x = 3 Calling Stack: Line 5: result = 91

Program Execution Line 1: def cube(n): Line 2: result = n*n*n Line 3: return result # start of main code Line 4: x = 3 Line 5: result = cube(x) + cube(x+1) Line 6: print("result =", result) 31 Main Namespace: x = 3 Calling Stack: Line 5: result = 91 Next to be done in Line 5: use computed value(s) as prescribed by that line (i.e., do an assignment).

Program Execution Line 1: def cube(n): Line 2: result = n*n*n Line 3: return result # start of main code Line 4: x = 3 Line 5: result = cube(x) + cube(x+1) Line 6: print("result =", result) 32 Main Namespace: x = 3 result = 91 Calling Stack: Line 5: result = 91 Next to be done in Line 5: use computed value(s) as prescribed by that line (i.e., do an assignment).

Program Execution Line 1: def cube(n): Line 2: result = n*n*n Line 3: return result # start of main code Line 4: x = 3 Line 5: result = cube(x) + cube(x+1) Line 6: print("result =", result) 33 Main Namespace: x = 3 result = 91 Calling Stack: Line 6: print("result =", result)