© 2010 David A Watt, University of Glasgow

Slides:



Advertisements
Similar presentations
4 If-Statements © 2010 David A Watt, University of Glasgow Accelerated Programming 2 Part I: Python Programming.
Advertisements

Python Basics: Statements Expressions Loops Strings Functions.
Chapter 2 Writing Simple Programs
Functions and abstraction Michael Ernst UW CSE 190p Summer 2012.
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.
Fundamentals of Python: From First Programs Through Data Structures
Functions.
Python quick start guide
Python Programming Fundamentals
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
Munster Programming Training
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Computer Science 101 Introduction to Programming.
8 For-Statements © 2010 David A Watt, University of Glasgow Accelerated Programming 2 Part I: Python Programming 1.
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.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Loops and Simple Functions CS303E: Elements of Computers and Programming.
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.
1 MODULAR DESIGN AND ABSTRACTION. 2 SPECIFYING THE DETAILS OF A PROBLEM INTO A RELATED SET OF SMALLER PROBLEMS.
5 While-Statements © 2010 David A Watt, University of Glasgow Accelerated Programming 2 Part I: Python Programming.
Algorithm Discovery and Design Objectives: Interpret pseudocode Write pseudocode, using the three types of operations: * sequential (steps in order written)
Data Types and Conversions, Input from the Keyboard CS303E: Elements of Computers and Programming.
3 Basics © 2010 David A Watt, University of Glasgow Accelerated Programming 2 Part I: Python Programming.
Data Types and Conversions, Input from the Keyboard If you can't write it down in English, you can't code it. -- Peter Halpern If you lie to the computer,
Loops and Simple Functions COSC Review: While Loops Typically used when the number of times the loop will execute is indefinite Typically used when.
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 (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Functions with Arguments and Return Values, Oh My! CS303E: Elements of Computers and Programming.
PH2150 Scientific Computing Skills Control Structures in Python In general, statements are executed sequentially, top to bottom. There are many instances.
CSC 1010 Programming for All Lecture 5 Functions Some material based on material from Marty Stepp, Instructor, University of Washington.
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
Chapter 2 Writing Simple Programs
Chapter 9: Value-Returning Functions
Introduction to Programming
Topics Designing a Program Input, Processing, and Output
Topic: Functions – Part 1
Introduction to Programming
Introduction to Python
Topic: Python’s building blocks -> Statements
Data Types and Conversions, Input from the Keyboard
Line Continuation, Output Formatting, and Decision Structures
Case Statements and Functions
Organization of Programming Languages
Functions CIS 40 – Introduction to Programming in Python
Functions.
Introduction to Programming
Line Continuation, Output Formatting, and Decision Structures
Arithmetic operations, decisions and looping
Python Primer 2: Functions and Control Flow
5. Functions Rocky K. C. Chang 30 October 2018
Functions Jay Summet.
Introduction to Value-Returning Functions: Generating Random Numbers
Topics Designing a Program Input, Processing, and Output
Python Basics with Jupyter Notebook
Experiment No. (1) - an introduction to MATLAB
Topics Designing a Program Input, Processing, and Output
Loops and Simple Functions
COMPUTER PROGRAMMING SKILLS
Introduction to Computer Science
Class code for pythonroom.com cchsp2cs
Functions Taken from notes by Dr. Neil Moore & Dr. Debby Keen
def-ining a function A function as an execution control structure
Functions Jay Summet.
Using Modules.
PYTHON - VARIABLES AND OPERATORS
Unit Testing.
Lecture 2 - Names & Functions
Presentation transcript:

© 2010 David A Watt, University of Glasgow Accelerated Programming 2 Part I: Python Programming 2 Functions © 2010 David A Watt, University of Glasgow

Python provides a variety of “built-in” functions. Arithmetic functions: abs(x) returns the absolute value of x max(x,y) returns the maximum of x and y min(x,y) returns the minimum of x and y round(x) returns the value of x rounded to the nearest integer

Built-in functions (2) Input functions: raw_input(p) displays prompt p, awaits user input, then returns the input as a string input(p) displays prompt p, awaits user input, then returns the input value (typically a number)

Example: using built-in functions (1) Program: n1 = input('Enter a number: ') n2 = input('Enter another number: ') m = max(n1, n2) print 'The larger number is', m Program’s output and input: Enter a number: 48 Enter another number: 63 The larger number is 63

Example: using built-in functions (2) Program: name = raw_input('Enter your name: ') print 'Hullo', name Program’s output and input: Enter your name: David Hullo David

Function calls A function call is an expression that invokes a named function, passing argument(s). E.g.: abs(10-n) max(n1, n2) input('Enter a number: ') The function call contains sub-expression(s) enclosed in parentheses. These sub-expressions are evaluated to determine the argument(s), which are passed to the named function.

To define a new function: Defined functions To define a new function: Specify the function. Decide what argument(s) the function will accept, and what the function is supposed to do. Choose a name for the function. It is good practice to choose a meaningful name that reflects what the function is supposed to do. The function needs parameter(s) – one for each argument. Choose a name for each parameter. Implement the function. Its header contains the name of the function and the name(s) of its parameter(s). Its body is a block of statements to do what the function is supposed to do.

Example: defining a function (1) Suppose that we need a function to print consecutive integers and their squares. The program of §1 did this for integers 2, 3, 4. Let us generalize this to integers p, …, q. Specification: The function will have two integer arguments, p and q. It will print each integer in the range p…q together with its square. Let us name the function print_squares. It will have two parameters, named p and q.

Example: defining a function (2) Implementation of the function: def print_squares (p, q): # Print each integer in the range p … q # together with its square. n = p-1 while n < q: n = n+1 print n, n**2

Example: defining a function (3) The following program calls the function: print_squares(2, 4) print '****' The function call “print_squares(2, 4)”: evaluates the arguments, here 2 and 4 respectively assigns these arguments to the parameters, p and q respectively executes the function body of print_squares, thus producing the following output: 2 4 3 9 4 16

Example: geometric program (1) Let us develop a program that: reads the radii of two circles displays the circumference and area of each circle. The relevant formulae are: circumference = 2 π r area = π r2 Clearly the program must compute the circumference and area using these formulae.

Example: geometric program (2) We could implement this directly: pi = 3.14159 r1 = input('Radius of circle: ') print 'Circumference is', 2 * pi * r1 print 'Area is', pi * (r1 ** 2) r2 = input('Radius of another circle: ') print 'Circumference is', 2 * pi * r2 print 'Area is', pi * (r2 ** 2) But this program: is not very readable. contains duplicated code.

Example: geometric program (3) Better, define functions to compute the circumference and area of a circle: pi = 3.14159 def circum (r): return 2 * pi * r def area (r): return pi * (r ** 2) r1 = input('Radius of circle: ') print 'Circumference is', circum(r1) print 'Area is', area(r1) r2 = input('Radius of circle: ') print 'Circumference is', circum(r2) print 'Area is', area(r2)

Functions and procedures Most Python functions return results. E.g., circum area Python functions that return results, but have no other effects, are like mathematical functions. Some Python functions do not return results. They only perform effects such as input/output. E.g.: print_squares These are called procedures in some programming languages.

Return-statements If a function is to return a result, the function body must execute a return-statement. If the function body does not execute a return-statement, it returns a special value None. This will cause the program to fail if the function call expects (say) a number or string to be returned!

Example: returning a result If abs were not a built-in function, we could define it ourselves: def abs (x): # Return the absolute value of the number x. if x < 0: return –x else: return x

The body of a function must be indented w.r.t. the function heading: Indentation (1) The body of a function must be indented w.r.t. the function heading: def f (…): … … … … … … function body The indentation shows the extent of the function body. Standard practice is to indent by 4 spaces consistently.

In each case, the indentation shows the extent of the statement. Similarly, the body of an if- or while- or for-statement must be indented w.r.t. the statement header: if …: while …: for …: … … … … … … … … … … … … else: … … … … In each case, the indentation shows the extent of the statement.