when should I make a function that has parameters / arguments?

Viewed 272

when should we actually create a function that has parameters / arguments?

today I made a programming project. Then it occurred to me when should I actually create a function that has parameters .. I usually create it when there is a global value / variable and that value must exist in some function then I make that value the argument of the function .. did I do it right? or wrong? if wrong what are the best practices for doing it?

varGlobal = "test"
def foo():
    print(varGlobal)

# or

def foo(parm):
    print(parm) # parm -> varGlobal


def foo():
    ask = input("ask")
    print(ask)

# or

def foo(parm):
    print(parm) # parm -> global user input
5 Answers

It's usually a good idea to use parameters. Consider what the purpose of the function is. Parameterized functions are more generally useful than non-parameterized functions.

If the first case, is whatever foo does applicable only to a single value, or could it be useful for arbitrary values, regardless of what variable might refer to them? In the former case, you are stuck using varGlobal. In the latter, the call can always use foo(varGlobal) if that's the necessary argument.

In the second, might foo be useful to someone who already has a value, and doens't need to call input? In the former case, you are stuck calling input. In the latter, the caller can always use foo(input()) or the like if they really need to call input.

I would strongly suggest that you should use parameters and arguments in every function. it simply makes the whole process of design simpler. You can clear see what data the function uses, and what it returns.

The only use of global values (either module globals, or globals imported from other modules are :

  • Module or application wide constants
  • Module or application wide function or classes (which are in Python efectively module level 'globals'.

Your functions should always return values and never change a global value (by definition if you stick to the above list that you wont be changing anything).

In my opinon using the 'global' keyword is never needed (in 8 years of coding I have never needed it, or identified a reason to use it).

Global variables can be used if you need to access or modify the variable in several methods/classes in the same module. Remember you need to point global my_global_variable to modify the variable.

Parameters are variables needed in the method to do the processing. These variables should live locally in the method. If you need to retrieve something from the method, you should add a return statement. Also, if you need to return several variables you can return as tuple.

So, in this way, you're organizing your code, making all variables visible to other people. Also I recommend you to use docstrings to fully document your methods, variables and processing.

When we need to solve the same sort of question but with different arguments. So you don't have to write the same function over and over again. Let's say you want to write a function that will return the square of the provided number as an argument. So you write

def square(num):
    return num*num

So every time you need to have square of a number..you just put that number in place of the argument and not write the whole function again.

Related