1 / 4

Passing Parameters by value

Passing Parameters by value. Can a function change a parameter permanently?. Given this little function def fun1 (a): a = 15

Download Presentation

Passing Parameters by value

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Passing Parameters by value

  2. 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 argumentwhich was used to call the function, that matched the parameter? No, it does not.

  3. 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.

  4. 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.

More Related