Functions Victor Norman CS104 Calvin College. Reading Quiz Counts toward your grade.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

FUNCTIONS. • Function เป็นชื่อของชุดคำสั่ง • Syntax ของฟังก์ชั่น Def NAME (PARAMETERS ): STATEMENTS • NAME เป็นชื่อของฟังก์ชัน สามารถใช้ชื่อใดๆ ตามกฎการตั้งชื่อ.
Selection Victor Norman CS104 Calvin College. Reading Quiz Counts toward your grade.
Do I need a robot today? You only need a robot if your team has not shown me your octagon and/or the octagon in a function.
Q and A for Chapter 6 CS 104 Victor Norman. Return values Q: A function definition that returns a value (i.e., a non-void function) must have at least.
Q and A for Chapter 3.4 – 3.14 CS 104 Victor Norman.
What Data Do We Have? Sections 2.2, 2.5 August 29, 2008.
1 Gentle Introduction to Programming Session 2: Functions.
 To be able to write larger programs ◦ By breaking them down into smaller parts and passing data between the parts.  To understand the concepts of Methods.
Strings Victor Norman CS104 Calvin College. Reading Quiz Counts toward your grade.
Introduction to Methods
Turtle Graphics Victor Norman CS104 Calvin College.
1 Chapter 9 Scope, Lifetime, and More on Functions.
The Python interpreter CSE 140 University of Washington Michael Ernst.
FUNCTIONS. Function call: >>> type(32) The name of the function is type. The expression in parentheses is called the argument of the function. Built-in.
Fall Week 3 CSCI-141 Scott C. Johnson.  Say we want to draw the following figure ◦ How would we go about doing this?
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
COMPSCI 101 Principles of Programming Lecture 25 – Nested loops, passing mutable objects as parameters.
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.
Functions, Procedures, and Abstraction Dr. José M. Reyes Álamo.
Chapter Function Basics CSC1310 Fall Function function (subroutine, procedure)a set of statements different inputs (parameters) outputs In.
Announcements Project1 Due Wednesday September 21 st 4:30pm We will post instructions on how to turnin from home Note: you can always turn in from the.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Functions Chapter 4 Python for Informatics: Exploring Information
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.
Simple Python Data Victor Norman CS104 Calvin College.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Python Functions.
Introduction to Computing Using Python for loop / def- ining a new function  Execution control structures ( if, for, function call)  def -ining a new.
Introduction to Computer Programming
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 17 Parameters, Scope, Return values.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Sudeshna Sarkar, IIT Kharagpur 1 Functions Lecture
1 Structure of a C Program (continued) Presentation original from Dr. Turner’s class USF - COP C for Engineers Summer 2008.
Q and A for Section 5.2 CS 106 Victor Norman. Function definition, call syntax def ( ): Note: is a comma-separated list of identifiers, called parameters,
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.
Mr. Hudson’s Review Test on Thursday. Topics Arrays Traversing Loops Selections Identifying code throughout a program Type Casting Conversion of numbers.
Programming Seminar 2009 Night 0. Why we’re holding this seminar You’re probably not a computer science major – Or even anything remotely close (e.g.
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
Lists Victor Norman CS104. Reading Quiz Lists Our second collection data type – elements are in order (like strings) – indexed from 0 to n – 1 (like.
Functions Chapter 4 Python for Informatics: Exploring Information Slightly modified by Recep Kaya Göktaş on March 2015.
4 March 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 3: Built-in functions.
Week 4 – Functions Coding Functions. Purpose of Coding Functions A function is written to perform a well-defined task; rather than having all logic in.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
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.
Programming what is C++
Introduction to Programming
Input and Output Upsorn Praphamontripong CS 1110
Web Design II Flat Rock Community Schools
Functions and Procedures
Functions CIS 40 – Introduction to Programming in Python
CMSC201 Computer Science I for Majors Lecture 10 – Functions (cont)
Example 1.
CS 1111 Introduction to Programming Fall 2018
Procedure Activations
Python Functions Part I Presented by : Sharmin Abdullah
G. Pullaiah College of Engineering and Technology
Nate Brunelle Today: Functions
The structure of programming
COMPUTER PROGRAMMING SKILLS
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Nate Brunelle Today: Functions
IST256 : Applications Programming for Information Systems
def-ining a function A function as an execution control structure
Functions, Procedures, and Abstraction
Parameters and Arguments
Using Modules.
Presentation transcript:

Functions Victor Norman CS104 Calvin College

Reading Quiz Counts toward your grade.

Function Def’n vs. Call Definition: def funcname(param_names): statements to execute when funcname is called Call: y = int(input(“Enter your age: “)) result = funcname(y) – How many function calls in this code? Call must provide the same # of values as the function definition requires.

Function Defn is an Abstraction A function abstracts and names lines of code. – can be called repeatedly – can take arguments. Consider: y = sin(x). Takes a value x, runs the sin function on it, producing a result that y is made to refer to.

Non-fruitful Functions Non-fruitful functions produce no value for the caller. – Just execute their statements – Not useful unless they do something like print something or make the robot move, etc. – Useful for turtle graphics. E.g., def drawSquare(size): code here to draw a square of size. Returns nothing.

Fruitful Function Computes a value and returns it to the caller. def squared(val): newval = val * val return newval Return sends value back to the caller and exits the code in the function. Function will exit the code anyway at the end of the function.

Mistakes CS104ers Make Provide literals as parameters in function definition: def xyz(p1, “hello”): # code here Ignoring the parameter value. def xyz(p1): p1 = 3 # code here using p1

Clicker Questions Given this code: 1 def foo(x): 2 return x + 1 3def bar(x, y): 4 t = foo(x) 5 return t + y 6def baz(z): 7 t = bar(z, z) 8 return foo(t) 9n = 3 10res = baz(n) 11print(res)

Parameters and Local Variables Parameters hold the *values* being passed in in the function call. Both are defined only within the code of the function. – That is their scope or lifetime. They can be altered within the code. Can think of them as "temporary variables" -- places to hold values while you work on computing the result or doing the work.

Return Statement Computes the result to be sent back to the caller. Exits the code in the function, even if there is code after it. E.g., def computeIt(x, y, z): w = x + y / z return w print("Done!") # never executed

Return vs. print() Printing a value in a function does not return it. – print sends characters out to the output “stream” – i.e., the screen. To send a value back to the caller you must use return

Clicker Questions Given this code: 1 def beatHope(i): 2 i = i * i 3 print("Let's beat Hope", i, "times") 4print(beatHope(3))

Assignment Do ~21 questions from the Functions section in CodeLab for Saturday, 11:59:59 pm. Study for test on Thursday.