Functions
Table of contents
Description
A function is a prewritten block of code with a name. A function is invoked by calling its name. Functions are used to eliminate repition in code. They optionally include/require:
- arguments: the function’s input; data given to the function enclosed within the parenthesis.
- return: the function’s output; data passed back to the line of code that called it.
Types
- Built-in Functions: These functions come pre-installed with Python. Common examples are
print()
,input()
,len()
, etc. - User-defined functions: These are custom functions defined by a programmer. After the programmer defines a custom function, the function can be invoked by its name.
- Imported functions: These functions are written in a module (aka python file) other than the file you are creating. An
import
statement at the top of your program allows you to use all the functions in that file (module).