PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, July 6, 2022

[FIXED] How can I pass "number variable" in function as reference in python?

 July 06, 2022     nested-function, pass-by-reference, python-3.x     No comments   

Issue

For better understanding here is a example code.

num = 0


def func(num):
    num += 1

func(num)
print(num)

This code prints 0 but I want 1(incremented by func()), how can I do that?

How can I do that? Is it possible?


Solution

Long story short, you can't just put number in function and expect it to be passed by reference.

In python...

  • (act like) pass by value : whole numbers, strings or tuples
  • (act like) pass by reference : python list

You should visit here for more info about "Pass(call) by Object Reference".

https://www.geeksforgeeks.org/is-python-call-by-reference-or-call-by-value/



Answered By - Eric Whale
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing