INLS 560 – F UNCTIONS Instructor: Jason Carter.

Slides:



Advertisements
Similar presentations
Python Basics: Statements Expressions Loops Strings Functions.
Advertisements

Introduction to C++ Functions Chapter 6 Sections 6.1 – 6.3 Computer II.
Programming Logic and Design Fourth Edition, Introductory
Program Design and Development
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.
Python.
INLS 560 – V ARIABLES, E XPRESSIONS, AND S TATEMENTS Instructor: Jason Carter.
Python Programming Fundamentals
Fruitful functions. Return values The built-in functions we have used, such as abs, pow, int, max, and range, have produced results. Calling each of these.
Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:
Functions Part I (Syntax). What is a function? A function is a set of statements which is split off into a separate entity that can be used like a “new.
Builtins, namespaces, functions. There are objects that are predefined in Python Python built-ins When you use something without defining it, it means.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
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 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
Functions and subroutines – Computer and Programming.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Functions.
Course A201: Introduction to Programming 11/04/2010.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 3 Simple.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Oct 15, 2007Sprenkle - CS1111 Objectives Creating your own functions.
With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.
Functions. Built-in functions You’ve used several functions already >>> len("ATGGTCA")‏ 7 >>> abs(-6)‏ 6 >>> float("3.1415")‏ >>>
Procedural programming in Java Methods, parameters and return values.
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
C463 / B551 Artificial Intelligence Dana Vrajitoru Python.
Python Functions.
Namespace, scope, compile time activities, runtime activities When do the small integer values get stored in RAM? How did the names in the builtin namespace.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
1 Structure of a C Program (continued) Presentation original from Dr. Turner’s class USF - COP C for Engineers Summer 2008.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
FUNCTIONS. Topics Introduction to Functions Defining and Calling a Void Function Designing a Program to Use Functions Local Variables Passing Arguments.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
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.
Python Let’s get started!.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 3: Built-in functions.
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.
Lesson 06: Functions Class Participation: Class Chat:
Chapter 9: Value-Returning Functions
Topics Introduction to Functions Defining and Calling a Void Function
CS 1110 Introduction to Programming Spring 2017
Chapter 6 Functions.
Functions CIS 40 – Introduction to Programming in Python
Functions.
Starting Out with Programming Logic & Design
Chapter 3 Simple Functions
Chapter 4 void Functions
Lesson 06: Functions Class Chat: Attendance: Participation
Topics Introduction to Functions Defining and Calling a Void Function
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Introduction to Value-Returning Functions: Generating Random Numbers
G. Pullaiah College of Engineering and Technology
Topics Introduction to Functions Defining and Calling a Function
Starting Out with Programming Logic & Design
COMPUTER PROGRAMMING SKILLS
Global Variables Created by assignment statement placed at beginning of program and outside all functions Can be accessed by any statement in the program.
IST256 : Applications Programming for Information Systems
Class code for pythonroom.com cchsp2cs
def-ining a function A function as an execution control structure
CPS125.
 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.
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

INLS 560 – F UNCTIONS Instructor: Jason Carter

C HANGE THE P ROGRAM Change the lines that say “The first name you entered has no characters” “The last name you entered has no characters” “The birthday you entered has no characters” TO Lines that say “The first name you entered is empty” “The last name you entered is empty” “The birthday you entered is empty”

Make changes 3 places

W HY F UNCTIONS ? Makes your program easier to read and debug. Groups statements If changes have to be made, only one place to make them in Reduce repetitive code. Makes programs smaller. Reuse Well-designed functions are often useful for many programs Debug once

F UNCTION A named sequence of statements that performs a computation Pick a name for a function that describes what it does just like variable names Usually one task of a large program Functions can be executed in order to perform overall program task Known as divide and conquer approach Modularized program: program wherein each task within the program is in its own function Function InputOutput

D IVIDE AND C ONQUER Program to make dinner make_appetizier() make_salad() make_entrée() make_dessert()

T WO T YPES OF F UNCTIONS Built-in User defined

B UILT - IN F UNCTIONS

B UILT - IN F UNCTIONS To use functions we call them print “Hello World” print (“Hello World”) What is the input to the print function? print InputOutput “Hello World” What is the output of the print function? Printing the words Hello World to the console

