Functions!!! Why functions? Functions provide good way to design systems!

Similar documents
CHAPTER 23 FUNCTIONS AND PARAMETERS

The float type and more on variables FEB 6 TH 2012

CS1004: Intro to CS in Java, Spring 2005

Unit 1: Binary and Java Input and Output. Sample Test (Inquiry) Name : Totals: /70 (!c), /9 (c), /79 (!c&&c)

Computational Physics

Lecture 02, Fall 2018 Friday September 7

CSCA08 Winter 2018 Week 2: Variables & Functions. Marzieh Ahmadzadeh, Brian Harrington University of Toronto Scarborough

Lecture 17: Classes (Chapter 15)

Scope and Parameter Passing 1 / 19

At full speed with Python

Module 01: Introduction to Programming in Python

A simple interpreted language

Introduction to: Computers & Programming: Review prior to 1 st Midterm

ECE 122. Engineering Problem Solving with Java

About Variables in Python F E B 1 1 T H

Table of Contents EVALUATION COPY

CHAPTER 2: Introduction to Python COMPUTER PROGRAMMING SKILLS

Functions. Python Part 3

Introduction to Python

Intro. Comparisons. > x > y if and only if x is bigger than y. < x < y if and only if x is smaller than y.

Intro. Classes & Inheritance

Chapter 8: Intraprogram Communication. Lecture 8 1

Python Programming Exercises 1

printf( Please enter another number: ); scanf( %d, &num2);

ENGR 102 Engineering Lab I - Computation

Conditional Expressions and Decision Statements

Introduction to computers and Python. Matthieu Choplin

Introduction to Python (All the Basic Stuff)

Introducing Python Modules

Functions, Scope & Arguments. HORT Lecture 12 Instructor: Kranthi Varala

Modularization. Functions and Modules. Functions. Functions how to define

Python for Analytics. Python Fundamentals RSI Chapters 1 and 2

CS 115 Lecture 4. More Python; testing software. Neil Moore

CMSC 201 Fall 2018 Python Coding Standards

CSC326 Python Imperative Core (Lec 2)

CS 102 Lab 3 Fall 2012

Chapter 9: Right Triangle Trigonometry

Python for C programmers

Object-Oriented Programming Concepts

Lecture 7. Memory in Python

Downloaded from Chapter 2. Functions

In Java we have the keyword null, which is the value of an uninitialized reference type

CS 302: INTRODUCTION TO PROGRAMMING IN JAVA. Lecture 16

Hello, World! An Easy Intro to Python & Programming. Jack Rosenthal

CSC 110 Final Exam. ID checked

Simple Algorithms. cs1

2SKILL. Variables Lesson 6. Remembering numbers (and other stuff)...

ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I

Final thoughts on functions F E B 2 5 T H

Getting Started. Office Hours. CSE 231, Rich Enbody. After class By appointment send an . Michigan State University CSE 231, Fall 2013

Lecture. Defining Functions. Richard E Sarkis CSC 161: The Art of Programming

CircuitPython 101: Working with Lists, Iterators and Generators

Hello World! Computer Programming for Kids and Other Beginners. Chapter 1. by Warren Sande and Carter Sande. Copyright 2009 Manning Publications

>>> * *(25**0.16) *10*(25**0.16)

Com S 127 Lab 2. For the first two parts of the lab, start up Wing 101 and use the Python shell window to try everything out.

Midterm 1 Review. Important control structures. Important things to review. Functions Loops Conditionals

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s

Jython. secondary. memory

Python Programming: An Introduction to Computer Science

Module 01 Processing Recap

Try and Error. Python debugging and beautification

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

61A Lecture 4. Monday, September 9

CHAPTER 6 DEFINING FUNCTIONS Python Programming: An Introduction to Computer Science

Functions Structure and Parameters behind the scenes with diagrams!

What is a class? Responding to messages. Short answer 7/19/2017. Code Listing 11.1 First Class. chapter 11. Introduction to Classes

Functions with Parameters and Return Values

Lecture Numbers. Richard E Sarkis CSC 161: The Art of Programming

1. BASICS OF PYTHON. JHU Physics & Astronomy Python Workshop Lecturer: Mubdi Rahman

Introduction to Internet of Things Prof. Sudip Misra Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Lecture 5. Functions II. Functions with Arguments. CptS 121 Summer 2016 Armen Abnousi

Introduction to Python

Introduction to: Computers & Programming Defining Identifiers: Objects with Names

Introduction to Computer Programming CSCI-UA 2. Review Midterm Exam 1

Slide 1 Side Effects Duration: 00:00:53 Advance mode: Auto

Key Differences Between Python and Java

Lecture 9. Memory and Call Stacks

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Lecture 4. Defining Functions

Functions: Decomposition And Code Reuse

Computing with Numbers Zelle - Chapter 3

More Loop Examples Functions and Parameters

Symbol Tables. ASU Textbook Chapter 7.6, 6.5 and 6.3. Tsan-sheng Hsu.

COMP1730/COMP6730 Programming for Scientists. Data: Values, types and expressions.

CS193D Handout 10 Winter 2005/2006 January 23, 2006 Pimp Your Classes

APS105. Modularity. C pre-defined functions 11/5/2013. Functions. Functions (and Pointers) main. Modularity. Math functions. Benefits of modularity:

