{"id":57,"date":"2022-11-26T12:54:07","date_gmt":"2022-11-26T12:54:07","guid":{"rendered":"https:\/\/blog.metu.edu.tr\/e243521\/?page_id=57"},"modified":"2022-11-26T13:07:55","modified_gmt":"2022-11-26T13:07:55","slug":"my-blog","status":"publish","type":"page","link":"https:\/\/blog.metu.edu.tr\/e243521\/my-blog\/","title":{"rendered":"My Blog"},"content":{"rendered":"<h1 class=\"entry-title\">PYTHON FUNCTIONS TUTORIAL<\/h1>\n<div class=\"entry-content\">\n<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=\"wp-image-46  alignleft\" src=\"https:\/\/blog.metu.edu.tr\/e243521\/files\/2022\/11\/f1-300x85.jpg\" alt=\"\" width=\"229\" height=\"65\" 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: 229px) 100vw, 229px\" \/><\/p>\n<\/div>\n<p>Above code explanation:<\/p>\n<p>Keyword\u00a0<strong>def<\/strong>\u00a0that marks the start of the function header.<\/p>\n<p>A colon (:) to mark the end of the function header.<\/p>\n<p>Optional documentation string (docstring) to describe what the function does.<\/p>\n<h3><\/h3>\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> itself or another function\/nested function. Following is the syntax for calling a function.<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\u2019s 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<h2 class=\"entry-title\">ARGUMENTS IN PYTHON<\/h2>\n<div class=\"entry-content\">\n<p>Functions accept arguments that can contain data. The function name is followed by parenthesis that list the arguments. Simply separate each argument with a comma to add as many as you like.<\/p>\n<p>Example 1: Python Function Arguments:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-36\" src=\"https:\/\/blog.metu.edu.tr\/e243521\/files\/2022\/11\/a1-300x200.jpg\" alt=\"\" width=\"300\" height=\"200\" srcset=\"https:\/\/blog.metu.edu.tr\/e243521\/files\/2022\/11\/a1-300x200.jpg 300w, https:\/\/blog.metu.edu.tr\/e243521\/files\/2022\/11\/a1-272x182.jpg 272w, https:\/\/blog.metu.edu.tr\/e243521\/files\/2022\/11\/a1.jpg 442w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<\/div>\n<p>In the above example, the function\u00a0<code>add_numbers()<\/code>\u00a0takes two parameters:\u00a0<code>a<\/code>\u00a0and\u00a0<code>b<\/code>.<\/p>\n<p><strong>Function Argument with Default Values:<\/strong><\/p>\n<p>In Python, we can provide default values to function arguments. We use the\u00a0<code>=<\/code>\u00a0operator to provide default values<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-37\" src=\"https:\/\/blog.metu.edu.tr\/e243521\/files\/2022\/11\/a2-300x258.jpg\" alt=\"\" width=\"300\" height=\"258\" srcset=\"https:\/\/blog.metu.edu.tr\/e243521\/files\/2022\/11\/a2-300x258.jpg 300w, https:\/\/blog.metu.edu.tr\/e243521\/files\/2022\/11\/a2.jpg 475w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-38\" src=\"https:\/\/blog.metu.edu.tr\/e243521\/files\/2022\/11\/ao1.jpg\" alt=\"\" width=\"207\" height=\"135\" \/><\/p>\n<p>In the above example, the function definition Here, we have provided default values\u00a0<strong>7<\/strong>\u00a0and\u00a0<strong>8<\/strong>\u00a0for parameters a and b respectively.<\/p>\n<p><strong>Python Keyword Argument<\/strong><\/p>\n<p>A function must always be called with the appropriate number of parameters by default. In other words, if your function requires 2 arguments, you must call it with 2 arguments, not more or fewer.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-39\" src=\"https:\/\/blog.metu.edu.tr\/e243521\/files\/2022\/11\/a3-300x80.jpg\" alt=\"\" width=\"300\" height=\"80\" srcset=\"https:\/\/blog.metu.edu.tr\/e243521\/files\/2022\/11\/a3-300x80.jpg 300w, https:\/\/blog.metu.edu.tr\/e243521\/files\/2022\/11\/a3.jpg 739w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>First name from the function declaration is paired with first name from the function call. Similar to how last name in the function specification corresponds to last name in the function call.<\/p>\n<h2 id=\"function\">Recursive Function<\/h2>\n<h2 id=\"what\">What is recursion?<\/h2>\n<p>Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function.<\/p>\n<h2 id=\"function\">Python Recursive Function<\/h2>\n<p>In Python, we know that a function can call other functions. It is even possible for the function to call itself. These types of construct are termed as recursive functions<\/p>\n<h2 class=\"entry-title\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-41\" src=\"https:\/\/blog.metu.edu.tr\/e243521\/files\/2022\/11\/r1-300x127.jpg\" alt=\"\" width=\"300\" height=\"127\" srcset=\"https:\/\/blog.metu.edu.tr\/e243521\/files\/2022\/11\/r1-300x127.jpg 300w, https:\/\/blog.metu.edu.tr\/e243521\/files\/2022\/11\/r1.jpg 618w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/h2>\n<p>Factorial of a number is the product of all the integers from 1 to that number. For example, the factorial of 3 (denoted as 3!) is 3*2*1. Quesition is that how to implement this function in python?<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-42\" src=\"https:\/\/blog.metu.edu.tr\/e243521\/files\/2022\/11\/r2-300x179.jpg\" alt=\"\" width=\"300\" height=\"179\" srcset=\"https:\/\/blog.metu.edu.tr\/e243521\/files\/2022\/11\/r2-300x179.jpg 300w, https:\/\/blog.metu.edu.tr\/e243521\/files\/2022\/11\/r2.jpg 687w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>In the above example, factorial() is a recursive function as it calls itself. When we call this function with a positive integer, it will recursively call itself by decreasing the number. Each function multiplies the number with the factorial of the number below it until it is equal to one.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-43\" src=\"https:\/\/blog.metu.edu.tr\/e243521\/files\/2022\/11\/r3-262x300.jpg\" alt=\"\" width=\"262\" height=\"300\" srcset=\"https:\/\/blog.metu.edu.tr\/e243521\/files\/2022\/11\/r3-262x300.jpg 262w, https:\/\/blog.metu.edu.tr\/e243521\/files\/2022\/11\/r3.jpg 538w\" sizes=\"auto, (max-width: 262px) 100vw, 262px\" \/><\/p>\n<p>Our recursion ends when the number reduces to 1. This is called the base condition. Every recursive function must have a base condition that stops the recursion or else the function calls itself infinitely.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PYTHON FUNCTIONS TUTORIAL 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\u00a0def\u00a0that marks the start of the [&hellip;]<\/p>\n","protected":false},"author":8584,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"class_list":["post-57","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blog.metu.edu.tr\/e243521\/wp-json\/wp\/v2\/pages\/57","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.metu.edu.tr\/e243521\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blog.metu.edu.tr\/e243521\/wp-json\/wp\/v2\/types\/page"}],"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=57"}],"version-history":[{"count":0,"href":"https:\/\/blog.metu.edu.tr\/e243521\/wp-json\/wp\/v2\/pages\/57\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.metu.edu.tr\/e243521\/wp-json\/wp\/v2\/media?parent=57"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}