Lecture08-Functions-part2-3pp

Cmput174 funcaons when a funcaon is called python

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: s an argument, the code inside the funcAon can change that object deletes the element at index 0 we’re printing the list outside the function • “Side ­effect”: those changes are visible outside of that funcAon notice that 0 is gone 4 CMPUT174  ­ ­ FuncAons • When a funcAon is called, Python starts execuAng the code inside the funcAon definiAon call How Python executes func%ons? • Python needs to remember “where it was” before the funcAon was called!   When we call f() in line 7, we must go to line 8 once we return from f() 5 CMPUT174  ­ ­ FuncAons How Python executes func%ons? • The Call Stack is what Python uses to remember where it is during the execuAon, at any point in Ame previous execution point call   Most programming languages work this way too! current execution point CMPUT174  ­ ­ FuncAons 6 2 13 ­10 ­09 Stacks • Linear data structures (like lists) where we add/remove elements from one of the endpoints   pushing an element adding   popping an element removing • Recall:   Lists: add/remove anywhere   Stacks: add/remove at the front 7 CMPUT174  ­ ­ FuncAons Python and the call stack • Start with the main program at the top of the stack • When a funcAon is called, push a new “frame” in the stack, corresponding to the call • When we finish a funcAon, pop the corresponding frame and conAnue from the top (i.e., previous frame) main program function called 8 CMPUT174  ­ ­ FuncAons Python and the call stack • Another example: a funcAon calling another funcAon call call main program call to g() call to f() CMPUT174  ­ ­ FuncAons 9 3...
View Full Document

This document was uploaded on 03/02/2014.

Ask a homework question - tutors are online