COMP-202: Foundations of Programming. Lecture 26: Review; Wrap-Up Jackie Cheung, Winter 2016

C# AND.NET FRAMEWORK 1 CCET PREPARED BY R.VINODINI

Introduction to programming with Python

Use of scanf. scanf("%d", &number);

Chapter 6 Classes and Objects

Programming Training. Main Points: - Python Statements - Problems with selections.

WELCOME! (download slides and.py files and follow along!) LECTURE 1

Basic Concepts. Computer Science. Programming history Algorithms Pseudo code. Computer - Science Andrew Case 2

Introduction to: Computers & Programming Defining Identifiers: Objects with Names

Be careful when deciding whether to represent data as integers or floats, and be sure that you consider all possible behaviors in computation.

Introduction. C provides two styles of flow control:

7. MODULES. Rocky K. C. Chang October 17, (Based on Dierbach)

Binghamton University. CS-211 Fall Variable Scope

Writing Functions. Reading: Dawson, Chapter 6. What is a function? Function declaration and parameter passing Return values Objects as parameters

Transcription:

Functions!!! Why functions? Functions provide good way to design systems!

Coding Design! DRY principle - Don't Repeat Yourself! Loops help with this! Functions/procedures/modules help even more! Modular design Functions/procedures/modules help with this!

Functions!!! What's a function?! we've already been using lots of functions: print() int(), str(), float() math.sqrt() help()

Functions!!! What's a function?! a function is a stored set of instructions that perform a specific task We can 'define' new functions Let's see an example!

Functions!!! def welcome(): '''prints an intro to our program''' print('welcome user!') print( We're learning functions! ) print('tomorrow... who knows!')

Functions!!! How do we use functions?

Calling Functions def welcome(): '''prints an intro to our program''' print('welcome user!') print( We're learning functions! ) print('tomorrow... who knows!') # call the welcome function welcome()

Making programs more modular def welcome(): '''prints an intro to our program''' print('welcome user!') print('we're learning functions!') print('tomorrow... who knows!') ''' main program logic ''' welcome() # other program logic

Making programs more modular def welcome(): '''prints an intro to our program''' print('welcome user!') print('we're learning functions!') print('tomorrow... who knows!') def main(): ''' main program logic ''' welcome() # other program logic main()

Functions!!! comprised of: name - name used to call the function parameters (optional) - variables passed to the function In addition to parameters being optional themselves, there are two types of parameters: required - parameters that a function requires optional - some parameters can be optional parameters (e.g. print's 'sep' and 'end' parameters) code - statements that run on given parameters return value (optional) - result returned by the function

Variable Scope Scope is the context in which a variable is accessible (what code is the variable accessible in) Two main types of variables and their scope: Global variables Local variables

Global Variables Global variables - variables declared outside of a function or class that are accessible by the main program and other functions in the main program. These variables are accessible globally and modifiable globally (from functions) If accessed in a function you need to use the keyword 'global' when first declaring it so that python knows you're trying to use a global variable (otherwise it defaults to a local variable) Generally a bad idea to use globals These are what we have been using (so far)

Local Variables Local variables - variables that are defined inside a specific code context and are not accessible outside of that context. In this case inside of a function. In functions we sometimes call these 'function variables' Deleted after function is done running Using local variables instead of global variables ensures good modular design in general, a function shouldn't be dependent on code outside of itself, it should be self contained.

Parameters Parameters are variables that are passed to a function. Local to the function they are passed to (not accessible outside the function) Changing the contents of a parameter does not change the contents of a original variable passed to that function. Lets see an example.

Function - Required Parameters def welcome(topic): '''prints an intro to our program param - topic is printed as part of the intro ''' print('welcome user!') print( We're learning about, topic) print('tomorrow... who knows!')

Function - Optional Parameters def welcome(topic='functions'): '''prints an intro to our program param - topic is printed as part of the intro ''' print('welcome user!') print( We're learning about, topic) print('tomorrow... who knows!')

Function - Required & Optional Parameters # optional parameters come last def welcome(topic, print_tomorrow=true): '''prints an intro to our program''' print('welcome user!') print( We're learning about, topic) if print_tomorrow: print('tomorrow... who knows!')

Function - Return Values def addition(x, y): '''Does simple addition returns - addition of x and y''' return x+y

Function - Return Values def addition(x, y): '''Does simple addition returns - addition of x and y''' return x+y def main(): print('3 plus 4 equals', addition(3,4)) main()

Functions with Keywords When calling a function, we can pass parameters using keywords instead of passing them in the same order as the function definition Example: def volume_cone(radius, height): # CODE GOES HERE # all of these function calls work and are equivalent volume_cone(2,3) # using default ordering volume_cone(radius='2', height='3') # using keywords volume_cone(height='3', radius='2') # using keywords

Functions with Keywords When calling a function, we can pass parameters using keywords instead of passing them in the same order as the function definition Pro: You don't have to remember the order of parameters as long as you remember the name of the parameters Con: If the function parameter names change, all function calls will need to be changed (what if the parameter in the function definition was misspelled?)

Libraries/Modules A library is a set of utilities that provide functionality that apply to a specific topic (using functions). math libraries - import math python interpreter system libraries - import sys operating system libraries - import os random number libraries - import random date related libraries - import date