Skip to main content

8.2: Introduction to Functions

$$\newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} }$$

$$\newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}}$$

$$\newcommand{\id}{\mathrm{id}}$$ $$\newcommand{\Span}{\mathrm{span}}$$

( \newcommand{\kernel}{\mathrm{null}\,}\) $$\newcommand{\range}{\mathrm{range}\,}$$

$$\newcommand{\RealPart}{\mathrm{Re}}$$ $$\newcommand{\ImaginaryPart}{\mathrm{Im}}$$

$$\newcommand{\Argument}{\mathrm{Arg}}$$ $$\newcommand{\norm}[1]{\| #1 \|}$$

$$\newcommand{\inner}[2]{\langle #1, #2 \rangle}$$

$$\newcommand{\Span}{\mathrm{span}}$$

$$\newcommand{\id}{\mathrm{id}}$$

$$\newcommand{\Span}{\mathrm{span}}$$

$$\newcommand{\kernel}{\mathrm{null}\,}$$

$$\newcommand{\range}{\mathrm{range}\,}$$

$$\newcommand{\RealPart}{\mathrm{Re}}$$

$$\newcommand{\ImaginaryPart}{\mathrm{Im}}$$

$$\newcommand{\Argument}{\mathrm{Arg}}$$

$$\newcommand{\norm}[1]{\| #1 \|}$$

$$\newcommand{\inner}[2]{\langle #1, #2 \rangle}$$

$$\newcommand{\Span}{\mathrm{span}}$$ $$\newcommand{\AA}{\unicode[.8,0]{x212B}}$$

$$\newcommand{\vectorA}[1]{\vec{#1}} % arrow$$

$$\newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow$$

$$\newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} }$$

$$\newcommand{\vectorC}[1]{\textbf{#1}}$$

$$\newcommand{\vectorD}[1]{\overrightarrow{#1}}$$

$$\newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}}$$

$$\newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}}$$

$$\newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} }$$

$$\newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}}$$

In Python a function is a block of code that performs some task. Functions are objects of the class "function". They can be assigned to a variable, passed as arguments to other functions and returned as values from other functions. In Python  3 a function is always followed by open and closed parenthesis (), within which you pass the arguments of the parameter. You need the parenthesis, even if there are no arguments.

function_name (parameter_1, parameter_2...)

• Parameter -  variable name that function uses when it is called (molecule_name, molar_mass)
• Argument - value passed to the function (aspirin, 180.16)

There are several basic types of functions we will be using.

 Type of Function Description Built-in These are functions that are available when you load Python Imported  Standard Library Functions* These are imported from packages or libraries that come with Python but are not loaded into memory when you start it up, and so you need to import them. Imported Third Party Library Functions* These are functions from third party packages that you can download from places like PyPi.org and GitHub. Once installed, they behave like standard library functions User-defined These are functions you create. You could put them in a package and import them into another program methods Many classes of objects have methods, which are functions associated with that class.  For example a list has an append function, which adds an item to the list

* A library is a suite of python objects and can contains functions and classes.  Classes are a type of entity in python, like strings are a class, and they have attributes and methods associated with them.  One of the advantages of Python is you can create classes, or subclasses out of existing classes that are designed for specific applications. If you think about it, a number is a number, but a float, integer and string are all stored in the computer differently, and have different characteristics.

Note

Why does Python have standard library and built in functions, when they both come with the program when you install it?

The reason deals with efficiency and memory.  An analogy could be your desk and books.  Your desk is your workspace, like the memory is the computer's workspace, and if you want to read a book in the library, you first must get it, which is what the "import" statement does, and now that object is available in your workspace.  But, what if there is a book that is not in the library?  Then you need to go out and get it, and that is like the third party packages and modules. But once the book is in your library, you have access to it, and so can import it.

This page titled 8.2: Introduction to Functions is shared under a not declared license and was authored, remixed, and/or curated by Robert Belford.

• Was this article helpful?