{"id":127,"date":"2023-06-02T19:23:26","date_gmt":"2023-06-02T19:23:26","guid":{"rendered":"http:\/\/python.garden\/index.php\/2023\/06\/02\/defining-functions\/"},"modified":"2023-06-02T23:04:42","modified_gmt":"2023-06-02T23:04:42","slug":"defining-functions","status":"publish","type":"post","link":"https:\/\/python.garden\/index.php\/2023\/06\/02\/defining-functions\/","title":{"rendered":"Chapter 3.1: Defining Functions"},"content":{"rendered":"\n<p>Understanding how to define functions<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><\/h1>\n\n\n\n<p>In Python, functions are a way to group code fragments that perform a specific task. Functions promote code reusability, make the code easier to read, and allow for more manageable code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax of Function<\/h2>\n\n\n\n<p>Here is the basic syntax for defining a function in Python:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">def function_name(parameters):\n    &quot;&quot;&quot;docstring&quot;&quot;&quot;\n    statement(s)<\/pre><\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The keyword <code>def<\/code> starts the function definition, followed by the function name.<\/li>\n\n\n\n<li>Parentheses contain the function&#8217;s parameters, also called arguments. Multiple parameters are separated by commas.<\/li>\n\n\n\n<li>A colon (<code>:<\/code>) follows the parentheses, and the indented lines after the colon are the function&#8217;s body.<\/li>\n\n\n\n<li>The optional docstring (short for documentation string) describes what the function does. Although optional, documentation is a good programming practice.<\/li>\n\n\n\n<li>The function body contains one or more valid Python statements. These statements must be indented.<\/li>\n<\/ul>\n\n\n\n<p>Here&#8217;s an example of a function that adds two numbers:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">def add_numbers(num1, num2):\n    &quot;&quot;&quot;This function adds two numbers.&quot;&quot;&quot;\n    return num1 + num2<\/pre><\/div>\n\n\n\n<p>To use this function, you would call it, passing two numbers as arguments. For example, <code>add_numbers(2, 3)<\/code> would return <code>5<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The <code>return<\/code> Statement<\/h2>\n\n\n\n<p>The <code>return<\/code> statement is used to exit a function and go back to the place where it was called. It can also send a result back to the caller. For example, in our <code>add_numbers<\/code> function above, <code>return num1 + num2<\/code> sends the sum of the two numbers back to the caller.<\/p>\n\n\n\n<p>A function without a <code>return<\/code> statement returns <code>None<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Arguments and Parameters<\/h2>\n\n\n\n<p>You can call a function using the following types of arguments:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Required arguments: the function arguments that must be passed in the correct positional order.<\/li>\n\n\n\n<li>Keyword arguments: allows you to skip arguments or place them out of order because Python interpreter is able to use the keywords provided to match the values with parameters.<\/li>\n\n\n\n<li>Default arguments: a default value is assigned to the argument which is used if the function is called without passing that particular argument.<\/li>\n\n\n\n<li>Variable-length arguments: processes a function for more arguments than you specify while defining the function.<\/li>\n<\/ul>\n\n\n\n<p>By understanding the basic structure of a function and how to define one, you&#8217;ll be well on your way to writing cleaner, more reusable code in Python. Functions are a foundational component of any programming language and mastering them is crucial to becoming an efficient Python programmer.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Understanding how to define functions In Python, functions are a way to group code fragments that perform a specific task. Functions promote code reusability, make the code easier to read,&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[104,103,94],"tags":[],"class_list":["post-127","post","type-post","status-publish","format-standard","hentry","category-defining-functions","category-functions-and-modules","category-introduction-to-python-2"],"featured_image_src":null,"author_info":{"display_name":"shababdoo","author_link":"https:\/\/python.garden\/index.php\/author\/shababdoo\/"},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/posts\/127","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/comments?post=127"}],"version-history":[{"count":0,"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/posts\/127\/revisions"}],"wp:attachment":[{"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/media?parent=127"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/categories?post=127"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/tags?post=127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}