CS 1111 Introduction to Programming Fall 2018

Slides:



Advertisements
Similar presentations
ThinkPython Ch. 10 CS104 Students o CS104 n Prof. Norman.
Advertisements

Lists CMSC 201. Overview Today we will learn about: For loops.
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.
Lists Introduction to Computing Science and Programming I.
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.
Lists in Python.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
COMPSCI 101 Principles of Programming Lecture 25 – Nested loops, passing mutable objects as parameters.
Oct 15, 2007Sprenkle - CS1111 Objectives Creating your own functions.
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?
Q and A for Sections 2.9, 4.1 Victor Norman CS106 Fall 2015.
Chapter 8 More On Functions. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. First cut, scope.
With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.
User Defined Functions Chapter 7 2 Chapter Topics Void Functions Without Parameters Void Functions With Parameters Reference Parameters Value and Reference.
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.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 17 Parameters, Scope, Return values.
Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined.
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?
Introduction to Computing Using Python Namespaces – Local and Global  The Purpose of Functions  Global versus Local Namespaces  The Program Stack 
An Introduction to Programming with C++ Sixth Edition Chapter 10 Void Functions.
Python Let’s get started!.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
Scope, Aliasing, Tuples & Mutability Intro2CS – week 4a 1.
Python Basics  Functions  Loops  Recursion. Built-in functions >>> type (32) >>> int(‘32’) 32  From math >>>import math >>> degrees = 45 >>> radians.
Lists in Python Lists as Arguments/Parameters. Lists as arguments to functions Just like other data types, lists can be sent to functions as arguments.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
Q and A for Sections 2.9, 4.1 Victor Norman CS106 Fall 2015.
User-Written Functions
Chapter 7: User-Defined Functions II
CS 1110 Introduction to Programming Spring 2017
Chapter 10: Void Functions
CS 1110 Introduction to Programming Spring 2017
A Lecture for the c++ Course
Variables, Expressions, and IO
Functions CIS 40 – Introduction to Programming in Python
CMSC201 Computer Science I for Majors Lecture 10 – Functions (cont)
User-Defined Functions
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
User Defined Functions
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Chapter 8 Namespaces and Memory Realities
CS 1111 Introduction to Programming Fall 2018
CS 1111 Introduction to Programming Fall 2018
Functions Pass By Value Pass by Reference
Topics Introduction to Functions Defining and Calling a Void Function
Namespaces – Local and Global
CS 1111 Introduction to Programming Spring 2019
Methods and Data Passing
Chapter 7: User-Defined Functions II
Introduction to Value-Returning Functions: Generating Random Numbers
G. Pullaiah College of Engineering and Technology
CS 1111 Introduction to Programming Spring 2019
Functions So far we've been using functions from the builtin module, which is automatically loaded in most cases. Occasionally we've accessed functions.
CISC101 Reminders Assignment 3 due today.
Global Variables Created by assignment statement placed at beginning of program and outside all functions Can be accessed by any statement in the program.
Namespaces – Local and Global
def-ining a function A function as an execution control structure
Methods and Data Passing
Presentation transcript:

CS 1111 Introduction to Programming Fall 2018 More Functions CS 1111 Introduction to Programming Fall 2018 [The Coder’s Apprentice, §5, §8-8.3] Based in part on “Agnostic Programming: Learning to Design and Test Basic Programming Algorithms” by Kinga Dobolyi, Kindle]

Print versus Return Return Print Review 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 the first return statement reached gets run If no return statement, function returns None A return ceases execution of a function and returns a value A return value is not printed, unless a function is printed

Void vs. Value-Returning Functions Review Void vs. 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) (We will talk about random module later)

Tracing through Code with Functions Review Pass by value  copy of actual value Pass by reference  copy of the memory address http://www.pythontutor.com/visualize.html

Pass by Value Passing immutable types to a function. my_value = 11 def change_a_value(some_value): print("Inside change_a_value(), some_value starts as: ", some_value) some_value *=2 print("some_value now is: ", some_value) print("Starting the program, my_value starts as: ", my_value) change_a_value(my_value) print("my_value now is still: ", my_value) Passing immutable types to a function. A copy of the variable (value and everything) is sent to the function. Changes made to the variable passed in are not reflected back where the function was called.

Pass by Reference Passing mutable types to a function. my_list = ['a', 'b', 'c', 'd'] def change_a_ref(some_list): print("Inside change_a_ref(), some_list starts as: ", some_list) some_list.append('x') print("some_list now is:", some_list) print("Starting the program, my_list starts as: ", my_list) change_a_ref(my_list) print("my_list now is:", my_list) Passing mutable types to a function. A copy of the memory address of the object, is sent to the function. Changes made to the variable passed in are reflected back where the function was called.

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

More Example: Tracing through Code with multiple function def function1(num1): num1 = 2 print(num1) return num1 def function2(num1, num2): function1(num1) print(num1 + num2) def other(num1, num2): num1 = function1(num1) print(num1) num1 = 7 function2(num1, 5) print(other(num1, 5)) function1 function2 num1 num1 7 2 7 num2 5 2 2 none 7 + 5 Main Main none num1 7 2 12

More Example: Tracing through Code with multiple function def function1(num1): num1 = 2 print(num1) return num1 def function2(num1, num2): function1(num1) print(num1 + num2) def other(num1, num2): num1 = function1(num1) print(num1) num1 = 7 function2(num1, 5) print(other(num1, 5)) function1 other num1 num1 7 2 7 2 num2 5 2 none 2 Main Main num1 7 none 2 12 2 2 none

Local and Global Variables Local variables Arguments and any variables declared in the function Cannot be seen by other functions or code Even if they have the same name as variables outside the function, the computer treats them as different (think of two people both named Tom; they are different people though they happen to be named the same) Each function call has its own memory space and variables These local data disappear when the function finishes Arguments are assigned from the function call Global variables Is accessible to all the functions in a program file

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

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

Importing Existing Functions Assuming the add() function and other functions are saved in a file called math_lib.py (= module’s name is math_lib) Import module_name or From module_name 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 import math_lib math_lib.add(2, 3) print(math_lib.add(4, 5)) print(math_lib.add(1, -1)) Put at the top of the current file from math_lib import * add(2, 3) print(add(4, 5)) print(add(1, -1))