A function carries a name that identifies it so that it can be used thereafter in the program.
Thus, when making new functions it is advisable to give functions informative names,
usually so that you instantly know what its purpose is. It is possible to define two
functions with the same name, but the one defined secondly will overwrite the first one.
Here we define a simple function named ‘sayHello’ that prints a line of text to the screen:
print('Hello world')
The ‘def’ keyword tells the Python interpreter that a function definition follows, whose
name is ‘sayHello’. Note that the brackets are a required part of the definition and that the
code block that the function contains (just a single print line in this case) is indented
relative to the ‘def’. The above just defines the function in an abstract way and nothing
will be printed when the definition is made. To actually use a function you need to invoke
it (call it) by using its name and a pair of round parentheses:
sayHello() # Prints 'Hello world'
If required, a function may be written so it accepts input, which can be any kind of
Python object. In the next example we specify a variable called ‘name’ in the brackets of
the function definition and this variable is then used by the function. Although the input
variable is referred to inside the function the variable does not represent any particular
value. It only takes a value if the function is actually used in context.
def sayHello(name):
print('Hello ' + name)
When we call (invoke) this function we specify a specific value for the input. Here we
pass in the value ‘Marge’, so the name variable takes that value and uses it to print a
message, as defined in the function.
sayHello('Marge') # Prints 'Hello Marge'
When we call the function again with a different input value we naturally get a different
message. Here we illustrate that the input value can also be passed in as a variable (text in
this case).
text = 'Homer'
sayHello(text) # Prints 'Hello Homer'
A function may also generate output that is passed back or returned to the program at
the point at which the function was called. For example, here we define a function to do a
simple calculation using input (x) to create an output (y):
def calcFunc(x):
y = 2*x*x + 4*x + 1
return y
Once the return statement is reached the operation of the function will end, and
anything on the return line will be passed back as output. Here we call the function on an
input number and catch the output value as result. You can visualise the invocation of the
function as transforming the input into the output. Notice how the names of the variables
used inside the function definition are separate from any variable names we may choose to
use when calling the function.
number = 7
result = calcFunc(number)
print(result) # 127
Strictly, all Python functions give back a return value. In cases where no specific return
statement is given, like with sayHello() above, the value passed back from the function is
implicitly the None object. This alludes to the fact that we are not obliged to catch the
output of a function as a variable; even if a function returns a value the caller can choose
to ignore it. So, for example, we could just do:
calcFunc(5)
In this case the call is rather pointless, since the only reason to use this function is to
generate the value that is passed back. But it’s possible that a function can return a value
that is not of interest in all circumstances.
A function can accept multiple input values, otherwise known as arguments. These are
separated by commas inside the brackets of the function definition. Here we define a
function that takes two arguments and performs a calculation on both, before sending back
the result.
def calcFunc(x, y):
z = x*x + y*y
return z
result = calcFunc(1.414, 2.0)
print(result) # 5.999396
Note that this function does not check that x and y are valid forms of input. For the
function to work properly we assume they are numbers. Depending on how this function is
going to be used, appropriate checks could be added.
Do'stlaringiz bilan baham: