{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "###
San Jose State University
Department of Applied Data Science

**DATA 200
Computational Programming for Data Analytics**

Spring 2024
Instructor: Ron Mak
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 4.2 Defining Functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### You've already used some of Python's built-in functions such as `print()`, `int()`, `float()`, `len()`, and `input()`. To distinguish a variable name from a function name, we'll put an empty set of parentheses `()` after a function name, no matter how many parameters the function has." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Defining a Custom Function" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### We can define our own user-defined custom functions. For example, here we define function `square()` which has one parameter named `number`. It returns the square of the value of `number`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def square(number):\n", " \"\"\"\n", " Calculate and return the square of number.\n", " \"\"\"\n", " return number*number" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Here are examples of how to call `square()` and display its return value:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "square(7)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "square(2.5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print('The square of 7 is', square(7))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Returning a Result to a Function’s Caller" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Use the `return` statement to immediately return a value to the function's caller. \n", "- #### If there is no expression after the `return` keyword, the function returns the value `None`.\n", "- #### If a function completes its execution without executing a `return` statement, the function also returns `None`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Specifying a Custom Function’s Docstring " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Be Pythonic! The first thing after the function header should be a triple-quoted **docstring** that explains:\n", "- #### The purpose of the function. What does it do?\n", "- #### What each of its parameters are.\n", "- #### What does it return.\n", "#### The docstring can be one or more lines." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Accessing a Function’s Docstring Via IPython’s Help Mechanism " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "square?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "square??" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### **EXAMPLE:** Define a function `square_root()` that has one parameter and returns the square root of the parameter's value. What is the square root of `6.25`?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def square_root(x):\n", " \"\"\"\n", " Return the square root of the value of x.\n", " \"\"\"\n", " return x**0.5" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "square_root(6.25)" ] }, { "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.11.5" } }, "nbformat": 4, "nbformat_minor": 4 }