Passing Parameters by value

Slides:



Advertisements
Similar presentations
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)
Advertisements

Q and A for Chapter 3.4 – 3.14 CS 104 Victor Norman.
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
Main task -write me a program
Functions.
Functions Part I (Syntax). What is a function? A function is a set of statements which is split off into a separate entity that can be used like a “new.
Oct 15, 2007Sprenkle - CS1111 Objectives Creating your own functions.
CPS120: Introduction to Computer Science Functions.
Chapter 8 More On Functions. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. First cut, scope.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Learners Support Publications Functions in C++
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 6 Defining Functions.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
Python Functions.
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
ProgLan Python Session 4. Functions are a convenient way to divide your code into useful blocks, allowing us to: order our code, make it more readable,
Decisions in Python Boolean functions. A Boolean function This is a function which returns a bool result (True or False). The function can certainly work.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Week 8 - Friday.  What did we talk about last time?  Static methods.
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.
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Machine Language Computer languages cannot be directly interpreted by the computer – they are not in binary. All commands need to be translated into binary.
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.
Make a function This is a starter activity and should take 5 minutes [ slide 1 ] >>> def count(number): n=1 while n
Exam #1 You will have exactly 30 Mins to complete the exam.
Topic: Recursion – Part 2
Intro to Computer Science CS1510 Dr. Sarah Diesburg
There, Their, They’re There, their and they’re are all words that sound the same, but have different meanings and are spelt differently. These are called.
Week 8 - Friday CS 121.
Function Objects and Comparators
Function There are two types of Function User Defined Function
Functions CIS 40 – Introduction to Programming in Python
Functions.
Barb Ericson Georgia Institute of Technology Dec 2009
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.
Functions Inputs Output
Writing Functions( ) (Part 5)
Call Stacks, Arguments and Returns
Log onto a computer first then ….
Object Oriented Programming in Python
CS 1111 Introduction to Programming Fall 2018
Functions as everyday items
Structured Programming Taken from notes by Dr. Neil Moore
Nate Brunelle Today: Functions again, Scope
Array and Method.
Functions Pass By Value Pass by Reference
What does this do? def revList(L): if len(L) < = 1: return L x = L[0] LR = revList(L[1:]) return LR + x.
Writing Functions( ) (Part 4)
Writing Functions( ) (Part 4)
By Andrew Horn And Ryan Kluck
Ruby Classes.
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
The structure of programming
CSE 231 Lab 4.
point when a program element is bound to a characteristic or property
Functions So far we've been using functions from the builtin module, which is automatically loaded in most cases. Occasionally we've accessed functions.
Python 12 Mr. Husch.
CSE 190p University of Washington Michael Ernst
Thinking procedurally
Nate Brunelle Today: Functions again, Scope
Exceptions for safe programming.
CMPT 120 Lecture 19 – Unit 3 – Graphics and Animation
Lecture 20 – Practice Exercises 4
Parameters and Arguments
Functions Maria Novosolov.
C Parameter Passing.
Presentation transcript:

Passing Parameters by value

Can a function change a parameter permanently? Given this little function def fun1 (a): a = 15 Does it change its parameter inside the function? Yes. After that statement, a would be 15. How long would a be 15? Until it was changed again or until the function ended Does it change the argument which was used to call the function, that matched the parameter? No, it does not.

Passing by value In Python the default way that arguments are passed to become parameters in a function is by value. That means that the function gets a copy of the argument’s value for the parameter to use. Whatever happens to the parameter in the function has NO effect on the argument in the calling code. Making permanent changes is what the return statement is for. You use the return statement to send back values to the calling code.

Why? Why discuss this? This is described in the other examples of functions Python and other languages have another way to pass arguments to parameters which does allow for a permanent change This way is called passing by reference We will see this later when we get into lists. For now, just remember that passing by value is the default way and it means that functions do NOT make permanent changes to their parameters.