B UILT - IN F UNCTIONS len Return the length (the number of items) of an object. The argument may be a sequence (such as a string, bytes, tuple, list, or range) or a collection (such as a dictionary, set, or frozen set). len(“Hello World”) What is the input to the len function? print InputOutput “Hello World” What is the output of the len function? 11

B UILT - IN F UNCTIONS abs Return the absolute value of a number. The argument may be a plain or long integer or a floating point number. If the argument is a complex number, its magnitude is returned abs(-4.5) What is the input to the abs function? abs InputOutput -4.5 What is the output of the len function? 4.5

B UILT - IN F UNCTIONS Print the length of the string “Hello World” to the console print(len (“Hello World”)) length = len (“Hello World”); print( length ) Print the absolute value of -4.5 to the console print(abs (-4.5)) abs_value= abs (-4.5) print( abs_value )

T WO T YPES OF F UNCTIONS Built-in User defined

U SER D EFINED F UNCTIONS def name(): statement Function Header Notice how the statements are indented!!! This is how python knows where a function begins and ends

I NDENTATION Each block must be indented Lines in block must begin with the same number of spaces Use tabs or spaces to indent lines in a block, but not both as this can confuse the Python interpreter Pycharm automatically indents the lines in a block Blank lines that appear in a block are ignored

C ALLING U SER D EFINED F UNCTIONS After defining a function, to run the code inside, you call the function. When a function is called: Interpreter jumps to the function and executes statements in the block Interpreter jumps back to part of program that called the function

F LOW OF E XECUTION The order in which statements are executed Execution always begins at the first statement of the program Statements are executed one at a time, in order from top to bottom. name(): statement name() Execution starts here

C ALLING U SER D EFINED F UNCTIONS name(): statement name() After defining a function, you can call it any number of times Each time it is called Python acts as if you had typed in all the lines of the function definition A function call must come after the function definition!

MAIN () F UNCTION From this point on, always define a main() function in your programs Always call the main()function as the last line in your program. main function: called when the program starts Calls other functions when they are needed Defines the mainline logic of the program

E XAMPLE U SER D EFINED F UNCTIONS

A RGUMENTS AND P ARAMETERS User Defined functions can take input just like built- in functions

E XAMPLE A RGUMENTS AND P ARAMETERS

L OCAL V ARIABLE Local variable: variable that is assigned a value inside a function Belongs to the function in which it was created Only statements inside that function can access it, error will occur if another function tries to access the variable Scope: the part of a program in which a variable may be accessed For local variable: function in which created

L OCAL V ARIABLE A local variable cannot be accessed by statements inside its function which precede its creation Different functions may have local variables with the same name Each function does not see the other function’s local variables, so no confusion

E XAMPLE

P ARAMETERS ARE L OCAL V ARIABLES “That sounds like local variables.” Just as local variables are invisible outside of the function that owns them, variables used as parameters inside a function definition are local to that function. Parameters in a function definition are really local variables that are created and assigned values automatically when the function is called.

T YPES OF F UNCTIONS User defined functions has output like built-in functions Output can be printed something to the console or screen Functions that return a value are called “fruitful functions” Functions that do not return a value are called void functions

F RUITFUL F UNCTIONS Return keyword

V OID F UNCTIONS

I N -C LASS E XAMPLE Using functions, write a program that prompts the user for 3 numbers and outputs the average of those numbers.

G LOBAL V ARIABLES Global variable : created by assignment statement written outside all the functions Can be accessed by any statement in the program file, including from within a function DO NOT USE GLOBAL VARIABLES! Global variables making debugging difficult Many locations in the code could be causing a wrong variable value Functions that use global variables are usually dependent on those variables Makes function hard to transfer to another program Global variables make a program hard to understand!

G LOBAL C ONSTANTS Global constant: global name that references a value that cannot be changed OK to use global constants in a program To simulate global constant in Python, create global variable and do not re-declare it within functions

G LOBAL C ONSTANT E XAMPLE

I N -C LASS E XAMPLE Write a Python program that asks the user to input 2 numbers and outputs the sum of those numbers. Use 2 functions main(): - Prompts the user to enter 2 numbers and calls sum() sum(): - Takes in 2 parameters and outputs the sum of those numbers