San Jose State University Department of Applied Data Science
**DATA 200 Computational Programming for Data Analytics**
Spring 2023 Instructor: Ron Mak
"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 4.3 Functions with Multiple Parameters"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Functions with Multiple Paraemters.\n",
"#### A function can have any number of parameters, including no parameters. For example:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def maximum(value1, value2, value3):\n",
" \"\"\"Return the maximum of three values.\"\"\"\n",
" max_value = value1\n",
" \n",
" if value2 > max_value:\n",
" max_value = value2\n",
" if value3 > max_value:\n",
" max_value = value3\n",
" \n",
" return max_value"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"maximum(12, 27, 36)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"maximum(12.3, 45.6, 9.7)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"maximum('yellow', 'red', 'orange')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"maximum(13.5, -3, 7)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Local Variables\n",
"#### Inside our custom `maximum()` function, we introduced the variable `max_value`. This variable is **local** to the function. We cannot access it outside of the function:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"max_value"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### If there were a variable also named `max_value` defined outside the function, it would be a totally different variable, as if it had a different name."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Multiple Return Values\n",
"\n",
"#### A function can return more than one return value. We can optionally parenthesize the values, and they are implicitly packed into a **tuple**. After calling such a function, **unpack** the returned tuple by assigning the returned values to multiple variables at once (as many variables as there are values in the tuple)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def square_and_cube(n):\n",
" \"\"\"\n",
" Return the square and the cube of the value of parameter n.\n",
" \"\"\"\n",
" return n*n, n*n*n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x = 3.14159\n",
"square, cube = square_and_cube(x)\n",
"\n",
"print(f'The square and cube of {x} are {square:6.4f} and {cube:6.4f}')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Python’s Built-In max and min Functions"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"max('yellow', 'red', 'orange', 'blue', 'green')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"min(15, 9, 27, 14)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### **EXERCISE:** Call function `max()` with the list `[14, 27, 5, 3]` as an argument. What is the result?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### **EXERCISE:** Call function `min()` with the string `'orange'` as an argument. What is the result?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Function Terminology from Computer Science\n",
"#### In computer science literature, several terms refer to the parameters listed in parentheses in the function header, such as `value1`, `value2`, and `value3` of our custom `maximum()` function:\n",
"- parameters\n",
"- formal parameters\n",
"- formal arguments\n",
"\n",
"#### When we call a function, the arguments we list in parentheses such as `15`, `9`, `27`, and `14` after the name of a function are refered to by several terms:\n",
"- arguments\n",
"- actual arguments\n",
"- actual parameters\n",
"\n",
"#### Not to be overly pedantic and to avoid unnecessary confusion, let's simply use the term **parameters** in the function definition and **arguments** in the function call."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"##########################################################################\n",
"# (C) Copyright 2019 by Deitel & Associates, Inc. and #\n",
"# Pearson Education, Inc. All Rights Reserved. #\n",
"# #\n",
"# DISCLAIMER: The authors and publisher of this book have used their #\n",
"# best efforts in preparing the book. These efforts include the #\n",
"# development, research, and testing of the theories and programs #\n",
"# to determine their effectiveness. The authors and publisher make #\n",
"# no warranty of any kind, expressed or implied, with regard to these #\n",
"# programs or to the documentation contained in these books. The authors #\n",
"# and publisher shall not be liable in any event for incidental or #\n",
"# consequential damages in connection with, or arising out of, the #\n",
"# furnishing, performance, or use of these programs. #\n",
"##########################################################################\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Additional material (C) Copyright 2023 by Ronald Mak"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
}
},
"nbformat": 4,
"nbformat_minor": 4
}