{"id":187,"date":"2019-02-07T01:55:11","date_gmt":"2019-02-07T01:55:11","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/python\/?p=187"},"modified":"2019-03-02T03:55:07","modified_gmt":"2019-03-02T03:55:07","slug":"15-python-functions","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/python\/15-python-functions\/","title":{"rendered":"Python &#8211; Functions"},"content":{"rendered":"<p>A function is a block of code that is executed as a unit. While there are many inbuilt functions in Python, you can also create your own function. This is known as user-defined functions. Once created, you can reuse a function at other parts of your program. Before you can use a function however, you must define it.<\/p>\n<p>We cover the following:<\/p>\n<ol>\n<li><a href=\"#t1\">Defining a Function<\/a><\/li>\n<li><a href=\"#t2\">Calling a Function<\/a><\/li>\n<li><a href=\"#t3\">Pass by Value and by Reference<\/a><\/li>\n<li><a href=\"#t4\">Arguments to Functions<\/a><\/li>\n<li><a href=\"#t5\">Required Arguments<\/a><\/li>\n<li><a href=\"#t6\">Keyword Arguments<\/a><\/li>\n<li><a href=\"#t7\">Default Arguments<\/a><\/li>\n<li><a href=\"#t8\">Required Arguments<\/a><\/li>\n<li><a href=\"#t9\">Global and Local Variables<\/a><\/li>\n<li><a href=\"https:\/\/www.youtube.com\/watch?v=ZDO7a5qRqUM\">Watch the Video<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. Defining a Function<\/strong><\/h4>\n<p>To define a function, you must follow the rules listed below:<\/p>\n<ul>\n<li>Function definition begin with the keyword def, followed\u00a0 by the function name, then open and close parentheses.<\/li>\n<li>Input parameters to the function are placed inside the braces<\/li>\n<li>After the braces, then you must add colon (:) before you start writing the function body<\/li>\n<li>The function body must be indented<\/li>\n<li>The last statement in a function is optionally a return statement.<\/li>\n<\/ul>\n<p>For example, the function below calculates the sum of two numbers<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\"># Function to calculate the sum of two number<\/span>\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">def<\/span> <span style=\"color: #0066bb; font-weight: bold;\">calculatesum<\/span>(a, b):\r\n    <span style=\"color: #007020;\">sum<\/span> <span style=\"color: #333333;\">=<\/span> a <span style=\"color: #333333;\">+<\/span> b\r\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #007020;\">sum<\/span>\r\n<\/pre>\n<p>After creating a function, you can use it by calling it.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Calling a Function<\/strong><\/h4>\n<p>To use a function, you must call it. If it requires parameters, then you must also provide the parameters. For example we can call the calculatesum function using the code below.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\"># Function to calculate the sum of two number<\/span>\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">def<\/span> <span style=\"color: #0066bb; font-weight: bold;\">calculatesum<\/span>(a, b):\r\n    <span style=\"color: #007020;\">sum<\/span> <span style=\"color: #333333;\">=<\/span> a <span style=\"color: #333333;\">+<\/span> b\r\n    <span style=\"color: #007020;\">print<\/span>(<span style=\"background-color: #fff0f0;\">\"The sum of is \"<\/span> <span style=\"color: #333333;\">+<\/span> <span style=\"color: #007020;\">str<\/span>(<span style=\"color: #007020;\">sum<\/span>))\r\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #007020;\">sum<\/span>\r\n\r\n<span style=\"color: #888888;\"># Calling the function calculatesum<\/span>\r\ncalculatesum(<span style=\"color: #0000dd; font-weight: bold;\">4<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">6<\/span>)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>So it is only when a function it called that the program executes the body of the function. One thing to note is that the function call must appear after the function definition (not before!)<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Pass by Value and by Reference<\/strong><\/h4>\n<p>In Python, all arguments to a function are passed by reference. This means that the the same variable used in defining a function is reference when calling. So if you change the reference of a parameter within a function, this change is reflected in the calling function.<\/p>\n<p>For example,<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">def<\/span> <span style=\"color: #0066bb; font-weight: bold;\">changelist<\/span>(list1):\r\n    list1<span style=\"color: #333333;\">.<\/span>append([<span style=\"color: #0000dd; font-weight: bold;\">10<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">20<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">30<\/span>])\r\n    <span style=\"color: #007020;\">print<\/span>(<span style=\"background-color: #fff0f0;\">\"Changes made Inside the function: \"<\/span> <span style=\"color: #333333;\">+<\/span> <span style=\"color: #007020;\">str<\/span>(list1))\r\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span>\r\n\r\n\r\nmylist <span style=\"color: #333333;\">=<\/span> [<span style=\"color: #0000dd; font-weight: bold;\">1<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">2<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">3<\/span>]\r\nchangelist(mylist)\r\n<span style=\"color: #007020;\">print<\/span>(<span style=\"background-color: #fff0f0;\">\"Changes reflect in the calling function: \"<\/span> <span style=\"color: #333333;\">+<\/span> <span style=\"color: #007020;\">str<\/span>(mylist))\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>In the code above, when the function is called, the argument <em>mylist<\/em> is passed by reference. So inside the function, the same <em>mylist<\/em> is used. So the changes are made to the same <em>mylist<\/em> object.<\/p>\n<p>The output of the code above would be:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">Changes made Inside the function: [<span style=\"color: #0000dd; font-weight: bold;\">1<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">2<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">3<\/span>, [<span style=\"color: #0000dd; font-weight: bold;\">10<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">20<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">30<\/span>]]\r\nChanges reflect <span style=\"color: #000000; font-weight: bold;\">in<\/span> the calling function: [<span style=\"color: #0000dd; font-weight: bold;\">1<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">2<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">3<\/span>, [<span style=\"color: #0000dd; font-weight: bold;\">10<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">20<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">30<\/span>]]<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t4\">4. Arguments to Functions<\/strong><\/h4>\n<p>You can pass arguments to a function by one of the following types of arguments:<\/p>\n<ul>\n<li>Required arguments<\/li>\n<li>Keyword arguments<\/li>\n<li>Default arguments<\/li>\n<li>Variable-length arguments<\/li>\n<\/ul>\n<p>Let&#8217;s now look at each one of these<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t5\">5. Required arguments<\/strong><\/h4>\n<p>This are arguments you must pass to a function, otherwise you get an error. When calling a function <em>foo(a, b, c)<\/em> for example, then you must provide the three arguments. Also they must match the correct position of each of parameters in the function definition.<\/p>\n<p>Let&#8217;s take an example, consider the <em>printdata<\/em> function below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\"># A function that just prints out the parameter<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">def<\/span> <span style=\"color: #0066bb; font-weight: bold;\">printdata<\/span>(data):\r\n    <span style=\"color: #007020;\">print<\/span>(data)\r\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span>\r\n<\/pre>\n<p>If you are going to call this function, then you better provide the argument, data. Otherwise you get an error. This is required argument<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t6\">6. Keyword Arguments<\/strong><\/h4>\n<p>In this case, when you call a function, you use name of the parameter to identify the argument being passed.<\/p>\n<p>This means that you can either skip some argument or even place he argument out of other when calling. But the keywords have to match the name of the parameters used in the function definition. Take for example, the function below that takes name and age, then prints them out.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">def<\/span> <span style=\"color: #0066bb; font-weight: bold;\">nameandage<\/span>(name, age):\r\n    <span style=\"color: #007020;\">print<\/span>(<span style=\"background-color: #fff0f0;\">\"Your name is \"<\/span> <span style=\"color: #333333;\">+<\/span> name)\r\n    <span style=\"color: #007020;\">print<\/span>(<span style=\"background-color: #fff0f0;\">\"Your age is \"<\/span> <span style=\"color: #333333;\">+<\/span> <span style=\"color: #007020;\">str<\/span>(age))\r\n\r\n\r\n<span style=\"color: #888888;\"># Calling the function using keyword parameters<\/span>\r\nnameandage(name <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"Jadon\"<\/span>, age <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">12<\/span>)\r\n\r\n<span style=\"color: #888888;\">#this is the same as<\/span>\r\nnameandage( age <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">12<\/span>, name <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"Jadon\"<\/span>)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4 id=\"t7\">7. Default Arguments<\/h4>\n<p>In this case you may fail to provide a value for the argument during the function call. Then the argument just assumes a default value.<\/p>\n<p>Take for example, the function below. In this case, a default argument is provided at the function definition. So if you omit this argument, the default is used.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\"># default argument specified for age<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">def<\/span> <span style=\"color: #0066bb; font-weight: bold;\">nameandage<\/span>(name, age <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">13<\/span>):\r\n    <span style=\"color: #007020;\">print<\/span>(<span style=\"background-color: #fff0f0;\">\"Your name is \"<\/span> <span style=\"color: #333333;\">+<\/span> name)\r\n    <span style=\"color: #007020;\">print<\/span>(<span style=\"background-color: #fff0f0;\">\"Your age is \"<\/span> <span style=\"color: #333333;\">+<\/span> <span style=\"color: #007020;\">str<\/span>(age))\r\n\r\n\r\n<span style=\"color: #888888;\"># Calling the function without providing argument for age<\/span>\r\nnameandage(name <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"Jadon\"<\/span>)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t8\">8. Variable-length arguments<\/strong><\/h4>\n<p>This is a situation where you need to provide more arguments than are used during the function definition. To create a variable-length argument, you use the * symbol before the name of the argument. Let&#8217;s take an example of a name and a list of scores. While the name is provided, but we would not know how many items are in the scores<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">def<\/span> <span style=\"color: #0066bb; font-weight: bold;\">printdata<\/span>(name, <span style=\"color: #333333;\">*<\/span>scores):\r\n    <span style=\"color: #007020;\">print<\/span>(<span style=\"background-color: #fff0f0;\">\"Student name: \"<\/span> <span style=\"color: #333333;\">+<\/span> name)\r\n    <span style=\"color: #007020;\">print<\/span>(<span style=\"background-color: #fff0f0;\">\"Scores are given below\"<\/span>)\r\n    <span style=\"color: #008800; font-weight: bold;\">for<\/span> score <span style=\"color: #000000; font-weight: bold;\">in<\/span> scores:\r\n        <span style=\"color: #007020;\">print<\/span>(score)\r\n\r\n\r\nscores <span style=\"color: #333333;\">=<\/span> [<span style=\"color: #0000dd; font-weight: bold;\">89<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">84<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">78<\/span>, <span style=\"color: #0000dd; font-weight: bold;\">90<\/span>]\r\n\r\n<span style=\"color: #888888;\">#the variable-length argument is provided<\/span>\r\nprintdata(<span style=\"background-color: #fff0f0;\">\"Solace\"<\/span>, scores) \r\n\r\n<span style=\"color: #888888;\"># the variable-length argument is omitted<\/span>\r\nprintdata(<span style=\"background-color: #fff0f0;\">\"Solace\"<\/span>) \r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>You can see that the variable-length argument can either be provided or omitted. When provided, it is used. But if it is omitted, then it is ignored. Try to run the above code to see the output.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t9\">9. Global and Local Variables<\/strong><\/h4>\n<p>Variables that are defined inside the body of a function are local variables. Therefore, they are available only inside the function block that defines it.<\/p>\n<p>However, variables variables defined outside a function are global variables. This means they are available everywhere in the program.<br \/>\n&nbsp;<\/p>\n<p><iframe loading=\"lazy\" width=\"100%\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/ZDO7a5qRqUM\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A function is a block of code that is executed as a unit. While there are many inbuilt functions in Python, you can also create &hellip; <\/p>\n","protected":false},"author":395,"featured_media":188,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[5],"tags":[13],"class_list":["post-187","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorials","tag-python-functions"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python - Functions - Python Tutorials<\/title>\n<meta name=\"description\" content=\"You learn how to create and call functions in Python. You learn about pass by reference and by value and different types of arguments\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.kindsonthegenius.com\/python\/15-python-functions\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python - Functions - Python Tutorials\" \/>\n<meta property=\"og:description\" content=\"You learn how to create and call functions in Python. You learn about pass by reference and by value and different types of arguments\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.kindsonthegenius.com\/python\/15-python-functions\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Tutorials\" \/>\n<meta property=\"article:published_time\" content=\"2019-02-07T01:55:11+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-03-02T03:55:07+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.kindsonthegenius.com\/python\/wp-content\/uploads\/sites\/8\/2019\/02\/Functions-in-Python2.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1005\" \/>\n\t<meta property=\"og:image:height\" content=\"574\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"kindsonthegenius\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"kindsonthegenius\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/15-python-functions\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/15-python-functions\\\/\"},\"author\":{\"name\":\"kindsonthegenius\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/#\\\/schema\\\/person\\\/63a68934672db675ff0cd80d066510c2\"},\"headline\":\"Python &#8211; Functions\",\"datePublished\":\"2019-02-07T01:55:11+00:00\",\"dateModified\":\"2019-03-02T03:55:07+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/15-python-functions\\\/\"},\"wordCount\":771,\"commentCount\":1,\"image\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/15-python-functions\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/wp-content\\\/uploads\\\/sites\\\/8\\\/2019\\\/02\\\/Functions-in-Python2.jpg\",\"keywords\":[\"Python Functions\"],\"articleSection\":[\"Python Tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/15-python-functions\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/15-python-functions\\\/\",\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/15-python-functions\\\/\",\"name\":\"Python - Functions - Python Tutorials\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/15-python-functions\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/15-python-functions\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/wp-content\\\/uploads\\\/sites\\\/8\\\/2019\\\/02\\\/Functions-in-Python2.jpg\",\"datePublished\":\"2019-02-07T01:55:11+00:00\",\"dateModified\":\"2019-03-02T03:55:07+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/#\\\/schema\\\/person\\\/63a68934672db675ff0cd80d066510c2\"},\"description\":\"You learn how to create and call functions in Python. You learn about pass by reference and by value and different types of arguments\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/15-python-functions\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/15-python-functions\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/15-python-functions\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/wp-content\\\/uploads\\\/sites\\\/8\\\/2019\\\/02\\\/Functions-in-Python2.jpg\",\"contentUrl\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/wp-content\\\/uploads\\\/sites\\\/8\\\/2019\\\/02\\\/Functions-in-Python2.jpg\",\"width\":1005,\"height\":574,\"caption\":\"Functions in Python\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/15-python-functions\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python &#8211; Functions\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/#website\",\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/\",\"name\":\"Python Tutorials\",\"description\":\"Python Tutorial for Programming and Data Science\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/#\\\/schema\\\/person\\\/63a68934672db675ff0cd80d066510c2\",\"name\":\"kindsonthegenius\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g\",\"caption\":\"kindsonthegenius\"},\"url\":\"https:\\\/\\\/www.kindsonthegenius.com\\\/python\\\/author\\\/kindsonthegenius-2\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python - Functions - Python Tutorials","description":"You learn how to create and call functions in Python. You learn about pass by reference and by value and different types of arguments","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.kindsonthegenius.com\/python\/15-python-functions\/","og_locale":"en_US","og_type":"article","og_title":"Python - Functions - Python Tutorials","og_description":"You learn how to create and call functions in Python. You learn about pass by reference and by value and different types of arguments","og_url":"https:\/\/www.kindsonthegenius.com\/python\/15-python-functions\/","og_site_name":"Python Tutorials","article_published_time":"2019-02-07T01:55:11+00:00","article_modified_time":"2019-03-02T03:55:07+00:00","og_image":[{"width":1005,"height":574,"url":"https:\/\/www.kindsonthegenius.com\/python\/wp-content\/uploads\/sites\/8\/2019\/02\/Functions-in-Python2.jpg","type":"image\/jpeg"}],"author":"kindsonthegenius","twitter_card":"summary_large_image","twitter_misc":{"Written by":"kindsonthegenius","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.kindsonthegenius.com\/python\/15-python-functions\/#article","isPartOf":{"@id":"https:\/\/www.kindsonthegenius.com\/python\/15-python-functions\/"},"author":{"name":"kindsonthegenius","@id":"https:\/\/www.kindsonthegenius.com\/python\/#\/schema\/person\/63a68934672db675ff0cd80d066510c2"},"headline":"Python &#8211; Functions","datePublished":"2019-02-07T01:55:11+00:00","dateModified":"2019-03-02T03:55:07+00:00","mainEntityOfPage":{"@id":"https:\/\/www.kindsonthegenius.com\/python\/15-python-functions\/"},"wordCount":771,"commentCount":1,"image":{"@id":"https:\/\/www.kindsonthegenius.com\/python\/15-python-functions\/#primaryimage"},"thumbnailUrl":"https:\/\/www.kindsonthegenius.com\/python\/wp-content\/uploads\/sites\/8\/2019\/02\/Functions-in-Python2.jpg","keywords":["Python Functions"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.kindsonthegenius.com\/python\/15-python-functions\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.kindsonthegenius.com\/python\/15-python-functions\/","url":"https:\/\/www.kindsonthegenius.com\/python\/15-python-functions\/","name":"Python - Functions - Python Tutorials","isPartOf":{"@id":"https:\/\/www.kindsonthegenius.com\/python\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.kindsonthegenius.com\/python\/15-python-functions\/#primaryimage"},"image":{"@id":"https:\/\/www.kindsonthegenius.com\/python\/15-python-functions\/#primaryimage"},"thumbnailUrl":"https:\/\/www.kindsonthegenius.com\/python\/wp-content\/uploads\/sites\/8\/2019\/02\/Functions-in-Python2.jpg","datePublished":"2019-02-07T01:55:11+00:00","dateModified":"2019-03-02T03:55:07+00:00","author":{"@id":"https:\/\/www.kindsonthegenius.com\/python\/#\/schema\/person\/63a68934672db675ff0cd80d066510c2"},"description":"You learn how to create and call functions in Python. You learn about pass by reference and by value and different types of arguments","breadcrumb":{"@id":"https:\/\/www.kindsonthegenius.com\/python\/15-python-functions\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.kindsonthegenius.com\/python\/15-python-functions\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.kindsonthegenius.com\/python\/15-python-functions\/#primaryimage","url":"https:\/\/www.kindsonthegenius.com\/python\/wp-content\/uploads\/sites\/8\/2019\/02\/Functions-in-Python2.jpg","contentUrl":"https:\/\/www.kindsonthegenius.com\/python\/wp-content\/uploads\/sites\/8\/2019\/02\/Functions-in-Python2.jpg","width":1005,"height":574,"caption":"Functions in Python"},{"@type":"BreadcrumbList","@id":"https:\/\/www.kindsonthegenius.com\/python\/15-python-functions\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.kindsonthegenius.com\/python\/"},{"@type":"ListItem","position":2,"name":"Python &#8211; Functions"}]},{"@type":"WebSite","@id":"https:\/\/www.kindsonthegenius.com\/python\/#website","url":"https:\/\/www.kindsonthegenius.com\/python\/","name":"Python Tutorials","description":"Python Tutorial for Programming and Data Science","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.kindsonthegenius.com\/python\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.kindsonthegenius.com\/python\/#\/schema\/person\/63a68934672db675ff0cd80d066510c2","name":"kindsonthegenius","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/3079a7f663b02e801d03cd075852a037af36bd179b5fbcd0603bae3dd7833a9b?s=96&d=mm&r=g","caption":"kindsonthegenius"},"url":"https:\/\/www.kindsonthegenius.com\/python\/author\/kindsonthegenius-2\/"}]}},"_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/posts\/187","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/users\/395"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/comments?post=187"}],"version-history":[{"count":5,"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/posts\/187\/revisions"}],"predecessor-version":[{"id":232,"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/posts\/187\/revisions\/232"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/media\/188"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/media?parent=187"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/categories?post=187"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/python\/wp-json\/wp\/v2\/tags?post=187"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}