CSE 231 Lab 4.

Slides:



Advertisements
Similar presentations
Functions Jay Summet. Aug Functions A function is a piece of code you can use over and over again Treat it like a black box You pass it (optional)
Advertisements

1 Functions Samuel Marateck © A function is a set of statements that can be referred by the function name. To start writing a function. 1. In the.
Chapter 2 Writing Simple Programs
CSC 107 – Programming For Science. Science Means Solving Problems  Physics – How does an atom work?
More Functions CS303E: Elements of Computers and Programming.
Python November 28, Unit 9+. Local and Global Variables There are two main types of variables in Python: local and global –The explanation of local and.
Chapter 6 Functions 1. Opening Problem 2 Find the sum of integers from 1 to 10, from 20 to 37, and from 35 to 49, respectively.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs.
Oct 15, 2007Sprenkle - CS1111 Objectives Creating your own functions.
C Programming Lecture 8-1 : Function (Basic). What is a Function? A small program(subroutine) that performs a particular task Input : parameter / argument.
CPS120: Introduction to Computer Science Functions.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Simple Functions and Names Sec 9-4 Web Design. Objectives The student will: Know how to create a simple function in Python. Know how to call a function.
Week 2 Lab2 Practice Dina A. Said
David Stotts Computer Science Department UNC Chapel Hill.
Topics: Function Intitution Algorithm Example Function Definition (and Indentation) Function Call Naming and Binding Local Variables Function Arguments.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski.
Math – What is a Function? 1. 2 input output function.
Introduction to Computing Using Python Namespaces – Local and Global  The Purpose of Functions  Global versus Local Namespaces  The Program Stack 
Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)
Introduction to Computing Using Python Namespaces – Local and Global  The Purpose of Functions  Global versus Local Namespaces  The Program Stack 
Q and A for Section 5.2 CS 106 Victor Norman. Function definition, call syntax def ( ): Note: is a comma-separated list of identifiers, called parameters,
2.1 Functions. Functions in Mathematics f x y z f (x, y, z) Domain Range.
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Function Basics. Function In this chapter, we will move on to explore a set of additional statements that create functions of our own function (subroutine,
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
Scope, Aliasing, Tuples & Mutability Intro2CS – week 4a 1.
Guide to Programming with Python Chapter Six Functions: The Tic-Tac-Toe Game.
Loops and Simple Functions COSC Review: While Loops Typically used when the number of times the loop will execute is indefinite Typically used when.
PYTHON FUNCTIONS. What you should already know Example: f(x) = 2 * x f(3) = 6 f(3)  using f() 3 is the x input parameter 6 is the output of f(3)
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
Functions with Arguments and Return Values, Oh My! CS303E: Elements of Computers and Programming.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
ITM 3521 ITM 352 Functions. ITM 3522 Functions  A function is a named block of code (i.e. within {}'s) that performs a specific set of statements  It.
CSC 1010 Programming for All Lecture 5 Functions Some material based on material from Marty Stepp, Instructor, University of Washington.
Lecture IV Functions & Scope ● Defining Functions ● Returning ● Parameters ● Docstrings ● Functions as Objects ● Scope ● Dealing with Globals ● Nesting.
Chapter 2 Writing Simple Programs
Lesson 06: Functions Class Participation: Class Chat:
Exam #1 You will have exactly 30 Mins to complete the exam.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Functions Jay Summet.
Functions Chapter 5 CS12 - Computer Programming 1 Chapter 5.
CS 1110 Introduction to Programming Spring 2017
Chapter 6 Functions.
Topic: Functions – Part 2
Fundamentals of Programming I Managing the Namespace
CMSC201 Computer Science I for Majors Lecture 10 – Functions (cont)
Functions Inputs Output
Teaching London Computing
Passing Parameters by value
Lesson 06: Functions Class Chat: Attendance: Participation
Suggested Layout ** Designed to be printed on white A3 paper.
Writing Functions.
Topics Introduction to Functions Defining and Calling a Void Function
Namespaces – Local and Global
Functions Jay Summet.
G. Pullaiah College of Engineering and Technology
Loops and Simple Functions
Functions Jay Summet.
Python Functions.
CSE 231 Lab 2.
What is a Function? Takes one or more arguments, or perhaps none at all Performs an operation Returns one or more values, or perhaps none at all Similar.
Namespaces – Local and Global
ITM 352 Functions.
Functions, Procedures, and Abstraction
Functions Jay Summet.
GCSE Computing.
Presentation transcript:

CSE 231 Lab 4

Functions Global VS local variables Topics to cover Functions Global VS local variables

Think a math function! F(x) = x^2 What is F(2) ? F(2) = 4 Functions Think a math function! F(x) = x^2 What is F(2) ? F(2) = 4 x = 2

Functions - Think a math function! F(x) = x^2 Functions have a Domain and a Range What is a Domain? All possible input of a function. What is a Range? All possible output of a function.

Functions – Back to Python What are functions? Why do we use functions? Examples of functions?

What are functions? Allow you to write code once and reuse it later Make code more readable, easier to debug Some examples of functions we’ve used: print(), input() Computer Science is full of buzzword, When you think functions, you think Abstraction.

Functions – Back to Python Functions in Python (and everywhere else in the world) have the option of: Accepting input. (Domain) Giving output. (Range)

Functions – What do they look like? Functions have a definition, parameters, and some have return value(s)

What is the output? Examples Example 1: def print_hi(): print(“Hi!”) def print_hi(name): print(“Hi”, name, “!”) print_hi(‘Bob’) Hi Bob! Example 3: def square(num): return num**2 Nothing Example 4: def print_hi(name): print(“Hi”, name, “!”) print_hi() Error

2.5 15 Examples def average(number1, number2): return average avg = average(2,3) print(avg) 2.5 num1 = 10 num2 = 20 avg = average(num1,num2) print(avg) 15

Functions – Multiple returns def get_double_and_triple(num): return num*2 num*3 double_two, triple_two = get_double_and_triple(2) print(“2 * 2 =”, double_two, ”| 2 * 3 =”, triple_two”) >>> SyntaxError: invalid syntax Why?

Functions – Multiple returns def get_double_and_triple(num): return num*2, num*3 double_two, triple_two = get_double_and_triple(2) print(“2 * 2 =”, double_two, ”| 2 * 3 =”, triple_two”) >>> 2 * 2 = 4 | 2 * 3 = 6 IMPORTANT

Global VS Local Variables

Local def local_test(): local_variable = “Hi, I’m Local!” print(local_variable) What’s the issue here?

Local def local_test(): local_variable = “Hi, I’m Local!” print(local_variable) local_variable only accessible inside local_test()!

Global x = 10 y = 4 def print_globals(y): y = 3 print(x) #10 print(y) #3 print_globals(2) print(y) #4

Global x = 10 y = 20 def mess_with_globals(): x += 10 y += 10 print(x) #What is the output? print(y) #What is the output?

Global x = 10 y = 20 def mess_with_globals(): x += 10 y += 10 print(x) #10 print(y) #20 X and y are global because they were defined outside of a function Nothing happen because you did not call the function

Global x = 10 y = 20 def mess_with_globals(): x += 10 y += 10 print(x) print(y) X and y are global because they were defined outside of a function UnboundLocalError: local variable ‘x' referenced before assignment

Global x = 10 y = 20 def mess_with_globals(): global x global y print(x) #20 print(y) #30 x and y are global because they were defined outside of a function All good. But this is not a good practice. If you want to change a variable, pass the variable to the function and return the result

All good Global x = 10 y = 20 def mess_with_globals(x,y): x += 10 return x,y x,y = mess_with_globals(x,y) print(x) #20 print(y) #30 All good

Local + some global  local_variable = “I’m a liar” def local_test(): local_variable = “Hi, I’m Local!” print(local_variable) #I’m a liar local_variable is actually a global variable.

Some things to remember! Function variables are independent of your main variables Do NOT use globally defined variables in functions Do NOT declare functions inside loops Coding standards, no. 2 If a function needs to access/modify a variable, pass the variable to the function and return the result