Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8802017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:58:50+00:00 2026-06-14T00:58:50+00:00

In some languages you can pass a parameter by reference or value by using

  • 0

In some languages you can pass a parameter by reference or value by using a special reserved word like ref or val. When you pass a parameter to a Python function it never alters the value of the parameter on leaving the function.The only way to do this is by using the global reserved word (or as i understand it currently).

Example 1:

k = 2

def foo (n):
     n = n * n     #clarity regarding comment below
     square = n
     return square

j = foo(k)
print j
print k

would show

>>4
>>2

showing k to be unchanged.

In this example the variable n is never changed

Example 2:

n = 0
def foo():
    global n
    n = n * n
    return n

In this example the variable n is changed.

Is there any way in Python to call a function and tell Python that the parameter is either a value or reference parameter instead of using global?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T00:58:52+00:00Added an answer on June 14, 2026 at 12:58 am

    You can not change an immutable object, like str or tuple, inside a function in Python, but you can do things like:

    def foo(y):
      y[0] = y[0]**2
    
    x = [5]
    foo(x)
    print x[0]  # prints 25
    

    That is a weird way to go about it, however, unless you need to always square certain elements in an array.

    Note that in Python, you can also return more than one value, making some of the use cases for pass by reference less important:

    def foo(x, y):
       return x**2, y**2
    
    a = 2
    b = 3
    a, b = foo(a, b)  # a == 4; b == 9
    

    When you return values like that, they are being returned as a Tuple which is in turn unpacked.

    edit:
    Another way to think about this is that, while you can’t explicitly pass variables by reference in Python, you can modify the properties of objects that were passed in. In my example (and others) you can modify members of the list that was passed in. You would not, however, be able to reassign the passed in variable entirely. For instance, see the following two pieces of code look like they might do something similar, but end up with different results:

    def clear_a(x):
      x = []
    
    def clear_b(x):
      while x: x.pop()
    
    z = [1,2,3]
    clear_a(z) # z will not be changed
    clear_b(z) # z will be emptied
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think that some newer languages like JS can do this natively, but I
I want to pass a parameter to some javascript using a single line of
Most high-level languages (Python, Ruby, even Java) use pass-by reference. Obviously, we don't have
I originally thought Python was a pure pass-by-reference language. Coming from C/C++ I can't
Are computer languages copyrighted or have some restrictions imposed on them how they can
In some languages, like php, you don't need to manually initialize each dimension of
I have some code written in Python which can not be transferred to a
Possible Duplicate: Special (magic) methods in Python who can tell me what can call
In languages like Java, you can declare a class without a modifier so it
Can software be written (for some specific programming language, platform, etc.) that will inform

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.