Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)

Slides:



Advertisements
Similar presentations
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Advertisements

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)
Lists Introduction to Computing Science and Programming I.
Methods Liang, Chapter 4. What is a method? A method is a way of running an ‘encapsulated’ series of commands. System.out.println(“ Whazzup ”); JOptionPane.showMessageDialog(null,
Introduction to Methods
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.
Chapter 6: Functions.
A First Program CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits: a significant part of.
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.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Agenda Review C++ Library Functions Review User Input Making your own functions Exam #1 Next Week Reading: Chapter 3.
Functions CMSC 201. Motivation Using the tools we have so far, we can easily write code to find the largest number in a list, right?
Variables and Expressions CMSC 201 Chang (rev )
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
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.
Exam 1 Review Instructor – Gokcen Cilingir Cpt S 111, Sections 6-7 (Sept 19, 2011) Washington State University.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 17 Parameters, Scope, Return values.
ProgLan Python Session 4. Functions are a convenient way to divide your code into useful blocks, allowing us to: order our code, make it more readable,
Functions CMSC 201. Motivation Using the tools we have so far, we can easily write code to find the largest number in a list, right?
Math, Data Types. Python Math Operations OperationOperator Addition + Subtraction – Multiplication * Division (floating point) / Division (integer) //
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Python Let’s get started!.
CPSC 217 T03 Week VI Part #1: A2 Post-Mortem and Functions Hubert (Sathaporn) Hu.
Scope, Aliasing, Tuples & Mutability Intro2CS – week 4a 1.
1 Project 2: Using Variables and Expressions. 222 Project 2 Overview For this project you will work with three programs Circle Paint Ideal_Weight What.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
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.
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.
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.
CSC 1010 Programming for All Lecture 5 Functions Some material based on material from Marty Stepp, Instructor, University of Washington.
Math, Data Types. Python Math Operations OperationOperator Addition + Subtraction – Multiplication * Division (floating point) / Division (integer) //
Intro to CS Nov 29, 2016.
Functions A function is a block of code with a name. function functionName() { code to be executed; }
Python Let’s get started!.
CHAPTER 4 CLIENT SIDE SCRIPTING PART 3 OF 3
CS 1110 Introduction to Programming Spring 2017
Functions and Procedures
Variables, Expressions, and IO
Formatting Output.
Functions CIS 40 – Introduction to Programming in Python
CMSC201 Computer Science I for Majors Lecture 10 – Functions (cont)
Review.
INPUT & OUTPUT scanf & printf.
Functions and Procedures
6 Chapter Functions.
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Writing Functions( ) (Part 4)
Writing Functions( ) (Part 4)
5. Functions Rocky K. C. Chang 30 October 2018
Topics Introduction to Functions Defining and Calling a Void Function
Functions Jay Summet.
Introduction to Value-Returning Functions: Generating Random Numbers
Topics Introduction to Functions Defining and Calling a Function
15-110: Principles of Computing
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
COMPUTER PROGRAMMING SKILLS
CSE 231 Lab 4.
Scope Scope is the space within which a variable label exists and can be used. Python talks of names being bound to the code in an area and that determining.
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.
CISC101 Reminders Assignment 3 due today.
ITM 352 Functions.
def-ining a function A function as an execution control structure
 A function is a named sequence of statement(s) that performs a computation. It contains  line of code(s) that are executed sequentially from top.
Functions Jay Summet.
Presentation transcript:

Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)

def hello( ): print (“Hi, there!”) print (“I’m a function.”) print (“Good morning”) print (“Welcome to class”) hello( ) print (“And, now we’re done”) Flow of Execution

def hello( ): print (“Hi, there!”) print (“I’m a function.”) print (“Good morning”) print (“Welcome to class”) hello( ) print (“And, now we’re done”) Flow of Execution >>

def hello( ): print (“Hi, there!”) print (“I’m a function.”) print (“Good morning”) print (“Welcome to class”) hello( ) print (“And, now we’re done”) Flow of Execution >> Good morning

def hello( ): print (“Hi, there!”) print (“I’m a function.”) print (“Good morning”) print (“Welcome to class”) hello( ) print (“And, now we’re done”) Flow of Execution >> Good morning Welcome to class

