{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "<small><small><i>\n", "All the IPython Notebooks in **Python Functions** lecture series by Dr. Milaan Parmar are available @ **[GitHub](https://github.com/milaan9/04_Python_Functions)**\n", "</i></small></small>" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Function Argument and Parameter\n", "\n", "The argument is a value, a variable, or an object that we pass to a function or method call. \n", "\n", "There can be two types of data passed in the function.\n", "\n", "* The First type of data is the data passed in the function call. This data is called **arguments**.\n", "\n", "* The second type of data is the data received in the function definition. This data is called **parameters**.\n", "\n", " - Arguments can be literals, variables and expressions. \n", " - Parameters must be variable to hold incoming values.\n", "\n", "Alternatively, arguments can be called as **actual parameters** or **actual arguments** and parameters can be called as **formal parameters** or **formal arguments**." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Python Function Arguments\n", "\n", "In Python, you can define a function that takes variable number of arguments. In this article, you will learn to define such functions using default, keyword and arbitrary arguments.\n", "\n", "In Python, there are 2 types of arguments allowed.\n", "<b>\n", "1. Positional Arguments (Basic)\n", "2. Variable Function Arguments\n", "</b>" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Positional Arguments (Basic)\n", "\n", "In the **[User-defined function](https://github.com/milaan9/04_Python_Functions/blob/main/Python_User_defined_Functions.ipynb)** topic, we learned about defining a function and calling it. Otherwise, the function call will result in an error. For example:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2021-10-08T17:53:46.249389Z", "start_time": "2021-10-08T17:53:46.233763Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello Arthur, Good morning!\n" ] } ], "source": [ "# Example 1:\n", "\n", "def greet(name, msg):\n", " \"\"\"This function greets to the person with the provided message\"\"\"\n", " print(\"Hello\", name + ', ' + msg)\n", "\n", "greet(\"Arthur\", \"Good morning!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Explanation:**\n", "\n", "Here, the function **`greet()`** has two parameters.\n", "\n", "Since we have called this function with two arguments, it runs smoothly and we do not get any error.\n", "\n", "If we call it with a different number of arguments, the interpreter will show an error message. Below is a call to this function with one and no arguments along with their respective error messages." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2021-10-08T17:53:46.738157Z", "start_time": "2021-10-08T17:53:46.252318Z" } }, "outputs": [ { "ename": "TypeError", "evalue": "greet() missing 1 required positional argument: 'msg'", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m<ipython-input-2-98a1ccc168e9>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mgreet\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Arthur\"\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# only one argument\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mTypeError\u001b[0m: greet() missing 1 required positional argument: 'msg'" ] } ], "source": [ "greet(\"Arthur\") # only one argument" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2021-10-08T17:53:58.135141Z", "start_time": "2021-10-08T17:53:58.121470Z" } }, "outputs": [ { "ename": "TypeError", "evalue": "greet() missing 2 required positional arguments: 'name' and 'msg'", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m<ipython-input-3-76bf7a927991>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mgreet\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# no arguments\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mTypeError\u001b[0m: greet() missing 2 required positional arguments: 'name' and 'msg'" ] } ], "source": [ "greet() # no arguments" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "ExecuteTime": { "end_time": "2021-10-08T17:54:01.795787Z", "start_time": "2021-10-08T17:54:01.785048Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "60\n", "30\n" ] } ], "source": [ "# Example 2:\n", "\n", "def add(a, b):\n", " print(a - b)\n", "\n", "add(90, 30) # Output 60\n", "add(60, 30) # Output -30" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "ExecuteTime": { "end_time": "2021-10-08T17:54:02.180554Z", "start_time": "2021-10-08T17:54:02.002333Z" } }, "outputs": [ { "ename": "TypeError", "evalue": "add() takes 2 positional arguments but 3 were given", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m<ipython-input-5-5b50fb5d02c4>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0madd\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mb\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m \u001b[1;33m-\u001b[0m \u001b[0mb\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 5\u001b[1;33m \u001b[0madd\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m109\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m633\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m9\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mTypeError\u001b[0m: add() takes 2 positional arguments but 3 were given" ] } ], "source": [ "# Example 2: If you try to use pass more parameters you will get an error.\n", "\n", "def add(a, b):\n", " print(a - b)\n", "add(109, 633, 9)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ">**Note:** In the **Positional argument** number and position of arguments must be matched. If we change the order, then the result may change. Also, If we change the number of arguments, then we will get an error." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "ExecuteTime": { "end_time": "2021-10-08T17:54:02.567760Z", "start_time": "2021-10-08T17:54:02.553115Z" }, "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "even\n", "odd\n" ] } ], "source": [ "# Example 3:\n", "\n", "def evenodd(x): # x is parameters or formal parameters or formal arguments.\n", " if (x % 2 == 0):\n", " print(\"even\")\n", " else:\n", " print(\"odd\")\n", "\n", "# Driver code\n", "evenodd(6) # here 2 is argument or actual perameter\n", "evenodd(9) # here 3 is argument or actual perameter" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "ExecuteTime": { "end_time": "2021-10-08T17:54:02.829482Z", "start_time": "2021-10-08T17:54:02.808974Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "8\n" ] } ], "source": [ "# Example 4:\n", "\n", "def cube(x):\n", " \"This x a passed num value into this function, return cube of x\"\n", " y=x*x*x;\n", " return y\n", "\n", "# Now you can call cube function\n", "z=cube(2) #required to pass argument\n", "print(z)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Variable Function Arguments\n", "\n", "Up until now, functions had a fixed number of arguments. In Python, there are other ways to define a function that can take variable number of arguments.\n", "\n", "Three different forms of this type are described below:\n", "<b>\n", "1. Default Arguments\n", "2. Keyword Arguments\n", "3. Arbitrary/Variable-length Arguments\n", "</b>" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Python Default Arguments\n", "\n", "Default arguments are arguments that take the default value during the function call. If we do not pass any argument to the function, then the default argument will take place. We can assign default values using the **`=`** assignment operator. For example:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "ExecuteTime": { "end_time": "2021-10-08T17:54:03.695692Z", "start_time": "2021-10-08T17:54:03.679093Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello Alan, Good morning!\n", "Hello Bruce, How do you do?\n", "Hello Carson, Good night!\n" ] } ], "source": [ "# Example 1:\n", "\n", "def greet(name, msg=\"Good morning!\"): # two arguments: `name` is fixed arg and 'msg' is variable arg\n", "\n", " print(\"Hello\", name + ', ' + msg)\n", "\n", "\n", "greet(\"Alan\")\n", "greet(\"Bruce\", \"How do you do?\")\n", "greet(\"Carson\",\"Good night!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Explanation:**\n", "\n", "In this function, the parameter **`name`** does not have a default value and is required (mandatory) during a call.\n", "\n", "On the other hand, the parameter **`msg`** has a default value of **`\"Good morning!\"`**. So, it is optional during a call. If a value is provided, it will overwrite the default value.\n", "\n", "Any number of arguments in a function can have a default value. But once we have a default argument, all the arguments to its right must also have default values.\n", "\n", "This means to say, non-default arguments cannot follow default arguments. For example, if we had defined the function header above as:\n", "\n", "```python\n", ">>> def greet(msg = \"Good morning!\", name):\n", "```\n", "\n", "We would get an error as:\n", "```python\n", "SyntaxError: non-default argument follows default argument\n", "```" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "ExecuteTime": { "end_time": "2021-10-08T17:54:04.298283Z", "start_time": "2021-10-08T17:54:04.279681Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello David\n", "Hello Everyone\n" ] } ], "source": [ "# Example 2: function with default argument\n", "\n", "def message(name=\"Everyone\"):\n", " print(\"Hello\", name)\n", "\n", "# calling function with argument\n", "message(\"David\")\n", "\n", "# calling function without argument\n", "message()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Explanation:** \n", "\n", "When we call a function with an argument, it will be considered that value.\n", "\n", "Like in the above example, we passed **`name=\"David\"`** we pass to the function, then the function will consider that value. If we do not pass any argument, it will be considered **`\"name=Everyone\"`** as a default value." ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "ExecuteTime": { "end_time": "2021-10-08T17:54:05.968643Z", "start_time": "2021-10-08T17:54:05.951068Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Details of: Bill\n", "Empoyee Id : 101\n", "Age : 21\n", "Company : Samsung.com\n", "-----------------------\n", "Details of: Cory\n", "Empoyee Id : 102\n", "Age : 22\n", "Company : Baidu.com\n" ] } ], "source": [ "# Example 3: it prints default age if it is not passed\n", "\n", "def emp_data(name, emp_id, age, company=\"Baidu.com\"): # total 4 arguments (3 is fixed and 1 is variable)\n", " print(\"Details of: \",name)\n", " print(\"Empoyee Id : \",emp_id)\n", " print(\"Age : \",age)\n", " print(\"Company : \",company)\n", "\n", "#call emp_data fun\n", "emp_data(\"Bill\",101,21,\"Samsung.com\")\n", "print(\"-----------------------\")\n", "emp_data(\"Cory\",102,22)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Python Keyword Arguments\n", "\n", "Keyword arguments are related to the function calls. A keyword argument is an argument value, passed to function preceded by the variable name and an equals sign.\n", "\n", "This allows you to skip arguments or place them out of order because the Python interpreter is able to use the keywords provided to match the values with parameters." ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "ExecuteTime": { "end_time": "2021-10-08T17:54:06.818254Z", "start_time": "2021-10-08T17:54:06.799703Z" }, "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello Eric, How do you do?\n", "Hello Eric, How do you do?\n", "Hello Eric, How do you do?\n", "Hello Eric, How do you do?\n" ] } ], "source": [ "# Example 1:\n", "\n", "greet(\"Eric\", \"How do you do?\")\n", "\n", "# 2 keyword arguments\n", "greet(name = \"Eric\",msg = \"How do you do?\")\n", "\n", "# 2 keyword arguments (out of order)\n", "greet(msg = \"How do you do?\",name = \"Eric\") \n", "\n", "# 1 positional, 1 keyword argument\n", "greet(\"Eric\", msg = \"How do you do?\") " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As we can see, we can mix positional arguments with keyword arguments during a function call. But we must keep in mind that keyword arguments must follow positional arguments.\n", "\n", "Having a positional argument after keyword arguments will result in errors. For example, the function call as follows:" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "ExecuteTime": { "end_time": "2021-10-08T17:54:07.312883Z", "start_time": "2021-10-08T17:54:07.298727Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello Eric, How do you do?\n" ] } ], "source": [ "greet(\"Eric\",\"How do you do?\")" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "ExecuteTime": { "end_time": "2021-10-08T17:54:07.575577Z", "start_time": "2021-10-08T17:54:07.569232Z" }, "scrolled": true }, "outputs": [ { "ename": "SyntaxError", "evalue": "positional argument follows keyword argument (<ipython-input-13-0d17a834dca2>, line 1)", "output_type": "error", "traceback": [ "\u001b[1;36m File \u001b[1;32m\"<ipython-input-13-0d17a834dca2>\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m greet(name=\"Eric\",\"How do you do?\") # Will result in an error\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m positional argument follows keyword argument\n" ] } ], "source": [ "greet(name=\"Eric\",\"How do you do?\") # Will result in an error" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "ExecuteTime": { "end_time": "2021-10-08T17:54:07.837788Z", "start_time": "2021-10-08T17:54:07.826073Z" }, "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Milaan Parmar\n" ] } ], "source": [ "# Example 2:\n", "\n", "def print_fullname(firstname, lastname):\n", " space = ' '\n", " full_name = firstname + space + lastname\n", " return full_name\n", "print(print_fullname(firstname = 'Milaan', lastname = 'Parmar'))" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "ExecuteTime": { "end_time": "2021-10-08T17:54:08.116107Z", "start_time": "2021-10-08T17:54:08.109271Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n" ] } ], "source": [ "# Example 3:\n", "\n", "def add_two_numbers (num1, num2):\n", " total = num1 + num2\n", " return total\n", "print(add_two_numbers(num2 = 3, num1 = 2)) # Order does not matter" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "ExecuteTime": { "end_time": "2021-10-08T17:54:08.454973Z", "start_time": "2021-10-08T17:54:08.439351Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Age: 200\n" ] } ], "source": [ "# Example 4:\n", "\n", "def calculate_age (current_year, birth_year):\n", " age = current_year - birth_year\n", " return age;\n", "print('Age: ', calculate_age(2019, 1819))" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "ExecuteTime": { "end_time": "2021-10-08T17:54:08.733298Z", "start_time": "2021-10-08T17:54:08.724510Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "remainder of 10/3 : 1\n" ] } ], "source": [ "# Example 5:\n", "\n", "def remainder(dividend,divisor):\n", " x=dividend%divisor\n", " return x\n", "\n", "#rem = remainder(10,3)\n", "rem = remainder(divisor = 3, dividend = 10) # keyword argument\n", "print(\"remainder of 10/3 : \",rem)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "ExecuteTime": { "end_time": "2021-10-08T17:54:09.011615Z", "start_time": "2021-10-08T17:54:08.992087Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello Frank Harper\n", "Hello Gretta Clayton\n" ] } ], "source": [ "# Example 6:\n", "\n", "def message(name, surname):\n", " print(\"Hello\", name, surname)\n", "\n", "message(name=\"Frank\", surname=\"Harper\")\n", "message(surname=\"Clayton\", name=\"Gretta\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In keyword arguments order of argument is not matter, but the number of arguments must match. Otherwise, we will get an error." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "While using keyword and positional argument simultaneously, we need to pass 1st arguments as positional arguments and then keyword arguments. Otherwise, we will get **`SyntaxError`**. See the following example." ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "ExecuteTime": { "end_time": "2021-10-08T17:54:09.751364Z", "start_time": "2021-10-08T17:54:09.743554Z" } }, "outputs": [ { "ename": "SyntaxError", "evalue": "positional argument follows keyword argument (<ipython-input-19-b8ab82a1062c>, line 10)", "output_type": "error", "traceback": [ "\u001b[1;36m File \u001b[1;32m\"<ipython-input-19-b8ab82a1062c>\"\u001b[1;36m, line \u001b[1;32m10\u001b[0m\n\u001b[1;33m message(first_nm=\"Frank\", \"Harper\") # SyntaxError: positional argument follows keyword argument\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m positional argument follows keyword argument\n" ] } ], "source": [ "# Example 7:\n", "\n", "def message(first_nm, last_nm):\n", " print(\"Hello..!\", first_nm, last_nm)\n", "\n", "# correct use\n", "message(\"Frank\", \"Harper\")\n", "message(\"Frank\", last_nm=\"Harper\")\n", "\n", "message(first_nm=\"Frank\", \"Harper\") # SyntaxError: positional argument follows keyword argument" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Python Arbitrary/Variable-length Arguments\n", "\n", "Sometimes, we do not know in advance the number of arguments that will be passed into a function. Python allows us to handle this kind of situation through function calls with an arbitrary number of arguments.\n", "\n", "We can pass any number of arguments to this function. Internally all these values are represented in the form of a **tuple**.\n", "\n", "In the function definition, we use an asterisk **`*`** before the parameter name to denote this kind of argument. For example:" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "ExecuteTime": { "end_time": "2021-10-08T17:54:10.415429Z", "start_time": "2021-10-08T17:54:10.407618Z" }, "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello Gary\n", "Hello Hank\n", "Hello Ivan\n", "Hello John\n" ] } ], "source": [ "# Example 1: \n", "\n", "def greet(*names):\n", " \"\"\"This function greets all the person in the names tuple.\"\"\"\n", " # names is a tuple with arguments\n", " for name in names:\n", " print(\"Hello\", name)\n", "\n", "greet(\"Gary\", \"Hank\", \"Ivan\", \"John\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here, we have called the function with multiple arguments. These arguments get wrapped up into a tuple before being passed into the function. Inside the function, we use a **`for`** loop to retrieve all the arguments back." ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "ExecuteTime": { "end_time": "2021-10-08T17:54:11.604882Z", "start_time": "2021-10-08T17:54:11.592189Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Sum is: 0\n", "Sum is: 33\n", "Sum is: 176.6\n" ] } ], "source": [ "# Example 2: \n", "\n", "def addition(*numbers):\n", " total = 0\n", " for no in numbers:\n", " total = total + no\n", " print(\"Sum is:\", total)\n", "\n", "addition() # 0 arguments\n", "addition(10, 5, 3, 6, 9) # 5 arguments\n", "addition(96, 77, 3.6) # 3 arguments" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "ExecuteTime": { "end_time": "2021-10-08T17:54:12.315821Z", "start_time": "2021-10-08T17:54:12.304103Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10\n" ] } ], "source": [ "# Example 3: \n", "\n", "def sum_all_nums(*nums):\n", " total = 0\n", " for num in nums:\n", " total += num # same as total = total + num \n", " return total\n", "print(sum_all_nums(2, 3, 5)) # 10" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Default and Arbitrary Number of Parameters in Functions" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "ExecuteTime": { "end_time": "2021-10-08T17:54:13.377345Z", "start_time": "2021-10-08T17:54:13.359769Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Output is: \n", "10\n", "Output is: \n", "70\n", "40\n", "20\n", "Output is: \n", "30\n", "60\n", "90\n", "120\n" ] } ], "source": [ "# Example 1: \n", "\n", "def printinfo( arg1, *vartuple ):\n", " \"This prints a variable passed arguments\"\n", " print (\"Output is: \")\n", " print (arg1)\n", " for var in vartuple:\n", " print (var)\n", " return\n", "\n", " # Now you can call printinfo function\n", "printinfo( 10 ) # We have given only one value for the argument\n", "printinfo( 70, 40, 20 )\n", "printinfo( 30, 60, 90, 120 )" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "ExecuteTime": { "end_time": "2021-10-08T17:54:14.148826Z", "start_time": "2021-10-08T17:54:14.127348Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Team-1\n", "Milaan\n", "Arthur\n", "Clark\n", "Ethan\n", "None\n" ] } ], "source": [ "# Example 2: \n", "\n", "def generate_groups (team,*args):\n", " print(team)\n", " for i in args:\n", " print(i)\n", "print(generate_groups('Team-1','Milaan','Arthur','Clark','Ethan'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Function as a Parameter of Another Function" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "ExecuteTime": { "end_time": "2021-10-08T17:54:15.540920Z", "start_time": "2021-10-08T17:54:15.520416Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "9\n" ] } ], "source": [ "# Example 1: You can pass functions around as parameters\n", "\n", "def square_number (n):\n", " return n * n\n", "def do_something(f, x):\n", " return f(x)\n", "print(do_something(square_number, 3)) # 27" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "hide_input": false, "kernelspec": { "display_name": "Python 3", "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.8.8" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false }, "varInspector": { "cols": { "lenName": 16, "lenType": 16, "lenVar": 40 }, "kernels_config": { "python": { "delete_cmd_postfix": "", "delete_cmd_prefix": "del ", "library": "var_list.py", "varRefreshCmd": "print(var_dic_list())" }, "r": { "delete_cmd_postfix": ") ", "delete_cmd_prefix": "rm(", "library": "var_list.r", "varRefreshCmd": "cat(var_dic_list()) " } }, "types_to_exclude": [ "module", "function", "builtin_function_or_method", "instance", "_Feature" ], "window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }