{"id":63,"date":"2026-03-01T15:51:43","date_gmt":"2026-03-01T10:51:43","guid":{"rendered":"https:\/\/gigz.pk\/python\/?post_type=lesson&#038;p=63"},"modified":"2026-03-13T10:10:03","modified_gmt":"2026-03-13T05:10:03","slug":"defining-functions","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/python\/lesson\/defining-functions\/","title":{"rendered":"\u00a0Defining Functions"},"content":{"rendered":"\n<p>A <strong>function<\/strong> is a block of organized, reusable code that performs a specific task.<br>Functions help make programs shorter, cleaner, and easier to manage.<\/p>\n\n\n\n<p>Instead of writing the same code again and again, you can define it once and reuse it.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>WHY USE FUNCTIONS?<\/strong><\/h1>\n\n\n\n<p>\u2022 Reduce code repetition<br>\u2022 Improve readability<br>\u2022 Make programs modular<br>\u2022 Simplify debugging and maintenance<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>SYNTAX OF A FUNCTION<\/strong><\/h1>\n\n\n\n<pre class=\"wp-block-preformatted\">def function_name(parameters):<br>    # code block<\/pre>\n\n\n\n<p>\u2022 <code>def<\/code> is the keyword used to define a function<br>\u2022 <code>function_name<\/code> is the name of the function<br>\u2022 <code>parameters<\/code> are optional inputs<br>\u2022 Indentation is required<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>EXAMPLE 1: SIMPLE FUNCTION<\/strong><\/h1>\n\n\n\n<pre class=\"wp-block-preformatted\">def greet():<br>    print(\"Hello, welcome to Python!\")greet()<\/pre>\n\n\n\n<p>When you call <code>greet()<\/code>, the message is printed.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>FUNCTION WITH PARAMETERS<\/strong><\/h1>\n\n\n\n<p>You can pass values into a function.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">def greet(name):<br>    print(\"Hello\", name)greet(\"Hira\")<\/pre>\n\n\n\n<p>The function prints a personalized message.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>FUNCTION WITH RETURN VALUE<\/strong><\/h1>\n\n\n\n<p>Functions can return a result using the <code>return<\/code> keyword.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">def add(a, b):<br>    return a + bresult = add(5, 3)<br>print(result)<\/pre>\n\n\n\n<p>The function calculates the sum and returns it.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>DEFAULT PARAMETERS<\/strong><\/h1>\n\n\n\n<p>You can provide default values to parameters.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">def greet(name=\"Guest\"):<br>    print(\"Hello\", name)greet()<br>greet(\"Ali\")<\/pre>\n\n\n\n<p>If no value is passed, the default value is used.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>KEY POINTS<\/strong><\/h1>\n\n\n\n<p>\u2022 Function names should be meaningful<br>\u2022 Always use parentheses when calling a function<br>\u2022 Use return when you need output from a function<br>\u2022 Indentation is very important<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>WHY FUNCTIONS ARE IMPORTANT<\/strong><\/h1>\n\n\n\n<p>Functions are essential for building large applications.<br>They help organize code into manageable sections and make programs more efficient and professional.<\/p>\n\n\n\n<p>Understanding how to define and use functions is a fundamental skill in Python programming.<\/p>\n\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1773378627998\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/python\/\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">PYTHON FUNDAMENTALS (PYF) > Functions > Defining Functions<\/span><\/span><\/div>","protected":false},"menu_order":21,"template":"","class_list":["post-63","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>\u00a0Defining Functions - One Language. Endless Possibilities<\/title>\n<meta name=\"description\" content=\"Learn Python functions to write reusable code, pass parameters, return values, use default arguments, and build efficient programs.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/python\/lesson\/defining-functions\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u00a0Defining Functions - One Language. Endless Possibilities\" \/>\n<meta property=\"og:description\" content=\"Learn Python functions to write reusable code, pass parameters, return values, use default arguments, and build efficient programs.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/python\/lesson\/defining-functions\/\" \/>\n<meta property=\"og:site_name\" content=\"One Language. Endless Possibilities\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-13T05:10:03+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/defining-functions\\\/\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/defining-functions\\\/\",\"name\":\"\u00a0Defining Functions - One Language. Endless Possibilities\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\"},\"datePublished\":\"2026-03-01T10:51:43+00:00\",\"dateModified\":\"2026-03-13T05:10:03+00:00\",\"description\":\"Learn Python functions to write reusable code, pass parameters, return values, use default arguments, and build efficient programs.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/defining-functions\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/defining-functions\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/defining-functions\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PYTHON FUNDAMENTALS (PYF) > Functions > Defining Functions\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\",\"name\":\"One Language. Endless Possibilities\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/python\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u00a0Defining Functions - One Language. Endless Possibilities","description":"Learn Python functions to write reusable code, pass parameters, return values, use default arguments, and build efficient programs.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/gigz.pk\/python\/lesson\/defining-functions\/","og_locale":"en_US","og_type":"article","og_title":"\u00a0Defining Functions - One Language. Endless Possibilities","og_description":"Learn Python functions to write reusable code, pass parameters, return values, use default arguments, and build efficient programs.","og_url":"https:\/\/gigz.pk\/python\/lesson\/defining-functions\/","og_site_name":"One Language. Endless Possibilities","article_modified_time":"2026-03-13T05:10:03+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/python\/lesson\/defining-functions\/","url":"https:\/\/gigz.pk\/python\/lesson\/defining-functions\/","name":"\u00a0Defining Functions - One Language. Endless Possibilities","isPartOf":{"@id":"https:\/\/gigz.pk\/python\/#website"},"datePublished":"2026-03-01T10:51:43+00:00","dateModified":"2026-03-13T05:10:03+00:00","description":"Learn Python functions to write reusable code, pass parameters, return values, use default arguments, and build efficient programs.","breadcrumb":{"@id":"https:\/\/gigz.pk\/python\/lesson\/defining-functions\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/python\/lesson\/defining-functions\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/python\/lesson\/defining-functions\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/python\/"},{"@type":"ListItem","position":2,"name":"PYTHON FUNDAMENTALS (PYF) > Functions > Defining Functions"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/python\/#website","url":"https:\/\/gigz.pk\/python\/","name":"One Language. Endless Possibilities","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/python\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson\/63","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/media?parent=63"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}