def hello( ): print (“Hi, there!”) print (“I’m a function.”) print (“Good morning”) print (“Welcome to class”) hello( ) print (“And, now we’re done”) Flow of Execution >> Good morning Welcome to class

def hello( ): print (“Hi, there!”) print (“I’m a function.”) print (“Good morning”) print (“Welcome to class”) hello( ) print (“And, now we’re done”) Flow of Execution >> Good morning Welcome to class

def hello( ): print (“Hi, there!”) print (“I’m a function.”) print (“Good morning”) print (“Welcome to class”) hello( ) print (“And, now we’re done”) Flow of Execution >> Good morning Welcome to class Hi, there!

def hello( ): print (“Hi, there!”) print (“I’m a function.”) print (“Good morning”) print (“Welcome to class”) hello( ) print (“And, now we’re done”) Flow of Execution >> Good morning Welcome to class Hi, there! I’m a function.

def hello( ): print (“Hi, there!”) print (“I’m a function.”) print (“Good morning”) print (“Welcome to class”) hello( ) print (“And, now we’re done”) Flow of Execution >> Good morning Welcome to class Hi, there! I’m a function. And, now we’re done

def hello( ): print (“hey there!”) def goodbye( ): print (“see you later!”) print (“hi!”) hello( ) print (“I’ll see you again”) goodbye( ) Multiple Functions

def hello( ): print (“hey there!”) def goodbye( ): print (“see you later!”) print (“hi!”) hello( ) print (“I’ll see you again”) goodbye( ) Multiple Functions >> hi! hey there! I’ll see you again see you later!

def main( ): print (“I have a message!”) message( ) print (“Goodbye!”) def message( ): print (“The answer is C!”) print (“Here we go” ) main( ) Functions within a Function

def main( ): print (“I have a message!”) message( ) print (“Goodbye!”) def message( ): print (“The answer is C!”) print (“Here we go” ) main( ) Functions within a Function >> Here we go I have a message! The answer is C! Goodbye!

Functions are like “mini programs” You can create variables inside functions, as you would in your main source code Local Variables

def rectangle( ): x = float(input(“length?: ”)) #local variable y = float(input(“width?: ”)) #local variable area = x * y #local variable print (“Area: ”, area) rectangle( ) Local Variables

However, variables that are defined inside of a function, they are considered “local” to that function Therefore, these local variables cannot be accessed from outside the function’s “scope”, because they simple do not exist to the main program Local Variables

def rectangle( ): x = float(input(“length?: ”)) #local variable y = float(input(“width?: ”)) #local variable area = x * y #local variable print (“Area: ”, area) print( x * y ) # error, x and y are not defined Local Variables

Because local variables don’t “exist” outside the scope of the function, different functions can use the same name for variables defined in their function These local variables, although carrying the same name, do not overwrite one another Local Variables

def Donald( ): money = 3000#local variable print (“Donald has $”, money) def Johnson( ): money = 4000#local variable print (“Johnson has $”, money) Local Variables

Functions can also take arguments, some more than others, as we’ve seen already print(“hello”, “goodbye”, 75) # 3 arguments format(“hello”, “<20s”) # 2 arguments input(“length?: ”) # 1 argument Arguments are separated by commas Passing Arguments to a Function

We can define functions that pass an argument We need to specify what exactly we are going to pass into the function, and what to do with it. We can do this by the use of variables. Passing Arguments to a Function

def square(side): # whatever is passed into print(side ** 2) # the square( ) function # will assume the value # of the variable “side” square(5) # variable “side” is assuming value 5 >> 25 Local Variables

You can define a function that passes multiple arguments Each argument is separated by a comma Each argument is assigned to the variable in the position in which it is defined in the function Passing Multiple Arguments

def average (test1, test2, test3): sum = test1 + test2 + test3 avg = sum / 3 print(avg) #test1 assumes 95 #test2 assumes 98 average(95, 98, 88) #test3 assumes 88 >> Local Variables

When arguments are passed into a function, we are actually passing the “value” into the function, not the actual variable Rules for Arguments

def change (num): print(“original value:”, num) num = 16 print(“new value: “, num) num = 5 change(num) print(“value:”, num) Local Variables

def change (num): print(“original value:”, num) num = 16 print(“new value: “, num) num = 5>> original value: 5 change(num) new value: 16 print(“value:”, num) value: 5 Local Variables

