Requirements:
- Linux Distribution
- python3
- A Shell Interface
My Setup:
- Debian GNU/Linux
- python3
- BASH
A function is a block of code which only runs when it is called. Functions are written when you have multiple instructions that you may want to commonly call. Rather then writing the instructions over and over, you can create a function which repeats those steps and call that when necessary. Functions can have what are known as parameters, which are variables that are to be passed in and used in any sort of way. The variables that are passed in themselves are considered arguments. Functions can also return data. To define a function,
you will use the def keyword.
def hello(): print("Hello World!")
To use this function, you would then just call hello() . In the next example I will demonstrate how to create a function with parameters, and use them :
def printInfo(name,age): print("Name: ", name) print("Age: ", age) myName = str(input("Hello! What is your name?")) myAge = int(input("And your age?")) printInfo(myName,myAge)
In this example, I use the input function again to get the data from the keyboard, and convert it into both string (for the name), and int (for the age). Since the printInfo block is a function, the python interpreter will not interpret that until it is called.
clim@debian:~/Desktop/tests/pytests/functions$ python3 func.py Hello! What is your name?Benjamin And your age?54 Name: Benjamin Age: 54 clim@debian:~/Desktop/tests/pytests/functions$
You can use the return keyword to return data from a function. For example :
def add(x,y): return x + y first = int(input("Pick the first number to add ")) second = int(input("Pick the second number to add ")) print("The value of the two is ", add(first,second))
clim@debian:~/Desktop/tests/pytests/functions$ python3 ret.py Pick the first number to add 10 Pick the second number to add 15 The value of the two is 25 clim@debian:~/Desktop/tests/pytests/functions$
If you would like to have a default value get passed into a function , you can set it within the definition like such :
def printName(name="Friend"): print("Hello " + name) printName("John") printName() printName("Becky")
clim@debian:~/Desktop/tests/pytests/functions$ python3 Python 3.7.3 (default, Jul 25 2020, 13:03:44) [GCC 8.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> def printName(name="Friend"): ... print("Hello " + name) ... >>> printName("John") Hello John >>> printName() Hello Friend >>> printName("Becky") Hello Becky >>> quit() clim@debian:~/Desktop/tests/pytests/functions$
Functions can also call themselves. This is called recursion. When you are dealing with recursion, it is important to have what is known as the “base case”, in which the recursive call exits from. Here is an example of getting a number at the “n”th index of the fibonacci sequence :
def fib(number): if number <= 1 : return number else : return(fib(number-1) + fib(number-2)) var = int(input("Pick a number to check the fibonacci number ")) print("Fibonacci number : ", fib(var))
clim@debian:~/Desktop/tests/pytests/functions$ python3 fib.py Pick a number to check the fibonacci number 10 Fibonacci number : 55
We can add to this to loop thru the whole list to that number like such:
def fib(number): if number <= 1 : return number else : return(fib(number-1) + fib(number-2)) var = int(input("Pick a number to check the fibonacci number ")) for i in range(var+1): print("Fibonacci index {} : {}".format(i, fib(i)))
clim@debian:~/Desktop/tests/pytests/functions$ python3 fib.py Pick a number to check the fibonacci number 10 Fibonacci index 0 : 0 Fibonacci index 1 : 1 Fibonacci index 2 : 1 Fibonacci index 3 : 2 Fibonacci index 4 : 3 Fibonacci index 5 : 5 Fibonacci index 6 : 8 Fibonacci index 7 : 13 Fibonacci index 8 : 21 Fibonacci index 9 : 34 Fibonacci index 10 : 55 clim@debian:~/Desktop/tests/pytests/functions$