Functions Jay Summet. Aug 29 2007 2 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)

Slides:



Advertisements
Similar presentations
Do I need a robot today? You only need a robot if your team has not shown me your octagon and/or the octagon in a function.
Advertisements

Μαθαίνοντας Python [Κ4] ‘Guess the Number’
Jay Summet CS 1 with Robots IPRE Python Review 1.
CS 1 with Robots Functions Institute for Personal Robots in Education (IPRE)‏
CSCI 116 Functions.
Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding.
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.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Functions and subroutines – Computer and Programming.
Course A201: Introduction to Programming 11/04/2010.
Topic 3 – The General Form of a C Program. CISC 105 – Topic 3 The General Form of a C Program Now, all of the basic building blocks of a C program are.
How to start Visual Studio 2008 or 2010 (command-line program)
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.
Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function.
Sep CS O'Hara1 CS 1301 Review Keith O’Hara
Python Functions.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Procedures and Modular Programming Part # 2. Interface Block ► Functions do not have to be internal to main program ► They can be stand-alone ► In this.
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,
Computer programming Outline Functions [chap 8 – Kochan] –Defining a Function –Arguments and Local Variables Automatic Local.
CCSA 221 Programming in C CHAPTER 8 – PART 1 WORKING WITH FUNCTIONS 1.
PHP. PHP User Defined Functions Besides the built-in PHP functions, we can create our own functions. A function is a block of statements that can be used.
Circumference Review. Review What is the relationship between a radius and a diameter? What does a circumference measure? What formulas do we use to calculate.
Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)
1 Functions, Part 1 of 2 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function Header Comments.
Review Expressions and operators Iteration – while-loop – for-loop.
Programming for GCSE Topic 8.1: Functions T eaching L ondon C omputing William Marsh School of Electronic Engineering and Computer Science Queen Mary University.
CSx 4091 – Python Programming Spring 2013 Lecture L2 – Introduction to Python Page 1 Help: To get help, type in the following in the interpreter: Welcome.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
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)
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.
Python Fundamentals: If Statements and Functions Eric Shook Department of Geography Kent State University.
CS0004: Introduction to Programming
Do I need a robot today? You only need a robot if your team has not shown me your octagon and/or the octagon in a function.
Web Design II Flat Rock Community Schools
Functions Jay Summet.
© 2010 David A Watt, University of Glasgow
Functions CIS 40 – Introduction to Programming in Python
Functions.
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Functions, Part 1 of 3 Topics Using Predefined Functions
Topics Introduction to Functions Defining and Calling a Void Function
Functions Institute for Personal Robots in Education (IPRE)‏
Functions Jay Summet.
G. Pullaiah College of Engineering and Technology
Topics Introduction to Functions Defining and Calling a Function
Functions, Part 1 of 3 Topics Using Predefined Functions
Functions Institute for Personal Robots in Education (IPRE)‏
Test Automation For Web-Based Applications
COMPUTER PROGRAMMING SKILLS
Functions Institute for Personal Robots in Education (IPRE)‏
Functions Jay Summet.
Introduction to Computer Science
CSE 231 Lab 4.
Python Functions.
Functions, Part 1 of 3 Topics Using Predefined Functions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Methods/Functions.
Functions I Creating a programming with small logical units of code.
ITM 352 Functions.
Top-Down Design with Functions
Functions Jay Summet.
Parameters and Arguments
Functions, Part 1 of 2 Topics Using Predefined Functions
Presentation transcript:

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) values, it does some work, and it (optionally) returns values You “call it”,”invoke it”, or “use it” by using its name and parentheses The things you pass it go inside the parentheses output = function( input )‏ function input output

Aug Using Simple Functions print( “Some Text” ) ‏ You call a function by using it's identifier (name) followed by parenthesis. If the function takes parameters, you insert them inside the parenthesis.

Aug Writing Simple Functions def sayHi(): print(“Hi there!”)‏ print(“Nice to meet you!”) sayHi()‏ Indent Defining functions Creates function Does not execute/run them Indenting indicates a “block” of code Call functions from top-level or other functions No Indention “Top Level”

Aug Format of a function definition def function-name(): statement …

Aug Writing Functions with Parameters def sayHi(name): print( “Hi there”, name) print( “Nice to meet you!”) sayHi(“Jay”)‏ sayHi(“Bob”)‏

