Question

Say I had a function in Python:

def createCube(no_of_cubes):

This function creates cubes and the number of cubes it creates is set by the parameter: no_of_cubes

If i then wanted to create another function:

def moveCubes():

and I wanted the parameter no_of_cubes to be used in this function, and for the parameter to use the same integer that has been inputted for the first function. How would I do so? Or is it not possible?

Was it helpful?

Solution

A parameter is merely an argument to a function, much like you'd have in a maths function, e.g. f(x) = 2*x. Also like in maths, you can define infinite questions with the same arguments, e.g. g(x) = x^2.

The name of the parameter doesn't change anything, it's just how your function is gonna call that value. You could call it whatever you wanted, e.g. f(potato) = 2 * potato. However, there are a few broad naming conventions—in maths, you'd give preference to a lowercase roman letter for a real variable, for example. In programming, like you did, you want to give names that make sense—if it refers to the number of cubes, calling it no_of_cubes makes it easier to read your program than calling it oquhiaisnca, so kudos on that.

I'm not sure how that bit fits into your program. A few of the other answers suggested ways to do it. If it's just two loose functions (not part of a class), you can define a variable outside the functions to do what you want, like this:

1: n = 4 # number of cubes, bad variable name
2: def createCube(no_of_cubes):
3:     # things
4: def moveCubes(no_of_cubes):
5:     # different things
6: createCube(n)
7: moveCubes(n)

What happens here is that line 6 calls the function createCube and gives it n (which is 4) as a parameter. Then line 7 calls moveCubes giving it the same n as a parameter.

This is a very basic question, so I'm assuming you're new to programming. It might help a lot if you take some python tutorial. I recommend Codecademy, but there are several others you can choose from. Good luck!

OTHER TIPS

It is possible. But that two definitions get that parameter as their own one. I mean that parameter works only the definition scope. It may not be harmful for another same name parameter on different definitions.

If you cannot, probably you shouldn't do it.

If they're two separate functions (not nested or so), they should not share parameters.

If they do have connection in some way, a better way is to define a class:

class Cube:
    def __init__(self, no_of_cubes):
        self.no_of_cubes = no_of_cubes

    def create_cube(self):
        # use self.no_of_cubes
        pass

    def move_cubes(self):
        # use self.no_of_cubes
        pass

c = Cube(no_of_cubes)
c.create_cube()
c.move_cubes

Unless you know what you're doing, don't define global variable.

You can load the function moveCubes() inside createCube(). For example:

def createCube(no_of_cubes):
    # Do stuff
    moveCubes(no_of_cubes)

def moveCubes(no_of_cubes):
    # Do more stuff

Or you could define no_of_cubes out of the functions so it is accessible to all.

no_of_cubes = 5

def createCube():
    global no_of_cubes
    # Do stuff

def moveCubes():
    global no_of_cubes
    # Do stuff
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top