Writing Functions( ) (Part 4)

Slides:



Advertisements
Similar presentations
CSC 270 Nov. 22, 2005 Last Day of Scheme Dr. Stephen Bloch
Advertisements

CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Introduction to Computing Science and Programming I
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
Lists/Tuples. Example Write a program to keep track of all students’ scores on exam 1. Need a list of everyone’s score Use 14 doubles What about next.
Main task -write me a program
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.
Programming Functions: Passing Parameters by Reference.
First Program  Open a file  In Shell  Type into the file: 3  You did it!!! You wrote your first instruction, or code, in python!
Oct 15, 2007Sprenkle - CS1111 Objectives Creating your own functions.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Practice with Lists and Strings CS303E: Elements of Computers and Programming.
Coding Design Tools Rachel Gauci. What are Coding Design Tools? IPO charts (Input Process Output) Input- Make a list of what data is required (this generally.
NA2204.1jcmt CSE 1320 Intermediate Programming C Program Basics Structure of a program and a function type name (parameters) { /* declarations */ statement;
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.
Logic Our programs will have to make decisions in terms of what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if.
Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski.
End of unit assessment Challenge 1 & 2. Course summary So far in this course you have learnt about and used: Syntax Output to screen (PRINT) Variables.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
Passing Arguments, Local/Global Variables Writing Functions( ) (Part 2)
Count Controlled Loops (Nested) Ain’t no sunshine when she’s gone …
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
COMPUTER PROGRAMMING Year 9 – lesson 1. Objective and Outcome Teaching Objective We are going to look at how to construct a computer program. We will.
Functions with Arguments and Return Values, Oh My! CS303E: Elements of Computers and Programming.
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.
Math, Data Types. Python Math Operations OperationOperator Addition + Subtraction – Multiplication * Division (floating point) / Division (integer) //
Variables Symbols used to represent unknown numbers or values.
Environments and the Contour Model
Line Continuation, Output Formatting, and Decision Structures
Formatting Output.
Formatting Output.
Functions CIS 40 – Introduction to Programming in Python
Functions.
Lecture 07 More Repetition Richard Gesick.
Lecture 4B More Repetition Richard Gesick
Review.
Standard Deviation.
Standard Deviation.
Higher-Order Procedures
Line Continuation, Output Formatting, and Decision Structures
PARTIAL DIFFERENTIATION 1
10 Real Numbers, Equations, and Inequalities.
Chapter 2 - Introduction to C Programming
Conditions and Ifs BIS1523 – Lecture 8.
INPUT & OUTPUT scanf & printf.
Chapter 2 - Introduction to C Programming
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Passing Parameters by value
Adapted from slides by Marty Stepp and Stuart Reges
Remembering lists of values lists
Chapter 2 - Introduction to C Programming
Writing Functions( ) (Part 4)
Computing Fundamentals
Python 19 Mr. Husch.
Chapter 2 - Introduction to C Programming
COMPUTER PROGRAMMING SKILLS
Introduction to Computer Science
PARTIAL DIFFERENTIATION 1
Chapter 7 Empowering Programs with Math
Standard Deviation.
Python 19 Mr. Husch.
Hint idea 2 Split into shorter tasks like this.
CISC101 Reminders Assignment 3 due today.
Functions Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Presentation transcript:

Writing Functions( ) (Part 4) Returning Values

Returning Values Now that we’ve created a communication street with our functions, let’s bring it back around We can define our functions to send back information to us using the “return” keyword Remember, that the functions must then be called to a variable location!

Return Value def cube ( num1 ): cubed = num1 ** 3 return cubed number = int(input(“Give me a number:”)) cube(number) # this will not output anything because the returned value does not have a location nor was it printed

Return Value def cube ( num1 ): cubed = num1 ** 3 return cubed number = int(input(“Give me a number:”)) new = cube(number) print(new)

Return Value def cube ( num1 ): cubed = num1 ** 3 return cubed number = int(input(“Give me a number:”)) print(cube(number)) Note: you can return values to be printed right away

Practice Define a function that passes three arguments denoting three different test scores from the user and returns the average of the test scores Then, call the function with the given test scores of 96, 88 and 90 and print out the average

Practice Define a main function that asks the user for the test scores and passes them into the average function you created from the previous exercise Run this main function for two different users and compare values to see who got the higher grade Print ONLY the higher grade average and congratulate that user

Returning Multiple Values As with passing arguments, we can define functions to return multiple values at a time. Again there is a warning, you must return all values into the appropriate number of variable locations Values will be returned to their respective located variables

Return Value def powers( number ): squared = number ** 2 cubed = number ** 3 fourth = number ** 4 return squared, cubed, fourth num1, num2 = powers(10) # this will cause an error because the function returns 3 values and is called into only two locations

Return Value def powers( number ): squared = number ** 2 cubed = number ** 3 fourth = number ** 4 return squared, cubed, fourth num1 = powers(10) # this will not cause an error but the three values will be returned to the variable num1 as a list

Return Value def powers( number ): squared = number ** 2 cubed = number ** 3 fourth = number ** 4 return squared, cubed, fourth num1, num2, num3 = powers(10)

IPO Notation Stands for Input, Processing, and Output notation. Programmers use IPO to describe the type of function they need to create. When given IPO notation, you must follow the rules for the number of arguments expect (input), what it should do (processing) and the number of values returned (output)

IPO Notation IPO notation is usually written as a comment inside of source code Example: # Input: passes 2 arguments # Processing: adds the two numbers # Output: returns 1 value as a sum def function (a, b): return a + b

Practice Define a function that passes four arguments (in any order), alphabetizes them and returns all four names in alphabetical order into separate variables labeled: “first”, “second”, “third”, and “fourth” Call the function with the input function passed four times as each of the required arguments and have the names printed out in alphabetical order, and then in reverse order

Practice Define a function that: Input: passes four arguments Processing: alphabetizes arguments, sorts from least to greatest Output: returns four values in order from least to greatest Call the function with the input function passed four times as each of the required arguments and have the names printed out in alphabetical order, and then in reverse order

Practice Now, combine the main average function and the alphabetizing function to see if we can organize which of the fours users achieved the highest grade Then, print out the scores from high to low