#🔒 Function parameters

61 messages · Page 1 of 1 (latest)

subtle wren
#

Hello guys, are function parameters necessary in python ? The majority of function I've seen so far doesn't make use of any parameters.

Because all variables defined in our main body (that is outside functions etc, are global variables so no need to pass them as arguments rights?)

Maybe when we will need to define a parameter is when a function is calling another function ?

shut wharfBOT
#

@subtle wren

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

hollow kestrel
#

it's entirely up to you how to define a function and its parameters, there's no rules. If you need a parameter, use a parameter, otherwise, don't

lethal sleet
young sigil
#

to add to that, we should use function parameters when the alternative is reassigning global variables inside a function

hollow kestrel
#

yeah, write functions assuming they are ignorant of global data

lethal sleet
hollow kestrel
#

they should be reusable in as many situations as possible

subtle wren
vapid ginkgo
#

I should add that beginner material doesn't tend to use parameters because the examples are relatively simple

subtle wren
vapid ginkgo
hollow kestrel
#

you cannot modify a global variable from a function without the global keyword

#

which should be avoided

subtle wren
#

yeah I see

vapid ginkgo
hollow kestrel
#

if anything it paints a better picture of the differences of assignment and mutation

young sigil
#

pretty much, I'm saying avoid using the global keyword. if you want to reassign a global variable, do it in global scope

# bad
a = 5
def foo():
  global a
  a -= 2
foo()
# good
a = 5
def foo(a): return a-2
a = foo(a)
lethal sleet
#

if you have some data and functions that operate on that data and change it - what you want is, perhaps, an object, or just well yeah, calculate the new value and reassign it

tender edge
#

Feeling like you need to use the global keyword to assign to the global scope is when you know you might want to use a class based approach instead.

subtle wren
#

Thanks guys, really appreciate for the knowledge shared

tender edge
#

There are valid use cases for global keyword use, but they rarely come up.

surreal cedar
#

having functions use and modify variables in the global scope looks primitive/archaic to me, it's everywhere if you read the 1993 doom source code and it makes the code hard to follow

tender edge
#

I think multiprocessing is the only time I've used it seriously.

#

Because then the child processes might have data per and need setting via the initializer.

#

So it's kind of semantically apt.

#

I mean, there are other approaches that would work, but

#

¯_(ツ)_/¯

subtle wren
#

oh huys

#

guys*

#

just a quick question

#

when do you use the "return" keyword in a function?

hollow kestrel
#

when you want to use something calculated by the function outside of the function

#
def get_rectangle_area(length, width):
#

let's say I define this function for calculating the area of a rectangle

#
def get_rectangle_area(length, width):
    return length * width
#

we can return the result

#
def get_rectangle_area(length, width):
    return length * width


area = get_rectangle_area(10, 15)
#

now area will hold the result of 10 * 15

#

!e

def get_rectangle_area(length, width):
    return length * width


area = get_rectangle_area(10, 15)
print(area)
shut wharfBOT
surreal cedar
#

return is also good for when you want to exit out of a function early because of some condition, you'll have multiple return statements in a function in that case

tender edge
#

The way I describe it is that the call of the function becomes a reference to the object given to the return keyword in the function.

#
def func():
    return 123

var = func()
# like
var = 123```
surreal cedar
#

there's a nice gif for it, let's see if we can find it

#

!return-gif

shut wharfBOT
#
Print and return

Here's a handy animation demonstrating how print and return differ in behavior.

See also: /tag return

subtle wren
#

yeah I see, okay so as a recap:

we can use return to leave a function based on some conditions
OR
We can use it whenever we need to store a value based on the function

hollow kestrel
tender edge
#

When encountered, a return statement (usually) terminates the execution of a function. If an object is provided, the call to the function evaluates to that object.

hollow kestrel
#
def duplicate_selection():
    selection = cmds.ls(sl=1)
    if not selection:
        return
    cmds.duplicate(selection)
#

This function duplicates my selection, but first I need to check if I even have a selection at all. If I don't have a selection, I tell the function to stop there

subtle wren
#

yep I see, it's like an exit command

tender edge
#

In effect.

#

"Terminate with this being the result of this function."

#

Broadly. It gets a bit more involved with generators/yield.

tough mirage
#

In general global variables are bad and should be avoided if possible

#

so yes function parameters are really important

shut wharfBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.