{"id":45,"date":"2022-11-26T10:52:44","date_gmt":"2022-11-26T10:52:44","guid":{"rendered":"https:\/\/blog.metu.edu.tr\/e243521\/?p=45"},"modified":"2022-11-26T11:31:32","modified_gmt":"2022-11-26T11:31:32","slug":"python-functions","status":"publish","type":"post","link":"https:\/\/blog.metu.edu.tr\/e243521\/2022\/11\/26\/python-functions\/","title":{"rendered":"Python Functions"},"content":{"rendered":"<h2 id=\"what\">What is a function in Python?<\/h2>\n<p>In Python, a function is a set of related statements that perform a specific task. Functions help break down our program into smaller and modular parts. As our program grows, functions make it more organized and manageable.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-46\" src=\"https:\/\/blog.metu.edu.tr\/e243521\/files\/2022\/11\/f1-300x85.jpg\" alt=\"\" width=\"300\" height=\"85\" srcset=\"https:\/\/blog.metu.edu.tr\/e243521\/files\/2022\/11\/f1-300x85.jpg 300w, https:\/\/blog.metu.edu.tr\/e243521\/files\/2022\/11\/f1.jpg 372w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>Above code explanation:<\/p>\n<ul>\n<li>Keyword <strong>def<\/strong> that marks the start of the function header.<\/li>\n<li>A colon (:) to mark the end of the function header.<\/li>\n<li>Optional documentation string (docstring) to describe what the function does.<\/li>\n<\/ul>\n<p>An example of one python function:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-47\" src=\"https:\/\/blog.metu.edu.tr\/e243521\/files\/2022\/11\/f2-300x113.jpg\" alt=\"\" width=\"300\" height=\"113\" srcset=\"https:\/\/blog.metu.edu.tr\/e243521\/files\/2022\/11\/f2-300x113.jpg 300w, https:\/\/blog.metu.edu.tr\/e243521\/files\/2022\/11\/f2.jpg 570w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<h3 id=\"call\">How to call a function in python?<\/h3>\n<p>Once a function is created in Python, we can call it by writing\u00a0<strong>functionName()<\/strong>\u00a0itself or another function\/ nested function. Following is the syntax for calling a function.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-48\" src=\"https:\/\/blog.metu.edu.tr\/e243521\/files\/2022\/11\/f3-300x148.jpg\" alt=\"\" width=\"300\" height=\"148\" srcset=\"https:\/\/blog.metu.edu.tr\/e243521\/files\/2022\/11\/f3-300x148.jpg 300w, https:\/\/blog.metu.edu.tr\/e243521\/files\/2022\/11\/f3.jpg 561w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<h2 id=\"return\">The return statement<\/h2>\n<p>The Python return statement is a special statement that you can use inside a function or method to send the function&#8217;s result back to the caller. A return statement consists of the return keyword followed by an optional return value.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-49\" src=\"https:\/\/blog.metu.edu.tr\/e243521\/files\/2022\/11\/f4-300x221.jpg\" alt=\"\" width=\"300\" height=\"221\" srcset=\"https:\/\/blog.metu.edu.tr\/e243521\/files\/2022\/11\/f4-300x221.jpg 300w, https:\/\/blog.metu.edu.tr\/e243521\/files\/2022\/11\/f4.jpg 499w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is a function in Python? In Python, a function is a set of related statements that perform a specific task. Functions help break down our program into smaller and modular parts. As our program grows, functions make it more organized and manageable. Above code explanation: Keyword def that marks the start of the function [&hellip;]<\/p>\n","protected":false},"author":8584,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[1],"tags":[],"class_list":["post-45","post","type-post","status-publish","format-standard","hentry","category-all-tutorial"],"_links":{"self":[{"href":"https:\/\/blog.metu.edu.tr\/e243521\/wp-json\/wp\/v2\/posts\/45","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.metu.edu.tr\/e243521\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.metu.edu.tr\/e243521\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.metu.edu.tr\/e243521\/wp-json\/wp\/v2\/users\/8584"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.metu.edu.tr\/e243521\/wp-json\/wp\/v2\/comments?post=45"}],"version-history":[{"count":0,"href":"https:\/\/blog.metu.edu.tr\/e243521\/wp-json\/wp\/v2\/posts\/45\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.metu.edu.tr\/e243521\/wp-json\/wp\/v2\/media?parent=45"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.metu.edu.tr\/e243521\/wp-json\/wp\/v2\/categories?post=45"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.metu.edu.tr\/e243521\/wp-json\/wp\/v2\/tags?post=45"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}