1 Functions Samuel Marateck ©2010. 2 A function is a set of statements that can be referred by the function name. To start writing a function. 1. In the.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Parameter passing mechanism: pass-by-reference. The Pass-by-reference mechanism - the agreement Recall: Parameter passing mechanism = agreement between.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
1 Loops. 2 Often we want to execute a block of code multiple times. Something is always different each time through the block. Typically a variable is.
1 Python Chapter 3 Reading strings and printing. © Samuel Marateck.
1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.
COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007.
PYTHON: LESSON 1 Catherine and Annie. WHAT IS PYTHON ANYWAY?  Python is a programming language.  But what’s a programming language?  It’s a language.
Recitation 1 Programming for Engineers in Python.
CS61A Lecture 2 Functions and Applicative Model of Computation Tom Magrino and Jon Kotker UC Berkeley EECS June 19, 2012.
Working with Numbers in Alice - Converting to integers and to strings - Rounding numbers. - Truncating Numbers Samantha Huerta under the direction of Professor.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Builtins, namespaces, functions. There are objects that are predefined in Python Python built-ins When you use something without defining it, it means.
FUNCTIONS. Function call: >>> type(32) The name of the function is type. The expression in parentheses is called the argument of the function. Built-in.
Introduction to Programming Workshop 2 PHYS1101 Discovery Skills in Physics Dr. Nigel Dipper Room 125d
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
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.
1 Functions 1 Parameter, 1 Return-Value 1. The problem 2. Recall the layout 3. Create the definition 4. "Flow" of data 5. Testing 6. Projects 1 and 2.
EET 2259 Unit 7 Case Structures; Sequence Structures  Read Bishop, Sections 5.4 and 5.5.  Lab #7 and Homework #7 due next week.  Quiz #3 next week.
Lists Computers and Programming. Agenda What is a list? How to access elements in the list? The for statement Operations on lists Looping with.
COMPSCI 101 Principles of Programming Lecture 25 – Nested loops, passing mutable objects as parameters.
Oct 15, 2007Sprenkle - CS1111 Objectives Creating your own functions.
Program Errors and Debugging Week 10, Thursday Lab.
Week 2 Lab2 Practice Dina A. Said
Functions. Built-in functions You’ve used several functions already >>> len("ATGGTCA")‏ 7 >>> abs(-6)‏ 6 >>> float("3.1415")‏ >>>
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #001 (January 17, 2015)
1 Chapter 9. To familiarize you with  Simple PERFORM  How PERFORM statements are used for iteration  Options available with PERFORM 2.
Copyright Curt Hill The C/C++ switch Statement A multi-path decision statement.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
CS 177 Week 10 Recitation Slides 1 1 Debugging. Announcements 2 2.
Python Basics  Functions  Loops  Recursion. Built-in functions >>> type (32) >>> int(‘32’) 32  From math >>>import math >>> degrees = 45 >>> radians.
February 7, You have seen how If statement works from last week’s activities. You have learned that If statement is a conditional statement. Today,
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)
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
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.
CSC 1010 Programming for All Lecture 5 Functions Some material based on material from Marty Stepp, Instructor, University of Washington.
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
Basic concepts of C++ Presented by Prof. Satyajit De
User-Written Functions
Introduction to Programming
COMP 14 Introduction to Programming
CS 1110 Introduction to Programming Spring 2017
FIGURE 4-10 Function Return Statements
CS 1110 Introduction to Programming Spring 2017
A Lecture for the c++ Course
Chapter 6 Functions.
Functions CIS 40 – Introduction to Programming in Python
PH2150 Scientific Computing Skills
Procedures Programming Guides.
Iteration: Beyond the Basic PERFORM
CS 1111 Introduction to Programming Fall 2018
Faculty of Computer Science & Information System
Suggested Layout ** Designed to be printed on white A3 paper.
Introduction to Programming
Conditional and iterative statements
Suppose I want to add all the even integers from 1 to 100 (inclusive)
Chapter 6: User-Defined Functions I
1D Arrays and Lots of Brackets
Loops and Simple Functions
COMPUTER PROGRAMMING SKILLS
CSE 231 Lab 4.
Python Functions.
Functions So far we've been using functions from the builtin module, which is automatically loaded in most cases. Occasionally we've accessed functions.
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
Introduction to Programming
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.
Presentation transcript:

1 Functions Samuel Marateck ©2010

2 A function is a set of statements that can be referred by the function name. To start writing a function. 1. In the File menu in the shell choose New Window. 2. In the new window start by writing the header def name(): where name is the name you choose for your function

3 The following is a function that will print NYU 10 times. def tenTimes(): for j in range(10): print(‘NYU’) Note that the header starts with def and ends with a colon. The function name must be followed by (). We will later see what the () are used for.

4 The colon here serves the same function as it does for the if statement. It automatically indents the following statements. def tenTimes(): for j in range(10): print(‘NYU’)

