CS 1110 Introduction to Programming Spring 2017

Slides:



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

Def f(n): if (n == 0): return else: print(“*”) return f(n-1) f(3)
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Q and A for Chapter 3.4 – 3.14 CS 104 Victor Norman.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 7: User-Defined Functions II.
1 Functions Samuel Marateck © A function is a set of statements that can be referred by the function name. To start writing a function. 1. In the.
16&17-2 Grasp the concept of top-down programming Identify Function Headers and Prototypes Understand when and where prototypes used Understand how arguments.
Builtins, namespaces, functions. There are objects that are predefined in Python Python built-ins When you use something without defining it, it means.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Oct 15, 2007Sprenkle - CS1111 Objectives Creating your own functions.
Python Programming Using Variables and input. Objectives We’re learning to build functions and to use inputs and outputs. Outcomes Build a function Use.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
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.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 17 Parameters, Scope, Return values.
Week 2. Functions: int max3(int num1, int num2, int num3) {int result; result = max(max(num1,num2),num3); return result; } //max3.
Functions CMSC 201. Motivation Using the tools we have so far, we can easily write code to find the largest number in a list, right?
Objective: Students will be able to: Declare and use variables Input integers.
Java methods Methods break down large problems into smaller ones Your program may call the same method many times saves writing and maintaining same code.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
INLS 560 – F UNCTIONS Instructor: Jason Carter.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Chapter 15. Modules Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.
PYTHON FUNCTIONS. What you should already know Example: f(x) = 2 * x f(3) = 6 f(3)  using f() 3 is the x input parameter 6 is the output of f(3)
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
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.
CSC 1010 Programming for All Lecture 5 Functions Some material based on material from Marty Stepp, Instructor, University of Washington.
Chapter 9: Value-Returning Functions
Chapter 7: User-Defined Functions II
CS 1110 Introduction to Programming Spring 2017
Chapter 10: Void Functions
Functions CIS 40 – Introduction to Programming in Python
User-Defined Functions
Call Stacks, Arguments and Returns
Passing Parameters by value
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
CS 1111 Introduction to Programming Fall 2018
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
CS 1111 Introduction to Programming Fall 2018
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Python Functions Part I Presented by : Sharmin Abdullah
Topics Introduction to Functions Defining and Calling a Void Function
Suppose I want to add all the even integers from 1 to 100 (inclusive)
Module 1-10: Recursion.
Chapter 7: User-Defined Functions II
Introduction to Value-Returning Functions: Generating Random Numbers
CS 1111 Introduction to Programming Spring 2019
Functions Imran Rashid CTO at ManiWeber Technologies.
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
CISC101 Reminders Assignment 3 due today.
Nate Brunelle Today: Functions again, Scope
The Python interpreter
Global Variables Created by assignment statement placed at beginning of program and outside all functions Can be accessed by any statement in the program.
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
More Basics of Python Common types of data we will work with
def-ining a function A function as an execution control structure
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Defining Functions.
Presentation transcript:

CS 1110 Introduction to Programming Spring 2017 Functions (2) CS 1110 Introduction to Programming Spring 2017

Print versus Return Print Return Review Print versus Return Print All print statements reached by the function are executed They are printed to the screen After a print statement is executed, the execution proceeds to the next statement Return A return statement is optional Only first return statement reached gets run If no return statement, function returns None A return ceases execution of a function and returns a value At most one (the first) return statement that is reached during a particular function call is executed A return value is not printed, unless a function is printed print(add(2, 3)) CS 1110

Void versus Value-Returning Functions Review Void versus Value-Returning Functions Void functions Does not return anything None in Python Examples print(str) random.seed(seed) random.shuffle() Value-Return functions return something Examples abs(some_number) random.randInt(0, 100) random.sample(some_list) CS 1110

Tracing through Code with Functions Review Tracing through Code with Functions Pass by value  copy of actual value Pass by reference  copy of the memory address seven steps for every function call: http://www.pythontutor.com/visualize.html CS 1110

Importing Existing Functions Review Importing Existing Functions Be careful when naming your file. If the file name is the same as Python standard objects/libraries, the interpreter gets confused !! import random up_time = 210 number_of_breakdowns = 16 num_runs = int(input("How many simulation runs? : ")) seed = int(input("Enter a seed (0 for random) : ")) if seed != 0: random.seed(seed) # sets the integer starting value used in generating random numbers sum_MTBF = 0 for run in range(num_runs): likelihood = random.random() MTBF = (up_time * likelihood) / number_of_breakdowns sum_MTBF += MTBF print(likelihood) print("Estimated MTBF over " + str(num_runs) + " simulations = " , sum_MTBF/num_runs) CS 1110

Importing Existing Functions Review Importing Existing Functions Assuming the add() function and other functions are saved in a file called math_lib.py From … import * allows us to import all functions from math_lib.py into the current file Call functions from other files Can use add() without defining it here Python imports some standard functions, such as str() and len() automatically; others need the import statement Put at the top of the current file from math_lib import * add(2, 3) print(add(4, 5)) print(add(1, -1)) CS 1110

Calling Functions from Functions def main(): print('The sum of 12 and 45 is ') show_sum(12, 45) def show_sum(num1, num2): result = num1 + num2 print(result) main() main num1 show_sum num2 12 45 result 57 none The sum of 12 and 45 is 57 CS 1110

Local Variables number = 0 def main(): number = int(input('Enter a number: ')) show_number() def show_number(): print('The number you entered is ', number) main() Enter a number: 7 The number you entered is 0 CS 1110

Global Variables number = 0 def main(): global number number = int(input('Enter a number: ')) show_number() def show_number(): print('The number you entered is ', number) main() Enter a number: 7 The number you entered is 7 CS 1110

Calling Functions with Named/Optional Parameters def my_function(name="Mary", school="UVa"): print(name + " goes to " + school) # call a function without param passing my_function() # call a function with some params (ordering) my_function("Tom") # call a function with a named param my_function(school="GMU") # call a function with param passing (match number of params and ordering) my_function("Ann", "VT") # call a function with named params in any order my_function(school="GMU", name="Ann") Default values Mary goes to Uva Tom goes to Uva Mary goes to GMU Ann goes to VT Ann goes to GMU CS 1110