Aug Parameters are Variables When you pass values into functions as parameters, they get assigned to the variable names declared in the definition line of the function. For example, when you call sayHi(“Jay”) The name variable is assigned (points to) the value “Jay” When the code in the function refers to the name variable, it evaluates to the string “Jay” So, when you call sayHi(“Jay”) and the sayHi function calls print(“Hi there”, name), it's the same as if it called print(“Hi there”, “Jay”)

Aug Format of a Function Definition with Parameters def function-name(param0, param1,...): statement … function-name(param0, param1,...)‏

Aug Using Functions that Return Values name = getName()‏ print( “Hello”, name)

Aug Composing Functions You can use the output (return value) of one function as the input (parameter) to another function. sayHi( getName() ) In this example, the getName() function executes first (things inside parenthesis execute before things outside parenthesis)‏ The getName() function returns a name, which is then given to the sayHi() function as a parameter.

Aug Writing Functions that Return Values def area(radius): return 3.14 * radius**2 def circumference(diameter): return 3.14 * diameter print( “Area of a 3 ft circle”, area(3)‏ ) print( “Circumference”, circumference(2*3)‏ )

Aug Return Statements The return statement is used to return a value from a function The return statement also affects the flow of execution Whenever the flow of execution hits a return statement it jumps back to the place where the function was called All functions have an implicit return statement at the end of the block of indented code, even if you do not specifically place one at the end of your function

Aug Functions with Local Variables def area(radius): a = 3.14 * radius**2 return a def circumference(diameter): c = 3.14 * diameter return c print( “Area of a 3 ft circle”, area(3)‏ ) print( “Circumference”, circumference(2*3)‏ )

Aug Variables in a Function are Local Variables in a function are private – Including the parameters Each function has its own variables – Even when the names are the same Allows you to write functions independently without worrying about using the same name

Aug Different Variables - Same Name def area(radius): a = 3.14 * radius**2 return a def circumference(radius): a = 3.14 * 2 * radius return a a = 20 print( “Area of a 3 ft circle”, area(3) )‏ print( “Circumference”, circumference(3)‏ ) print( a )

Aug Writing Functions with Return Values def function-name(list-of-params): statement … return value output = function-name(list-of-params)‏

Aug Passing variables to a functions If you pass a variable to a function, the function gets the value that the variable is pointing at userInput = input(“Enter your Name”)‏ sayHi(userInput)‏

Aug Functions in general # description of this function # what it expects as input # what is provides as output def function (p 0, p 2, …, p n ): statement … return value z = function(a 0, a 2, …, a n )‏

Aug Where’s the Error? def area: a = 3.14 * radius**2 return a print( “Area of a 3 ft circle”, area( 3‏ )

Aug Not enough Parentheses! def area: a = 3.14 * radius**2 return a print( “Area of a 3 ft circle”, area(3)‏ )

Aug Where’s the Error? def area( radius ): a = 3.14 * radius**2 return a print( “Area of a 3 ft circle”, area(3)‏ )

Aug No Indentation! def area(radius): a = 3.14 * radius**2 return a print( “Area of a 3 ft circle”, area(3)‏ )

Aug Where’s the Error? def area(radius): a = 3.14 * radius**2 return a print( “Area of a 3 ft circle”, area()‏ )

Aug No argument to the function! def area(radius): a = 3.14 * radius**2 return a print( “Area of a 3 ft circle”, area(3)‏ )

Aug Where's the Error? def area(radius): a = 3.14 * radius**2 print( “Area of a 3 ft circle”, area(3)‏ )

Aug No Return Value! def area(radius): a = 3.14 * radius**2 print( “Area of a 3 ft circle”, area(3)‏ )

Aug Where’s the Error? def area(radius): a = 3.14 * radius**2 return a area(3)‏ print( “Area of a 3 ft circle”, a )

Aug Don't save the return value into a! def area(radius): a = 3.14 * radius**2 return a area(3)‏ print( “Area of a 3 ft circle”, a )

Aug What’s the result? def area(radius): a = 3.14 * radius**2 return a v = area(3)‏ a = 16 print( “Area of a 3 ft circle”, v ) print( “value of a”, a)