0

I have a function in Python, and in that same function i have multiple if statements. Lets say i want to return a value from my function. I create a local variable and change it in if statements. Problem is i need it to be defined in function so it works in every if statement, but unlike in C i cant use int a, b, c; but instead i have to give them a value. Problem is that value is going to be recognized if none of the if statements are true.

One clear solution is to take in global values as parameters of the function but i dont want to do that because i dont need their value i only want to change it.

velocity = 5
distance = 6

def calc(distance):
    # i would need to say velocity = 5 here
    distance + velocity # this does nothing but uses velocity so code cant run 
    if distance < 7 :   # without defining velocity first (in first comment)
        velocity = 6
    elif distance < 10:
        velocity = 7
    return velocity
dzi
  • 319
  • 1
  • 13
  • 2
    What's the expected outcome if none of the if statements are true? Return `None`? – Tomas Farias Aug 18 '19 at 23:02
  • 1
    This is unclear. What is the motivation of defining a global variable with the same name as a local variable? If the function needs some information about the current value of `velocity`, just make it a parameter of the function. – John Coleman Aug 18 '19 at 23:02
  • @TomasFarias The value that is in first comment. Code was actually running the way i first wrote it so i edited it now, sorry for that – dzi Aug 18 '19 at 23:11
  • In C it would be undefined behavior to declare `int a,b,c;` in the body of a function, not give `a` a value, and then have something like `return a;`. You seem to be asking how to replicate undefined behavior in Python. Sounds like an [XY problem](https://meta.stackexchange.com/q/66377/357835) at best. – John Coleman Aug 18 '19 at 23:13
  • I do not understand what this function is supposed to do – Pitto Aug 18 '19 at 23:13
  • @JohnColeman Lets say i need to use velocity later in my code but i dont need its value in function, i only need to change it in function without worrying what it was before. As for your other comment, defining a variable without giving it a value and having it returned doesnt give ant erorrs to me. – dzi Aug 18 '19 at 23:17
  • @Pitto This function is supposed to change velocity's value based on distance, without having velocity as a parameter of the function and without giving it a value outside of the if statements. If i use velocity in any equation such as my stupid `velocity + distance` and then try to give it a value it would give me an error if i dont give it a value like i did in a part i commented first. – dzi Aug 18 '19 at 23:20
  • @dzi Undefined behavior frequently "works" but is at best very bad C. In C, using declared but uninitialized variables is ub. Some compilers might give such variables a default value, but it is dangerous to rely on it. See [this question](https://stackoverflow.com/q/15268799/4996248) for a discussion. – John Coleman Aug 18 '19 at 23:20
  • @JohnColeman I appreciate the comment, i didnt go that deep into giving an example in C because i would go too far from actual problem – dzi Aug 18 '19 at 23:26
  • 2
    Why not using velocity as a parameter? If you really don't want it to be a parameter of the function then build an object that has velocity as member variable and calc as method. – Pitto Aug 18 '19 at 23:27
  • I really don't see any good reason to avoid something like `velocity = calc(distance, velocity)`. Global variables tend to make code less readable. Writing a function where the semantics of the function depends upon the state of the calling code just seems like it is a bug waiting to happen. – John Coleman Aug 18 '19 at 23:29
  • @Pitto I can but i dont need its value in a function so i tried playing around that and i think your solution is what i am actually looking for. – dzi Aug 18 '19 at 23:31
  • @dzi I am glad you liked the idea. Please remember to upvote and/or choose my answer below. Good luck and come back if you have other issues with a new question. – Pitto Aug 18 '19 at 23:45

2 Answers2

2

If you really don't want velocity to be a parameter of the function then build an object that has velocity as member variable and calc as method.

Pitto
  • 8,229
  • 3
  • 42
  • 51
1

When writing Python functions, it is a good idea to strive to follow the following design principles, which come from functional programming.

  1. A function's output should only depend on its inputs. That means that everything that influences the result should be a parameter of the function. This is generally easy to do, and makes the function self-documenting. When you write def foo(x, y, z), it is immediately obvious that the result depends on x, y and z.

  2. A function preferably should only return its output. So it should not use global to modify variables outside of its scope, nor should it modify mutable arguments. (This cannot always be achieved in practice, but it is good to strive for.)

The main advantage is that such functions can be tested and debugged apart from the rest of the application, making those tasks much easier.

In this case,

def calc(distance)

should be:

def calc(distance, velocity)
Roland Smith
  • 42,427
  • 3
  • 64
  • 94