A function is a sequence of program statements that is packaged together as a unit
Functions can be defined to perform a specific task & then the function can be called in the program wherever the task needs to be performed
Functions can accept inputs and can return outputs
Functions help us write more efficient, neater, and less repetitive code – functions are just “named” scopes!
Watch this video to understand what functions are in programming and how they compare to the mathematical functions that you're familiar with!
This video unpacks the details of functions in programming a bit more!
Check out the trinket below to follow along and try things out as you watch the video!
The code below shows an example of a very basic function. Run it and see what happens! (Hint: it should be nothing!!) Then, call the function and run it again to see it in action!
Typically, you will receive an error message if you try to call a function without passing in the correct amount of arguments, but default parameters actually allow you to call their functions without specifying the arguments for those parameters.
If a default parameter is set and no arguments are passed into the function call for those parameters, then the default value will be used.
We specify default parameters in functions by setting the parameter equal to a value in the function definition. In the example to the right, name is the default parameter!
Then, we can call the function with or without a name argument.
Run the code to observe how default parameters can be used!
TO DO: Call the function again and pass in your own name as a parameter!
One important thing to note is that all non-default (normal) parameters have to come before any default parameters! You can have multiple non-default parameters and/or multiple default parameters in a single function as long as all the non-default parameters are listed before any of the default ones!
Understand the order in which lines of code get executed when using functions!
Something else to note is that you can return more than one value from a function!
One example of this would be to return a list of values (although this would still be just returning one variable!)
def function():
return [1, 2, 3, 4, 5]
func_list = function() # func_list = [1, 2, 3, 4, 5]
Another example of this would be to return multiple different variables, like this example where we return 3 variables:
def function2():
return True, "any string", 50
status, str, num = function2() # status = True, str = "any string", num = 50
We can create our own functions in Python using the previous methods, but Python also has built-in functions.
These built in functions can be called anytime from any location in Python code because they are built in to the Python interpreter.
This means you don’t have to define the functions or import any libraries to use them.
One example of a built-in function you have seen before is type(), which returns the type of a variable. We are also going to look at the range() function.
For a full list of Python built-in functions, feel free to refer to this Python documentation!
The rand library is very important when you want to access random numbers
To do so, you must import a library by calling “import random” at the top of your code.
The rand library contains many useful functions for random number generation. Some of the most commonly used are listed below
random(): Generates a random float between 0 and 1.
randint(a, b): Generates a random integer between a and b (inclusive).
choice(seq): Selects a random element from the non-empty sequence seq.
Watch the video below for more examples of using the rand library!
Use the trinket to the right to test out the three common methods of generating random outputs that were reviewed above!
Throughout these modules, we will use both numpy and the rand library to generate random numbers. It is possible to accomplish the same tasks with these two libraries, and it's up to you to decide which method you prefer! The table below shows how to achieve some common goals with the two libraries!