{"id":3316,"date":"2020-05-21T18:19:19","date_gmt":"2020-05-21T12:49:19","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=3316"},"modified":"2020-05-21T18:19:21","modified_gmt":"2020-05-21T12:49:21","slug":"python-functions-examples","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/python-functions-examples\/","title":{"rendered":"Python Functions Examples"},"content":{"rendered":"\n<p>Call, Indentation, Arguments &amp; Return Values<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is a function?<\/h2>\n\n\n\n<p>Consider a function a box just as shown below with two sides open one for input and other for output. This box takes input then does some operations on the given input and provides you with output. If this box is a function of addition then when you give it two numbers 3 and 4 as input it will output 7. If this box is a function of multiplication then with the same input 3 and 4 it will output 12.<\/p>\n\n\n\n<p class=\"has-text-align-center\"><img decoding=\"async\" width=\"189\" height=\"187\" src=\"https:\/\/lh4.googleusercontent.com\/tPlDXBIVOJhQxmZa3MjfwqTf763x-gC1yLEFehSIBihVQOozsI-GMd0fqXxRR7TSPxn25hBbHRUaQZOzDdVqI8OWiNszHINJNjGS97SnEUU38Yot6o1iAlXm6_hrjALenc0bAiECm-kKUEgPPA\" alt=\"\" title=\"\"><\/p>\n\n\n\n<p>Let\u2019s make an \u2018addition\u2019 function.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Addition Function Example<\/h2>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/jmz5Oza2g5WJSO0jyZHso3jUXzbMN2bAkbxCC3R9JMNwQZdnSkv2FA3nG-tpLMR35bDn9VFV_BonagXYvUTy_uE_dIwrLjLPZgz-XjsYWGBCOSTywQEmlWVWydCU7VXM-OLd9ql--gfGl8aZ5g\" alt=\"\" title=\"\"><\/figure><\/div>\n\n\n\n<p><strong>def <\/strong>is a keyword that is used to define a function. \u201cAddition\u201d is the name of the function.<\/p>\n\n\n\n<p>x and y are the input values. The<strong> result<\/strong> is a variable. When we passed 3 and 4 as input the function performed the addition operation on the inputs and stored the answer in the result variable. Which is then printed by the print function.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Calling Function<\/h2>\n\n\n\n<p>The red rectangle in the screenshot below is an example of function calls. While <a href=\"https:\/\/www.h2kinfosys.com\/blog\/python-dictionary\/\">calling any function <\/a>the name of the function is used and we give parameters in the round brackets as shown in the figure below.<\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img fetchpriority=\"high\" decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/wspOSQ6kztykO_N7PdHW0-pkO0dKl42WLl-yCRxYqWH2Vhvl3dhuPGdSVGvj8pPKGjb-nkVQ2Um6Mqb-syxoEXBlmTANvMkWidFvkDVpPxRpoawOZskoH208gZbfwHtERIn7n1AW9GA3KKmpwA\" alt=\"\" width=\"580\" height=\"233\" title=\"\"><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Indentation<\/h2>\n\n\n\n<p>In other languages like c++ semicolon is used to define that this line of code has ended. But Python has its own way of defining syntax. Traditionally, the Indentation of one tab or 4 spaces is used by the Python community. You can use any number of spaces but you have to use the same number of spaces in your entire code.&nbsp;<\/p>\n\n\n\n<p>Indentation in Python makes it a highly readable language. As shown below an indentation block is necessary to <a href=\"https:\/\/www.w3schools.com\/python\/python_functions.asp\" rel=\"nofollow noopener\" target=\"_blank\">define a function<\/a>. This indentation says that \u201cI am a part of this function\u201d. Below the call \u201caddition(2, 4)\u201d is not indented as it is not part of the function. The function ends at return.<br><img decoding=\"async\" width=\"624\" height=\"249\" src=\"https:\/\/lh3.googleusercontent.com\/BL5ABKjYDCJXsvKfQbXx0lbuq6-_f5DIv5ACNcNM8v4VtfkWrX1YUf2Tuz4Q0_HKd6gV9sgbrSag5wDg0NMPCdw1vpQnBoGjaF4CgHrMrMlSyjc1UPvUeq5hrQzQCZhNZx_sI_nvJi8qJL6apg\" alt=\"\" title=\"\"><\/p>\n\n\n\n<p>If we don\u2019t indent the code then we will face an \u201cIndentationError\u201d.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/JTgORCiTP2NBFl7wF9f3BzBKopF3M8tm2FQImjNDBdp8RIUFtzKBA7vVFi8Moa_uBwK7I4k7VGqQADqk0f8-OCVEXTkAQ6t6wucmgcdBPM-i3IhfanGRcI4N4YyWNnY1cWfuO7dQ7zIK-r0ANg\" width=\"565\" height=\"256\" alt=\"\" title=\"\"><\/h2>\n\n\n\n<h2 class=\"wp-block-heading\">Function Arguments<\/h2>\n\n\n\n<p>In the above example, the x and y are the arguments of the function. Arguments are just what will be the input of the function.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/Q_k7-Xy3JdmJ6FXdecgF_M_ukq-o-IFpDjRYDBzFOx36FPWzL52oixsfgvEtf-WqkUL59xBkLkQJF7XUir7pddVQkHRLCdogOF0BXmP3_icKRVMQkbBDXZeF6Gkh_rgwySwfn_5os-R8jR1rJQ\" alt=\"\" title=\"\"><\/figure>\n\n\n\n<p>A function can be without arguments and can also have as many arguments as required. Let\u2019s take a look at a function without arguments.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/3N4tpgGUXOJxrV6vwSpwXb7XG-NXYevbJeTIczxRt6vwr5vXKNYyGD_v9E1Dker4K2HGq9UZvCJ3C7hGHQbAQZyVRvdA0nQB-Ps272JgULV58C5iAn9yP8BXJWOwa1xeTSbei79ZGaNnzO3N-Q\" alt=\"\" title=\"\"><\/figure>\n\n\n\n<p>As it can be seen that the above function does not contain \u201creturn\u201d. Such functions return \u201cNone\u201d.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Return Value<\/h2>\n\n\n\n<p>A \u201creturn\u201d keyword is used to return a value from the function or return the output. As shown in the example below the \u201cresult\u201d variable will be returned whenever an \u201caddition\u201d function is called.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/LEQvkJyWeTPkeG9KamdR665ulvKO_TnqhBM57Ur3bdJNABCFGy8fAKNHYOacbYWKOVDJVRZNrnxXr11od3Udzqo8-WgJ5eg5X15ZaRtnLHo1F-2-jR3rbmHFfQfvkl77T8Q-gD_tCEmRfxnWzA\" alt=\"\" title=\"\"><\/figure>\n\n\n\n<p>Let\u2019s combine all the concepts in one example.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/TX1IfzEJGwF1zqXejkd30jrC8nOdMMAfWs9pNOqkpXNqKV6dnwCt6VBbehycEOlT7oPLLwB0EVGVOS3WIRODG95GfjhI-Lfiz4wfpuORfytxK78ObSSs5M4OYW-JIF86rxIn2daJu6l_nbhXnA\" alt=\"\" title=\"\"><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Call, Indentation, Arguments &amp; Return Values What is a function? Consider a function a box just as shown below with two sides open one for input and other for output. This box takes input then does some operations on the given input and provides you with output. If this box is a function of addition [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3320,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[342],"tags":[802,801,803,639],"class_list":["post-3316","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorials","tag-function-arguments","tag-function-example","tag-indentation","tag-python-functions"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/3316","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=3316"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/3316\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/3320"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=3316"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=3316"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=3316"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}