Functions - Python (2016)

Python (2016)

CHAPTER 17: Functions

In the Python language, a function is a way to reuse a code block so that it is usable more than once. These functions can give the programmer the ability to minimize the redundancy of his code. It can also help steer away from wasting several hours at unnecessary work. Functions let codes have a centralized structure, where a single change in the function will affect all the other parts that depend upon it. They can also be used to split up tasks that would otherwise be very strenuous to accomplish.

Creating functions

In order to create a function, it should be defined with the def keyword. This is then followed by the name of the function, along with a blank pair of parentheses. As before, this is punctuated by a colon. A function can be part of a code block, so you will have to indent codes that are within the function. If you fail to follow indentation, you will get an “IndentationError”.

Here is an example:

Def hi():

Print(“Hello there!”)

This code serves to assign and then create the “hi” function. It would act just like the x=1 command -- in both instances, a new variable is created (hi and x).

Now that a function is created, you might be waiting for something to change—but nothing will happen yet. In order to call or invoke the function, the programmer will need to use the name of the function followed by the parentheses. This is demonstrated in the following lines of code:

Def hi():

Print(“Hello there!”)

Hello()

This should produce the following output:

Hello there!

Adding another instance of the command will result in the output coming out twice. This is just a small part of the usefulness of the function.

Parameters of Functions

Perhaps you might be wondering what is the use of the blank parentheses when calling out functions. True, typing “hello” would be two keystrokes (three, if you count Shift) shorter than “hello()”. However, these parentheses serve as the “conduit” for passing arguments to the function. This will allow the programmer to create a function that can do things that are passed onto their arguments. First off, the argument has to be declared when the function is initially defined. And then, this is called with an argument.

Def spam(x):

Print(x**2)

Spam(2)

Spam(3)

Spam(4)

The output of these lines should be:

4

9

16

The variable x acts within the spam function. You should notice that x will only exist if it is within the function. Let’s take a look at these lines of code:

Def eggs(x):

Print(x**2)

Eggs(2)

Eggs(3)

Eggs(4)

Print(x)

This will return a NameError since the variable x cannot exist by itself, outside the square.

Functions are able to take up multiple arguments, separated by commas. This is demonstrated below:

Def juice(x,y):

Print((x**2)+y)

Juice(2,3)

Juice(3,2)

Notice that this may be a great tool for those studying algebra (or related subjects). The result should be:

7

11

Default Parameters

Parameters are great, but they can also get repetitive. Instead of this, the programmer can define default values for parameters. This is done by setting the values when the parameter is first defined. Here is an example script for this:

Def bacon(x=0):

Print(x)

bacon(1)

bacon()

bacon(23)

This should produce the following output:

1

0

23

Notice that when we called bacon the second time, there was no argument given -- and yet, there was a 0 that printed out. This is a default that is assigned automatically in such cases when no arguments are given.

Returning Values

If a programmer wishes to use the value that the previous spam (or eggs) function, then he could rewrite it so that it makes use of the return command.

>>>def spam(x):

... Return x**2

...

This can be used to do some fun things. Check out the following:

>>>print(square(3))

9

>>>print(square(square(3)))

81

>>>10+square(2)

14

>>>y=square(10)

>>>y

100