{"id":55,"date":"2025-10-19T15:04:59","date_gmt":"2025-10-19T15:04:59","guid":{"rendered":"https:\/\/codetypingpro.com\/?p=55"},"modified":"2025-12-17T07:50:20","modified_gmt":"2025-12-17T07:50:20","slug":"lesson-7-functions-in-python","status":"publish","type":"post","link":"https:\/\/codetypingpro.com\/?p=55","title":{"rendered":"Lesson 7: Functions in Python"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\"><\/h3>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83c\udfaf <strong>Lesson Objective<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To understand what functions are, how to create and use them, and why they are essential for code organization and reuse.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83e\udde0 <strong>1. What Is a Function?<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A <strong>function<\/strong> is a reusable block of code that performs a specific task.<br>Functions make programs easier to read, test, and maintain.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u2699\ufe0f <strong>2. Syntax of a Function<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>def function_name(parameters):\n    # code block\n    return value\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u2705 <strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def greet():\n    print(\"Hello, welcome to Python!\")\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>greet()\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Hello, welcome to Python!\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83e\udde9 <strong>3. Function with Parameters<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Functions can accept inputs (called <strong>parameters<\/strong>) that make them flexible.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def add_numbers(a, b):\n    result = a + b\n    print(\"Sum:\", result)\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>add_numbers(5, 7)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Sum: 12\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83e\uddfe <strong>4. Function with Return Statement<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A function can <strong>return<\/strong> data back to the caller using the <code>return<\/code> keyword.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def multiply(a, b):\n    return a * b\n\nresult = multiply(4, 6)\nprint(\"Product:\", result)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Product: 24\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udd01 <strong>5. Default Parameters<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">If a parameter value isn\u2019t provided, Python uses the default.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def greet(name=\"User\"):\n    print(\"Hello,\", name)\n\ngreet(\"Sameer\")\ngreet()\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Hello, Sameer\nHello, User\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83e\uddee <strong>6. Function with Multiple Returns<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A function can return multiple values separated by commas.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def calculate(a, b):\n    return a + b, a - b\n\nsum_val, diff_val = calculate(10, 5)\nprint(\"Sum:\", sum_val)\nprint(\"Difference:\", diff_val)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Sum: 15\nDifference: 5\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udd04 <strong>7. Variable Scope<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Variables defined inside a function are <strong>local<\/strong>, while those outside are <strong>global<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>x = 10  # Global variable\n\ndef show():\n    x = 5  # Local variable\n    print(\"Inside function:\", x)\n\nshow()\nprint(\"Outside function:\", x)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Inside function: 5\nOutside function: 10\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83e\uddf0 <strong>8. Lambda (Anonymous) Functions<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A <strong>lambda function<\/strong> is a short, one-line function without a name.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>square = lambda x: x * x\nprint(square(4))\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>16\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83c\udf0d <strong>9. Real-Life Example: Calculator Function<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>def calculator(a, b, operation):\n    if operation == \"add\":\n        return a + b\n    elif operation == \"subtract\":\n        return a - b\n    elif operation == \"multiply\":\n        return a * b\n    elif operation == \"divide\":\n        return a \/ b\n    else:\n        return \"Invalid operation\"\n\nprint(calculator(10, 5, \"add\"))\nprint(calculator(10, 5, \"divide\"))\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>15\n2.0\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83e\udde0 <strong>10. Practice Ideas<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Write a function to find the factorial of a number.<\/li>\n\n\n\n<li>Create a function that checks whether a number is even or odd.<\/li>\n\n\n\n<li>Build a function to count vowels in a string.<\/li>\n\n\n\n<li>Write a function to convert Celsius to Fahrenheit.<\/li>\n\n\n\n<li>Make a calculator using functions for each operation.<\/li>\n\n\n\n<li>Use a lambda function to find the square and cube of a number.<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>\ud83c\udfaf Lesson Objective To understand what functions are, how to create and use them, and why they are essential for code organization and reuse. \ud83e\udde0 1. What Is a Function? A function is a reusable block of code that performs a specific task.Functions make programs easier to read, test, and maintain. \u2699\ufe0f 2. Syntax of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,1],"tags":[],"class_list":["post-55","post","type-post","status-publish","format-standard","hentry","category-python-easy-course-outline","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/55","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=55"}],"version-history":[{"count":1,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/55\/revisions"}],"predecessor-version":[{"id":56,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=\/wp\/v2\/posts\/55\/revisions\/56"}],"wp:attachment":[{"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=55"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=55"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codetypingpro.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=55"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}