CS 1111 Introduction to Programming Fall 2018

Slides:



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

Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Lilian Blot CORE ELEMENTS PART V: FUNCTIONS PARAMETERS & VARIABLES SCOPE Lecture 5 Autumn 2014 TPOP 1.
Lecture 04 – Classes.  Python has a number of classes built-in  lists, dictionaries, sets, int, float, boolean, strings  We can define our own classes.
Chapter 7: User-Defined Functions II
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Q and A for Chapter 3.4 – 3.14 CS 104 Victor Norman.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Variables, Functions & Parameter Passing CSci 588 Fall 2013 All material not from online sources copyright © Travis Desell, 2011.
CS0004: Introduction to Programming Variables – Strings.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 3 Simple.
Chapter 6: User-Defined Functions
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.
CPS120: Introduction to Computer Science Functions.
CPS120: Introduction to Computer Science Lecture 14 Functions.
With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
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.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
Scoping and Namespaces CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Python Let’s get started!.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
PHP Reusing Code and Writing Functions 1. Function = a self-contained module of code that: Declares a calling interface – prototype! Performs some task.
Programming for GCSE Topic 8.1: Functions T eaching L ondon C omputing William Marsh School of Electronic Engineering and Computer Science Queen Mary University.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
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.
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)
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.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Chapter 7: User-Defined Functions II
Introduction to Programming
CS 1110 Introduction to Programming Spring 2017
CS 1110 Introduction to Programming Spring 2017
Functions CIS 40 – Introduction to Programming in Python
week 1 - Introduction Goals
Using local variable without initialization is an error.
© 2016 Pearson Education, Inc.,Hoboken, NJ. All rights reserved.
Imperative Programming
Chapter 3 Simple Functions
6 Chapter Functions.
Passing Parameters by value
Chapter 8 Namespaces and Memory Realities
CS 1111 Introduction to Programming Fall 2018
Functions Pass By Value Pass by Reference
Libraries of Code Notes from Wilson, Software Design and Development Preliminary Course pp
CS2011 Introduction to Programming I Arrays (I)
More About Functions Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An.
Chapter 9: Value-Returning Functions
Introduction to Value-Returning Functions: Generating Random Numbers
G. Pullaiah College of Engineering and Technology
For this assignment, copy and past the XHTML to a notepad file with the .html extension. Then add the code I ask for to complete the problems.
Topics Introduction to Functions Defining and Calling a Function
CS 1111 Introduction to Programming Spring 2019
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Introduction to Computer Science
Global Variables Created by assignment statement placed at beginning of program and outside all functions Can be accessed by any statement in the program.
CS 1111 Introduction to Programming Spring 2019
def-ining a function A function as an execution control structure
Imperative Programming
Scope Rules.
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]

Local and Global Variables Review 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))

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 1111-Spring2018