5 Before you execute the function, you must save it. Do this by choosing Save on the File menu and then Run Menu on the Run menu. When you do this, you will be taken to the shell where you will see >>>

6 Type the function name and (). >>>tenTimes(). This will be followed by the output.

7 In order to run the function from the new window do the following: def tenTimes(): for j in range(10): print(‘NYU’) tenTimes()

8 Now when you hit Run Module, the output will appear in the shell. Although this is a convenient way of running the function, it will not work if you want to execute many functions.

9 The best way of running the program is to write a main() function as shown here. def tenTimes(): for j in range(10): print(‘NYU’) Def main(): tenTimes() main()

10 Again when you hit Run Module, the output will be displayed in the shell. You don’t have to call the method that calls the function, main() as we see here. We call it duh(). def tenTimes(): for j in range(10): print(‘NYU’) Def duh(): tenTimes() duh()

11 If you wanted the upper limit of the for loop to be fed to the function, you could do it by using a parameter, the entity appearing between the (). Thus you would write,

12 def tenTimes(n): for j in range(n): print(‘NYU’) def main(): m = 10 tenTimes(m) main()

13 Here m is the actual parameter and n is the dummy or formal parameter. The value of m is copied into n. This is a one-way process as we will soon explain. The actual and dummy parameters do not have to be different variables.

14 Here both the actual and dummy parameters are n. def tenTimes(n): for j in range(n): print(‘NYU’) Def main(): n = 10 tenTimes(n) main()

15 Just as before there are two locations. The n in main() labels a different location than n in tenTimes does. Now let’s see what happens if we change the value of the dummy parameter in a function if it changes the value of the actual parameter

16 Now let’s see what happens if we change the value of the dummy parameter in a function if it changes the value of the actual parameter. def change(num): num = 4 def main(): n = 8 change(n) print(n) main() What value of n is printed in main()?

17 What value of n is printed in main()? def change(num): num = 4 def main(): n = 8 change(n) print(n) main() The function main() prints 8

18 Now we see a function with two parameters: def f(x, y): print(x + y**2) def main(): f(1, 3) main()

19 def f(x, y): print(x + y**2) def main(): f(1, 3) main() The order of the 1 and 3 determined what is printed f(3,1) would give a different answer.

20 def f(x, y): print(x + y**2) def main(): f(3, 1) main()

21 What happens if we want a function to return a value. Let’s write a function that returns the sum of integers to n:

22 What happens if we want a function to return a value. Let’s write a function that returns the sum of integers to n: def add(n): sum = 0 for j in range(1,n + 1): sum = sum + j return sum The return statement returns execution to the main() function.

23 How do we write main() so that we can use add() in main()?

24 How do we write main() so that we can use add() in main()? def add(n): sum = 0 for j in range(1,n + 1): sum = sum + j return sum def main(): total = add(5) print(total) main() We use a return statement at the end of the function add. This returns the execution to add(5) and the result is assigned to total.

25 Can we write main() in another way so that we can call add(n)? def add(n): sum = 0 for j in range(1,n + 1): sum = sum + j return sum

26 Can we write main() in another way? def add(n): sum = 0 for j in range(1,n + 1): sum = sum + j return sum def main(): print(add(5)) main()

27 Now we call add() in the print(). def add(n): sum = 0 for j in range(1,n + 1): sum = sum + j return sum def main(): print(add(5)) main() The value returned to add(5) is printed in main().

28 Whenever the results of a function are assigned to a variable in the calling function, here main(), or used in a print(), the function must have a return statement. Let’s see what happens if we forget to use a return statement

29 def increment(x): y = x + 1 def main(): z = increment(x) print(z) main()

30 def increment(x): y = x + 1 def main(): z = increment(x) print(z) main() Since increment doesn’t return any value, nothing can be assigned to z. What is printed?

31 def increment(x): y = x + 1 def main(): z = increment(x) print(z) main() Since increment doesn’t return any value, nothing can be assigned to z. What is printed? The computer prints None.

32 We can use more than one return in a function: def greater(a, b): if a > b: return a else: return b def main(): print(greater(3, 7)) main()

33 Using a default parameter: def greater(a, b = 6): if a > b: return a else: return b def main(): z = greater(3) print(z) main()

34 Using a default parameter: def greater(a, b = 6): if a > b: return a else: return b def main(): z = greater(3) print(z) main() The actual parameters are now 3, and by default, 6

35 Using a default parameter: def greater(a, b = 6): if a > b: return a else: return b def main(): z = greater(3) print(z) main() The default parameters must follow the regular parameter, so def greater( a = 6, b): causes an error.

36 Using a default parameter. If we use two actual parameters, they override the default parameter. def greater(a, b = 6): if a > b: return a else: return b def main(): z = greater(3,8) print(z) main() The value 8 is printed.

37 The main() function can call more than one function: def greater(a, b): if a > b: return a else: return b def f(x,y): z = x + y**2 return z def main(): u = greater(3, 7) print(u) s = f(3,4) print(s) main()