{"id":412,"date":"2026-02-10T03:55:16","date_gmt":"2026-02-10T03:55:16","guid":{"rendered":"https:\/\/mitalgoswami.in\/?p=412"},"modified":"2026-02-17T04:37:49","modified_gmt":"2026-02-17T04:37:49","slug":"function","status":"publish","type":"post","link":"https:\/\/mitalgoswami.in\/?p=412","title":{"rendered":"Function"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">&#x1f527; What is a function?<\/h2>\n\n\n\n<p>A <strong>function<\/strong> is a block of code that:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>performs a <strong>specific task<\/strong><\/li>\n\n\n\n<li>can be <strong>reused<\/strong><\/li>\n\n\n\n<li>reduces code duplication<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">&#x1f539; Function syntax<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>function_name () {\n    commands\n}\n<\/code><\/pre>\n\n\n\n<p>OR<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function function_name {\n    commands\n}\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">&#x2705; Example 1: Simple function (EXAM BASIC &#x2b50;)<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\n\ngreet () {\n    echo \"Hello Linux\"\n}\n\ngreet\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">&#x2705; Example 2: Function with arguments<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\n\nadd () {\n    sum=$(( $1 + $2 ))\n    echo \"Sum = $sum\"\n}\n\nadd 10 20\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">&#x2705; Example 3: Function returning value (using echo)<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\n\nsquare () {\n    echo $(( $1 * $1 ))\n}\n\nresult=$(square 5)\necho \"Square = $result\"\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">&#x2705; Example 4: Function to check even or odd<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\n\ncheck () {\n    if &#91; $(( $1 % 2 )) -eq 0 ]; then\n        echo \"Even number\"\n    else\n        echo \"Odd number\"\n    fi\n}\n\ncheck 7\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">&#x2705; Example 5: Function using global variable<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\n\nx=10\n\nshow () {\n    echo \"Value of x = $x\"\n}\n\nshow\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">&#x2705; Example 6: Function with local variable<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\n\ndisplay () {\n    local msg=\"Linux OS\"\n    echo $msg\n}\n\ndisplay\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">&#x2705; Example 7: Function + array<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\n\nprint_array () {\n    echo ${arr&#91;@]}\n}\n\narr=(1 2 3 4)\nprint_array\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>&#x1f527; What is a function? A function is a block of code that: &#x1f539; Function syntax OR &#x2705; Example 1: Simple function (EXAM BASIC &#x2b50;) &#x2705; Example 2: Function with arguments &#x2705; Example 3: Function returning value (using echo) &#x2705; Example 4: Function to check even or odd &#x2705; Example 5: Function using global variable [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-412","post","type-post","status-publish","format-standard","hentry","category-unix"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/posts\/412","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=412"}],"version-history":[{"count":1,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/posts\/412\/revisions"}],"predecessor-version":[{"id":413,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=\/wp\/v2\/posts\/412\/revisions\/413"}],"wp:attachment":[{"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=412"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=412"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mitalgoswami.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=412"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}