Week 7 - Friday CS 113.

Slides:



Advertisements
Similar presentations
Summer Computing Workshop. Introduction to Variables Variables are used in every aspect of programming. They are used to store data the programmer needs.
Advertisements

Programming Logic and Design Fourth Edition, Introductory
CS 106 Introduction to Computer Science I 02 / 26 / 2007 Instructor: Michael Eckmann.
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
Main task -write me a program
In.  This presentation will only make sense if you view it in presentation mode (hit F5). If you don’t do that there will be multiple slides that are.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
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.
Python Programming Chapter 6: Iteration Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
Oct 15, 2007Sprenkle - CS1111 Objectives Creating your own functions.
For loops in programming Assumes you have seen assignment statements and print statements.
Python Functions.
Course A201: Introduction to Programming 09/16/2010.
1 Structure of a C Program (continued) Presentation original from Dr. Turner’s class USF - COP C for Engineers Summer 2008.
Methods. Methods also known as functions or procedures. Methods are a way of capturing a sequence of computational steps into a reusable unit. Methods.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Week 8 - Friday.  What did we talk about last time?  Static methods.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Week 10 - Friday.  What did we talk about last time?  References and primitive types  Started review.
Lecture 6: Methods MIT-AITI Kenya © 2005 MIT-Africa Internet Technology Initiative In this lecture, you will learn… What a method is Why we use.
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.
Week 7 - Friday.  What did we talk about last time?  Software engineering  Testing  Programming languages.
Development Environment
More about comments Review Single Line Comments The # sign is for comments. A comment is a line of text that Python won’t try to run as code. Its just.
Week 10 - Wednesday CS 113.
Week 3 - Wednesday CS 113.
Topics Introduction to Functions Defining and Calling a Void Function
Introduction to Python
Week 3 - Friday CS222.
IF statements.
Week 8 - Friday CS 121.
Programming Fundamentals Lecture #7 Functions
Week 15 – Wednesday CS 121.
Functions CIS 40 – Introduction to Programming in Python
CompSci 230 Software Construction
Functions.
Learning to program with Logo
Call Stacks, Arguments and Returns
Procedures Programming Guides.
Learning to Program in Python
Python 17 Mr. Husch.
Functions BIS1523 – Lecture 17.
Introduction to pseudocode
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
CS190/295 Programming in Python for Life Sciences: Lecture 6
Python 17 Mr. Husch.
Topics Introduction to Functions Defining and Calling a Void Function
Elements of a Python Program
Week 4 Lecture-2 Chapter 6 (Methods).
Topics Introduction to Functions Defining and Calling a Function
Chapter 3: Selection Structures: Making Decisions
CISC101 Reminders All assignments are now posted.
IPC144 Introduction to Programming Using C Week 4 – Lesson 1
IPC144 Introduction to Programming Using C Week 4 – Lesson 2
COMPUTER PROGRAMMING SKILLS
Introduction to Computer Science
Module 5 ● Vocabulary 1 a sensing block which will ask whatever question is typed into the block and display an input text box at the bottom.
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.
Class code for pythonroom.com cchsp2cs
Functions Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Functions, Procedures, and Abstraction
 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.
Week 5 - Friday CS 121.
Week 1 - Friday COMP 1600.
Presentation transcript:

Week 7 - Friday CS 113

Last time What did we talk about last time? Software engineering Testing Programming languages

Questions?

Project 2

Functions

Idea of a function Functions allow you to package up some code to run over and over Functions usually take some input (like numbers or text) so that they can be customized Functions often give back an answer (like the square root of a number) Also called methods in some other languages The customizable purple blocks played the role of functions in Scratch

Advantages of functions More modular programming Break a program into separate tasks Each task could be assigned to a different programmer Code reusability Use code over and over Even from other programs Less code duplication Improved readability Each function can do a few, clear tasks

Defining a function Name of 1st argument Function name def name( arg1, … , argn ): statement1 statement2 … statementm Required syntax Name of last argument Code done by function (must be indented)

Simple function example Given two numbers, find the smaller: def smaller(a, b): if a < b: return a else: return b

Value returning functions It is possible to divide functions into two types: Functions that return values Functions that don't return values Functions that do return values give an answer: It's as if the function is replaced by whatever answer is returned x = 3 y = 4 small = smaller(x, y) #small contains 3

Functions that don't return values A function doesn't have to return an answer: This function only prints things to the screen A function that doesn't return anything does some task but doesn't give back an answer def callForHelp(times): for i in range(times): print("Help!")

return statements Like most code in Python, the code inside of a function executes line by line Of course, you are allowed to put if statements and loops inside methods You can also put in return statements A function will stop executing and jump back to wherever it was called from when it hits a return The return statement is where you put the value that will be given back to the caller

Calling functions Defining a function is only half the game You have to call functions to use them Calling a function means giving it the parameters (or arguments) it needs and then waiting for its answer By now, you have done many function calls print() You can call your own functions the same way

Calling functions callForHelp(3) #calls for help 3 times You type the name of the function and then its arguments in parentheses If it's a value returning function, you can store the answer it gives back The arguments you give can be values or variables You have to give the function the right number of arguments or the program will have an error callForHelp(3) #calls for help 3 times result = smaller(9, 2)

Key syntax Every function starts with def Then comes the name of the function Followed by the names of the arguments in parentheses Followed by a colon (:) Like loops and if statements, the code inside a function is indented one level Call the function in other code by typing the name and the correct number of arguments in parentheses

Function example Lets say we want to make a function that will draw three balls stacked on top of each other The bottom one is green, the middle is orange, and the top is red We want the function to take a radius and a position The bottom ball will be drawn at the position with the radius given The middle ball will be above it with half the radius The top ball will be above that with one quarter the radius

Function code Here's the function stacks() which takes a radius and a position and draws the balls def stacks(radius, position): sphere(radius=radius, pos=position, color=color.green) newPosition=vector(position.x, position.y + 3*radius/2,position.z) sphere(radius=radius/2, pos=newPosition, color=color.orange) newPosition=vector(position.x, position.y + 9*radius/4, position.z) sphere(radius=radius/4, pos=newPosition, color=color.red)

Calling the function Now we can call the function several times Each time, it will draw a stack with the size and location we specify Result shown to the right stacks(1, vector(0,0,0)) stacks(.5, vector(2,3,1)) stacks(.2, vector(-2,1,2))

Lab 7

Upcoming

Next time… Moore's law Multicore computers Complex decisions

Reminders Finish Project 2 Reading Python Chapter 7 Due tonight before midnight! Reading Python Chapter 7 Think about what you want to do for your Final Project Proposal due by 3/31