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.

Slides:



Advertisements
Similar presentations
JAVA Revision Lecture Electronic Voting System Marina De Vos.
Advertisements

This is Java Jeopardy Writing Methods…chapter 4…
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)
CSCI 1100/ , 6.2, 6.4 April 12, 15, 17.
Inheritance Inheritance Reserved word protected Reserved word super
Lists Introduction to Computing Science and Programming I.
1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:
CSE 2501 Review Declaring a variable allocates space for the type of datum it is to store int x; // allocates space for an int int *px; // allocates space.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Functions Pass by Value Pass by Reference IC 210.
1 MATERI PENDUKUNG METHOD Matakuliah: M0074/PROGRAMMING II Tahun: 2005 Versi: 1/0.
Functions Dilshad M. Shahid New York
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010, All Rights Reserved 1.
11 Chapter 5 METHODS. 22 INTRODUCTION TO METHODS A method is a named block of statements that performs a specific task. Other languages use the terms.
Macro & Function. Function consumes more time When a function is called, the copy of the arguments are passed to the parameters in the function. After.
Lists in Python.
Lists and More About Strings CS303E: Elements of Computers and Programming.
CSE 2341 Honors Professor Mark Fontenot Southern Methodist University Note Set 04.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
DECOMPOSITION. KEY TERMS  Structured programming  Functionality  Structure Charts  Stepwise refinement  Modularity.
More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.
Chapter 6: User-Defined Functions
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
Computer Science 1MD3 Introduction to Programming
C Functions Pepper. Objectives Create functions Function prototypes Parameters – Pass by value or reference – Sending a reference Return values Math functions.
Programming Logic and Design Using Methods. 2 Objectives Review how to use a simple method with local variables and constants Create a method that requires.
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions Lecture 12.
1 Structure of a C Program (continued) Presentation original from Dr. Turner’s class USF - COP C for Engineers Summer 2008.
Pointers *, &, array similarities, functions, sizeof.
ACT476 CAPSTONE WRITING AN USER MANUAL. Developers VS Users Developers want to write code Have little time to document or write user’s manuals Users on.
ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed.
More on Variables and Subroutines. Introduction Discussion so far has dealt with self- contained subs. Subs can call other subs or functions. A module.
Starting Out With Java Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 9 Slide #1 Review.
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/11/2006 Lecture 7 – Introduction to C.
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
Data Structures & Algorithms CHAPTER 2 Arrays Ms. Manal Al-Asmari.
Strings A string is a sequence of characters that is treated as a single value. Strings are objects. We have been using strings all along. For example,
Review for Test2. Scope 8 problems, 60 points. 1 Bonus problem (5 points) Coverage: – Test 1 coverage – Exception Handling, Switch Statement – Array of.
User-Written Functions
-Neelima Singh PGT(CS) KV Sec-3 Rohini
Introduction to Programming Using C
Chapter 10: Void Functions
Chapter 3: Using Methods, Classes, and Objects
Function There are two types of Function User Defined Function
©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
User-Defined Functions
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
C Passing arrays to a Function
Object Oriented Programming COP3330 / CGS5409
METHODS AND BEHAVIORS AKEEL AHMED.
Return by Reference CSCE 121 J. Michael Moore.
Passing Parameters by value
CS 1111 Introduction to Programming Fall 2018
Array and Method.
Functions Pass By Value Pass by Reference
Why did the programmer quit his job?
Lecture 5: Functions and Parameters
Java Programming Language
Unit-1 Introduction to Java
Chapter 10: Void Functions
ㅎㅎ Fourth step for Learning C++ Programming Call by value
Announcements.
SPL – PS2 C++ Memory Handling.
Location Of The Online Examples
C Parameter Passing.
Chapter 4 Test Review First day
Presentation transcript:

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 The function must be aware of the fact that a parameter is a list, so it can treat it as a list (use subscripts) Lists are passed to functions in a different way than other arguments you have seen so far

Passed by value The process y ou have seen so far is that the values of arguments are copied into the spaces reserved for parameters The function can do anything you want with the copies The copies are deleted when the function finishes and returns This is called ‘passing arguments by value’

Passing by reference Lists are handled differently Their address is sent to the function in the place of a parameter Using the address (also called the reference) the function can change the elements of the list – in other words, the function is changing the original contents of the original list In other words, the list contents can be changed permanently

Python versus other languages In languages like C, C++ and Java, passing by reference is fairly common A main reason for this is that those languages can return no more than one value via a return statement If a function needs to return two things, it must use pass by reference to get one of the things back to the calling code

Python vs. other languages Python on the other hand can return many different values through the return statement Typically functions do not need to pass things by reference because of this fact The main thing to be aware of is that a function can change a list, which will affect the calling function’s variable. Most Python programmers would consider doing this a mistake by the programmer. If you do use passing by reference, document it WELL in the header, so that anyone using the function knows that a list argument can be changed!