Call Stacks, Arguments and Returns

Slides:



Advertisements
Similar presentations
Spring Semester 2013 Lecture 5
Advertisements

The LC-3 – Chapter 6 COMP 2620 Dr. James Money COMP
Recursion in Python. Recursion Problems in every area of life can be defined recursively, that is, they can be described in terms of themselves. An English.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Recursion.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
Recursion. Binary search example postponed to end of lecture.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
1 Executing Method Calls This lecture tells you precisely how method calls are executed (a few details will have to wait until we get to classes and objects).
Recursion.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
20-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Functions Part I (Syntax). What is a function? A function is a set of statements which is split off into a separate entity that can be used like a “new.
Comp 249 Programming Methodology Chapter 10 – Recursion Prof. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
CS0004: Introduction to Programming Subprocedures and Modular Design.
Fall Week 3 CSCI-141 Scott C. Johnson.  Say we want to draw the following figure ◦ How would we go about doing this?
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 12 Recursion, Complexity, and Searching and Sorting
RecursionRecursion Recursion You should be able to identify the base case(s) and the general case in a recursive definition To be able to write a recursive.
Oct 15, 2007Sprenkle - CS1111 Objectives Creating your own functions.
Chapter 13 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Recursive Functions for Tasks(13.1) Recursive Functions.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 14 Recursion.
Chapter 17 Recursion Data Structures with Java © Rick Mercer Data Structures with Java © Rick Mercer.
Debuggers in Python. The Debugger Every programming IDE has a tool called a debugger. This application does NOT locate or fix your bugs for you! It slows.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Recursion.
Functions Part I (Syntax). What is a function? A function is a set of statements which is split off into a separate entity that can be used like a “new.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
Today… Modularity, or Writing Functions. Winter 2016CISC101 - Prof. McLeod1.
PROGRAMMING USING PYTHON LANGUAGE ASSIGNMENT 1. INSTALLATION OF RASPBERRY NOOB First prepare the SD card provided in the kit by loading an Operating System.
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Today… Functions, Cont.: –Designing functions. –Functional Decomposition –Importing our own module –A demo: Functional solution to assignment 2. Winter.
Function Recursion to understand recursion you must understand recursion.
Chapter 6 Functions The Tic-Tac-Toe Game. Chapter Content In this chapter you will learn to do the following: 0 Write your own functions 0 Accept values.
Recursion occurs when a method calls itself. public class RecursionOne { public void run(int x) { System.out.println(x); run(x+1); } public static void.
Functions. What is a Function?  We have already used a few functions. Can you give some examples?  Some functions take a comma-separated list of arguments.
Programming Logic and Design Fourth Edition, Introductory Chapter 2 Understanding Structure.
to understand recursion you must understand recursion
Software Development.
Data Structures with Java © Rick Mercer
Methods Chapter 6.
Programming Fundamentals Lecture #7 Functions
Data Structures Recursion CIS265/506: Chapter 06 - Recursion.
Chapter 14 Recursion. Chapter 14 Recursion Overview 14.1 Recursive Functions for Tasks 14.2 Recursive Functions for Values 14.3 Thinking Recursively.
to understand recursion you must understand recursion
Writing Functions( ) (Part 5)
Writing Functions( ) (Part 5)
Chapter 3 Simple Functions
Chapter 4 void Functions
Recursion This slide set was compiled from the Absolute Java textbook slides (Walter Savitch) and the instructor’s class materials.
Passing Parameters by value
Structured Programming Taken from notes by Dr. Neil Moore
What does this do? def revList(L): if len(L) < = 1: return L x = L[0] LR = revList(L[1:]) return LR + x.
Basics of Recursion Programming with Recursion
Recursion Taken from notes by Dr. Neil Moore
The structure of programming
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Introduction to Computer Science
Comp 249 Programming Methodology
The structure of programming
Thinking procedurally
Lecture 20 – Practice Exercises 4
Presentation transcript:

Call Stacks, Arguments and Returns And a bit about Recursion

Functions can call other functions! It is a natural tendency to break a big problem up into smaller problems This can be repeated several times – funA can be broken up into funA1 and funA2 and funA3, which can be broken into funA11 and funA12 and so on The execution proceeds just as in other examples of function calls. The interpreter keeps track of all these function calls using a data structure called a “call stack”.

More semantics - Call stacks (stack frames) To keep track of all the activity involved in a function call, the interpreter uses a data structure called a “call stack” It is a stack of records, each record is about a function call. When a function call is made, a record of that fact is put on top of the call stack. That indicates that that function is the one executing. When a function call is finished and is ready to return, its record is removed from the top of the stack to say it is no longer the one running. The record underneath that is the one belonging to the function which called.

More semantics – Arguments and Parameters Parameters are named in the def statement (can only be identifiers) They are placeholders, waiting to be filled in when the function runs There can be any number of parameters (but don’t go overboard! more than 10 is excessive) Arguments are named in the function call Arguments have values which are copied to the parameters Arguments can be identifiers, expressions, constants The number of arguments must match the number of parameters The names of arguments do NOT have to match the names of the parameters The types of arguments and parameters should match or at least be similar

The return statement The return statement tells Python that the execution of the current function (the function that the return statement is in) is done and that control passes back to the function which called it The return statement can have nothing after the keyword, in which case Python returns the special value None Or the return statement can have one or more expressions after the keyword (yes, a Python function can return multiple values!) When the return statement is executed, the function is finished – therefore, do NOT put code after a return statement, it will never execute!

Recursion Functions can call themselves! That is called Recursion. See chapter 12 for details. Recursion is a powerful technique but must be written carefully or it will cause infinite loops. Don’t worry about recursion until it is covered in class