We call this a “passing by value” It creates a one-way communication with the function, meaning we can send data into a function but the function cannot change the argument or communicate back to the main program (We will learn how to create a two-way communication street later) Rules for Arguments

As we discussed, most functions pass arguments by position def function (a, b, c): print(a, b, c) function(1, 2, 3) # variable a holds 1 # variable b holds 2 # variable c holds 3 Keyword Arguments

However, we can also pass arguments by assigning them keywords, or their variable names as defined in the function def function (a, b, c): print(a, b, c) function(b = 1, a = 2, c = 3) Keyword Arguments

You can also pass arguments both by position and with keywords However, you need to pass positional arguments first, then by the keywords Keyword Arguments

def function (a, b, c): print(a, b, c) function( 5, c = 2, b = 3) # here, a holds 5 # b holds 3 # and c holds 2 Keyword Arguments

We said that variables defined inside a function are called “local variables” When variables are created outside of a function, they are called “global variables” We’ve created these global variables all along Global variables can be accessed anywhere in the code, even from inside a function Global Variables

name = “Donald” def sayhello( ): print (“Hey there,”, name) print (“You are”, name) sayhello( ) Global Variables

You can change global variables inside of a function But you must first tell Python you want to do so by adding the keyword “global” before defining the variable inside your function Global Variables

name = “Donald” def sayhello( ): global name print (“Hey there,”, name) name = “Bryan” print (“Hey there,”, name) print (“You are”, name) sayhello( ) print (“You are”, name) Global Variables

name = “Donald” def sayhello( ): global name print (“Hey there,”, name) name = “Bryan” print (“Hey there,”, name) print (“You are”, name) sayhello( ) print (“You are”, name) Global Variables >>

name = “Donald” def sayhello( ): global name print (“Hey there,”, name) name = “Bryan” print (“Hey there,”, name) print (“You are”, name) sayhello( ) print (“You are”, name) Global Variables >>

name = “Donald” def sayhello( ): global name print (“Hey there,”, name) name = “Bryan” print (“Hey there,”, name) print (“You are”, name) sayhello( ) print (“You are”, name) Global Variables >> You are Donald

name = “Donald” def sayhello( ): global name print (“Hey there,”, name) name = “Bryan” print (“Hey there,”, name) print (“You are”, name) sayhello( ) print (“You are”, name) Global Variables >> You are Donald

name = “Donald” def sayhello( ): global name print (“Hey there,”, name) name = “Bryan” print (“Hey there,”, name) print (“You are”, name) sayhello( ) print (“You are”, name) Global Variables >> You are Donald

name = “Donald” def sayhello( ): global name print (“Hey there,”, name) name = “Bryan” print (“Hey there,”, name) print (“You are”, name) sayhello( ) print (“You are”, name) Global Variables >> You are Donald Hey there, Donald

name = “Donald” def sayhello( ): global name print (“Hey there,”, name) name = “Bryan” print (“Hey there,”, name) print (“You are”, name) sayhello( ) print (“You are”, name) Global Variables >> You are Donald Hey there, Donald

name = “Donald” def sayhello( ): global name print (“Hey there,”, name) name = “Bryan” print (“Hey there,”, name) print (“You are”, name) sayhello( ) print (“You are”, name) Global Variables >> You are Donald Hey there, Donald Hey there, Bryan

name = “Donald” def sayhello( ): global name print (“Hey there,”, name) name = “Bryan” print (“Hey there,”, name) print (“You are”, name) sayhello( ) print (“You are”, name) Global Variables >> You are Donald Hey there, Donald Hey there, Bryan You are Bryan

You can also pass arguments that are inputted by the user into a function def sayhello(name): print( “Hello”, name ) sayhello( input (“What’s your name: ”) ) Global Variables

def square(x): print(“The square of”, x, “is”, x**2) square( int ( input (“Give me a number: ”) ) ) >> Give me a number: 8 The square of 8 is 64 Global Variables

Write a program that defines three functions. One that finds the circumference of a circle, the area of a circle and the volume of a sphere. Then a main program that passes a single variable, which is the radius, into three subsequent functions. Set a global variable for pi and use it in your functions. Practice - Rectangles