{"id":451,"date":"2025-05-20T04:51:26","date_gmt":"2025-05-20T04:51:26","guid":{"rendered":"https:\/\/buhave.com\/courses\/?p=451"},"modified":"2025-05-20T12:56:49","modified_gmt":"2025-05-20T12:56:49","slug":"functions","status":"publish","type":"post","link":"https:\/\/buhave.com\/courses\/python\/functions\/","title":{"rendered":"Functions"},"content":{"rendered":"<h2><strong>Defining and calling functions<\/strong><span style=\"font-weight: 400\"><br \/>\n<\/span><\/h2>\n<p><strong>What Are Functions?<\/strong><\/p>\n<p>A function is a block of reusable code that performs a specific task. You define functions to avoid repeating code, enhance readability, and make your code more modular.<\/p>\n<h3>1. Defining a Function<\/h3>\n<p>To define a function, you use the def keyword followed by the function name, parentheses, and a colon (:). The indented code that follows is the body of the function, where the logic happens.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<p style=\"text-align: center\"><em>def function_name(parameters):<\/em><br \/>\n<em># function body<\/em><br \/>\n<em># code to be executed<\/em><\/p>\n<p><strong>Example 1:<\/strong> A Simple Function<\/p>\n<p style=\"text-align: center\"><em>def greet():<\/em><br \/>\n<em>print(&#8220;Hello, welcome!&#8221;)<\/em><\/p>\n<ul>\n<li>def defines the function.<\/li>\n<li>greet() is the function name.<\/li>\n<li>The body contains the print() statement.<\/li>\n<\/ul>\n<p>This function doesn\u2019t accept any parameters and simply prints a message when called.<\/p>\n<h3>2. Calling a Function<\/h3>\n<p>After defining a function, you can call it by using its name followed by parentheses.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<p style=\"text-align: center\"><em>function_name()<\/em><\/p>\n<p style=\"text-align: center\"><strong>Example 2:<\/strong> Calling the greet() function<\/p>\n<p><em>greet()<\/em><\/p>\n<p><strong>Output:<\/strong><\/p>\n<p style=\"text-align: center\"><em>Hello, welcome!<\/em><\/p>\n<p>The function greet() is executed when called, and it prints the message.<\/p>\n<h3>3. Functions with Parameters<\/h3>\n<p>Functions can accept parameters \u2014 values that are passed into the function when it\u2019s called.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<p style=\"text-align: center\"><em>def function_name(parameter1, parameter2):<\/em><br \/>\n<em># code<\/em><\/p>\n<p><strong>Example 3:<\/strong> Function with Parameters<\/p>\n<p style=\"text-align: center\"><em>def greet(name):<\/em><br \/>\n<em>print(f&#8221;Hello, {name}!&#8221;)<\/em><\/p>\n<p><strong>Calling the Function with an Argument<\/strong><\/p>\n<p style=\"text-align: center\"><em>greet(&#8220;Alice&#8221;)<\/em><\/p>\n<p><strong>Output:<\/strong><\/p>\n<p style=\"text-align: center\"><em>Hello, Alice!<\/em><\/p>\n<p>The function greet() accepts a name parameter and prints a personalized greeting.<\/p>\n<h3>4. Functions with Return Values<\/h3>\n<p>Functions can return a value using the return keyword, allowing you to send results back to where the function was called.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<p style=\"text-align: center\"><em>def function_name():<\/em><br \/>\n<em>return value<\/em><\/p>\n<p><strong>Example 4:<\/strong> Function with a Return Value<\/p>\n<p style=\"text-align: center\"><em>def add(a, b):<\/em><br \/>\n<em>return a + b<\/em><\/p>\n<p><strong> Calling the Function and Using the Return Value<\/strong><\/p>\n<p style=\"text-align: center\"><em>result = add(5, 3)<\/em><br \/>\n<em>print(result)<\/em><\/p>\n<p><strong>Output:<\/strong><\/p>\n<p style=\"text-align: center\"><em>8<\/em><\/p>\n<p>The add() function returns the sum of a and b, and the result is stored in the result variable, which is then printed.<\/p>\n<h3>5. Functions with Multiple Parameters and Default Values<\/h3>\n<p>Functions can also have default parameters, meaning that if no value is passed for them, a default value will be used.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<p style=\"text-align: center\"><em>def function_name(param1, param2=default_value):<\/em><br \/>\n<em># code<\/em><\/p>\n<p><strong>Example 5:<\/strong> Function with a Default Parameter<\/p>\n<p style=\"text-align: center\"><em>def greet(name, greeting=&#8221;Hello&#8221;):<\/em><br \/>\n<em>print(f&#8221;{greeting}, {name}!&#8221;)<\/em><\/p>\n<p><strong> Calling the Function with and without Default Parameter<\/strong><\/p>\n<p style=\"text-align: center\"><em>greet(&#8220;Bob&#8221;) # Uses default greeting<\/em><br \/>\n<em>greet(&#8220;Alice&#8221;, &#8220;Good morning&#8221;) # Custom greeting<\/em><\/p>\n<p><strong>Output:<\/strong><\/p>\n<p style=\"text-align: center\"><em>Hello, Bob!<\/em><br \/>\n<em>Good morning, Alice!<\/em><\/p>\n<p><strong>Summary of Key Points:<\/strong><\/p>\n<ul>\n<li>Defining a function: Use def followed by the function name and parameters.<\/li>\n<li>Calling a function: Simply write the function name followed by parentheses.<\/li>\n<li>Parameters: Functions can accept inputs (parameters) to customize their behavior.<\/li>\n<li>Return values: Use return to send results from a function back to the caller.<\/li>\n<li>Default values: Parameters can have default values in case no argument is provided.<\/li>\n<\/ul>\n<p><strong>Example of Multiple Concepts:<\/strong><\/p>\n<p style=\"text-align: center\"><em>def multiply(a, b=2):<\/em><br \/>\n<em>return a * b<\/em><\/p>\n<p style=\"text-align: center\"><em>result1 = multiply(5) # Uses default value for b<\/em><br \/>\n<em>result2 = multiply(5, 3) # Custom value for b<\/em><\/p>\n<p style=\"text-align: center\"><em>print(result1) # Output: 10<\/em><br \/>\n<em>print(result2) # Output: 15<\/em><\/p>\n<h2>Parameters, arguments, and return values<\/h2>\n<h3>1. Parameters<\/h3>\n<p><span style=\"font-size: 1.25rem\">Parameters are variables that are defined in a function\u2019s signature (the function definition). They act as placeholders for values that are passed into the function when it is called.<\/span><\/p>\n<p>Parameters let you write generic, reusable functions that can handle different inputs.<\/p>\n<p><strong>Syntax for Parameters:<\/strong><\/p>\n<p style=\"text-align: center\"><em>def function_name(parameter1, parameter2):<\/em><br \/>\n<em># function body<\/em><\/p>\n<p><strong>Example:<\/strong> Parameters in Action<\/p>\n<p style=\"text-align: center\"><em>def greet(name, greeting=&#8221;Hello&#8221;):<\/em><br \/>\n<em>print(f&#8221;{greeting}, {name}!&#8221;)<\/em><\/p>\n<ul>\n<li>name and greeting are parameters.<\/li>\n<li>greeting has a default value (&#8220;Hello&#8221;), so it&#8217;s optional when calling the function.<\/li>\n<\/ul>\n<p><strong>Key Points:<\/strong><\/p>\n<ul>\n<li>Parameters define the inputs a function requires.<\/li>\n<li>Parameters are used within the function body.<\/li>\n<\/ul>\n<h3>2. Arguments<\/h3>\n<p>Arguments are the actual values that you pass into a function when you call it. The function receives arguments and uses them to perform its tasks.<\/p>\n<p>Arguments are provided in the function call and match the function\u2019s parameters in order.<\/p>\n<p><strong>Syntax for Arguments:<\/strong><\/p>\n<p style=\"text-align: center\"><em>function_name(argument1, argument2)<\/em><\/p>\n<p><strong>Example:<\/strong> Passing Arguments<\/p>\n<p style=\"text-align: center\"><em>greet(&#8220;Alice&#8221;, &#8220;Good morning&#8221;)<\/em><\/p>\n<ul>\n<li>&#8220;Alice&#8221; and &#8220;Good morning&#8221; are arguments.<\/li>\n<li>&#8220;Alice&#8221; is passed to the name parameter, and &#8220;Good morning&#8221; is passed to the greeting parameter.<\/li>\n<\/ul>\n<p><strong>Key Points:<\/strong><\/p>\n<ul>\n<li>Arguments are the real values passed to the function when it is called.<\/li>\n<li>Arguments replace parameters with actual data.<\/li>\n<\/ul>\n<h3>3. Return Values<\/h3>\n<p>A return value is the result that a function sends back after it completes its task. A function can return any kind of value \u2014 numbers, strings, lists, etc.<\/p>\n<p>The return keyword is used to send a value back from a function. Once return is executed, the function exits, and the returned value can be used or stored outside of the function.<\/p>\n<p><strong>Syntax for Return Values:<\/strong><\/p>\n<p style=\"text-align: center\"><em>def function_name():<\/em><br \/>\n<em>return value<\/em><\/p>\n<p><strong>Example:<\/strong> Function with Return Value<\/p>\n<p style=\"text-align: center\"><em>def add(a, b):<\/em><br \/>\n<em>return a + b<\/em><\/p>\n<ul>\n<li>The add() function returns the sum of a and b.<\/li>\n<li>When the function finishes, it sends the result back to the caller.<\/li>\n<\/ul>\n<p><strong>Key Points:<\/strong><\/p>\n<ul>\n<li>A function can return a value using return.<\/li>\n<li>The function stops executing after a return statement.<\/li>\n<\/ul>\n<p><strong>Example of Using Parameters, Arguments, and Return Value Together:<\/strong><\/p>\n<p style=\"text-align: center\"><em>def multiply(a, b=1):<\/em><br \/>\n<em>return a * b<\/em><\/p>\n<p style=\"text-align: center\"><em># Calling the function with arguments:<\/em><br \/>\n<em>result1 = multiply(5) # Uses default b=1<\/em><br \/>\n<em>result2 = multiply(5, 3) # Custom b=3<\/em><\/p>\n<p style=\"text-align: center\"><em>print(result1) # Output: 5 (5 * 1)<\/em><br \/>\n<em>print(result2) # Output: 15 (5 * 3)<\/em><\/p>\n<ul>\n<li>Parameters: a and b are defined in the function signature.<\/li>\n<li>Arguments: 5 and 3 are passed when calling the function.<\/li>\n<li>Return Value: The result of a * b is returned and stored in result1 and result2.<\/li>\n<\/ul>\n<p><strong>Key Takeaways:<\/strong><\/p>\n<ul>\n<li>Parameters: Variables defined in the function to receive inputs.<\/li>\n<li>Arguments: Values passed to the function when calling it (matching parameters).<\/li>\n<li>Return Values: The result that the function sends back after completing its task, allowing you to use the output outside the function.<\/li>\n<\/ul>\n<h2>Variable scope<\/h2>\n<h3>What is Variable Scope?<\/h3>\n<p>Variable scope refers to the region of a program where a variable can be accessed. In Python, the scope is influenced by where the variable is declared and where it is used.<\/p>\n<p><strong>Types of Scope in Python<\/strong><\/p>\n<ul>\n<li>Local Scope<\/li>\n<li>Enclosing Scope<\/li>\n<li>Global Scope<\/li>\n<li>Built-in Scope<\/li>\n<\/ul>\n<h3>1. Local Scope<\/h3>\n<p>A variable has local scope if it is defined inside a function or a block of code. This means it is only accessible within that function or block.<\/p>\n<ul>\n<li>Local variables are created when the function is called and destroyed when the function finishes execution.<\/li>\n<\/ul>\n<p><strong>Example of Local Scope:<\/strong><\/p>\n<p style=\"text-align: center\"><em>def my_function():<\/em><br \/>\n<em>x = 10 # x is local to my_function<\/em><br \/>\n<em>print(x)<\/em><\/p>\n<p style=\"text-align: center\"><em>my_function() # Output: 10<\/em><br \/>\n<em># print(x) # This would cause an error because x is local to my_function<\/em><\/p>\n<p>In the example, x is local to my_function(), and trying to access x outside the function will raise a NameError.<\/p>\n<h3>2. Enclosing Scope (Nonlocal)<\/h3>\n<p>Enclosing scope refers to the scope of a function within another function. Variables defined in the outer (enclosing) function are accessible to the inner (nested) function.<\/p>\n<ul>\n<li>These variables are not local to the inner function but can be modified using the nonlocal keyword.<\/li>\n<\/ul>\n<p><strong>Example of Enclosing Scope:<\/strong><\/p>\n<p style=\"text-align: center\"><em>def outer_function():<\/em><br \/>\n<em>x = 20 # x is in the enclosing scope<\/em><br \/>\n<em>def inner_function():<\/em><br \/>\n<em>print(x) # Can access x from the enclosing function<\/em><br \/>\n<em>inner_function()<\/em><\/p>\n<p style=\"text-align: center\"><em>outer_function() # Output: 20<\/em><\/p>\n<p>Here, inner_function() can access x from the outer_function() because x is in an enclosing scope.<\/p>\n<h3>3. Global Scope<\/h3>\n<p>A variable has global scope if it is defined at the top level of the script or module (outside any function). Global variables can be accessed from any part of the program, including inside functions (but not always modified directly without the global keyword).<\/p>\n<ul>\n<li>Global variables exist throughout the life of the program.<\/li>\n<\/ul>\n<p><strong>Example of Global Scope:<\/strong><\/p>\n<p style=\"text-align: center\"><em>x = 50 # x is a global variable<\/em><\/p>\n<p style=\"text-align: center\"><em>def my_function():<\/em><br \/>\n<em>print(x) # Can access global x inside the function<\/em><\/p>\n<p style=\"text-align: center\"><em>my_function() # Output: 50<\/em><br \/>\n<em>print(x) # Output: 50<\/em><\/p>\n<p>In this example, x is defined in the global scope and can be accessed inside the function my_function().<\/p>\n<p><strong>Modifying a Global Variable:<\/strong><\/p>\n<p>To modify a global variable inside a function, use the global keyword:<\/p>\n<p style=\"text-align: center\"><em>x = 50 # Global variable<\/em><\/p>\n<p style=\"text-align: center\"><em>def my_function():<\/em><br \/>\n<em>global x<\/em><br \/>\n<em>x = 100 # Modifies the global x<\/em><\/p>\n<p style=\"text-align: center\"><em>my_function()<\/em><br \/>\n<em>print(x) # Output: 100<\/em><\/p>\n<p>Without the global keyword, trying to assign to x inside the function would create a local variable x instead of modifying the global one.<\/p>\n<h3>4. Built-in Scope<\/h3>\n<p>Built-in scope refers to the names that are available in Python\u2019s built-in namespace, which includes functions like print(), len(), sum(), and more. These names are available throughout the entire program.<\/p>\n<ul>\n<li>Built-in variables and functions are part of Python&#8217;s core language and are always accessible.<\/li>\n<\/ul>\n<p><strong>Example of Built-in Scope:<\/strong><\/p>\n<p style=\"text-align: center\"><em>print(len(&#8220;Hello&#8221;)) # &#8216;len&#8217; is a built-in function<\/em><\/p>\n<p>In this example, print() and len() are built-in functions, available globally without needing to be imported or defined.<\/p>\n<p><strong>LEGB Rule (Local, Enclosing, Global, Built-in)<\/strong><\/p>\n<p>When you reference a variable, Python follows the LEGB rule to look for it:<\/p>\n<ul>\n<li>Local: First, Python looks for the variable in the local function scope.<\/li>\n<li>Enclosing: If it&#8217;s not found, Python checks in any enclosing functions (from outer to inner).<\/li>\n<li>Global: If it\u2019s not found in enclosing scopes, Python searches for the variable in the global scope.<\/li>\n<li>Built-in: If the variable is still not found, Python checks the built-in scope for the variable.<\/li>\n<\/ul>\n<p><strong>Example of LEGB Rule:<\/strong><\/p>\n<p style=\"text-align: center\"><em>x = 5 # Global variable<\/em><\/p>\n<p style=\"text-align: center\"><em>def outer_function():<\/em><br \/>\n<em>x = 10 # Enclosing variable<\/em><br \/>\n<em>def inner_function():<\/em><br \/>\n<em>x = 15 # Local variable<\/em><br \/>\n<em>print(x) # Prints the local x, not global or enclosing<\/em><br \/>\n<em>inner_function()<\/em><\/p>\n<p style=\"text-align: center\"><em>outer_function() # Output: 15<\/em><br \/>\n<em>print(x) # Output: 5 (global x remains unaffected)<\/em><\/p>\n<p>Here, inside inner_function(), Python uses the local x because it\u2019s closest. The enclosing and global x are not used.<\/p>\n<p><strong>Modifying Variables in Different Scopes<\/strong><\/p>\n<ul>\n<li>Local variable: Can be modified directly within the function.<\/li>\n<li>Enclosing variable: Can be modified using the nonlocal keyword.<\/li>\n<li>Global variable: Can be modified using the global keyword.<\/li>\n<\/ul>\n<p><strong>Key Takeaways:<\/strong><\/p>\n<ul>\n<li>Local scope: Variables are accessible only inside the function where they are defined.<\/li>\n<li>Enclosing scope: Variables are accessible in nested functions.<\/li>\n<li>Global scope: Variables are accessible throughout the program.<\/li>\n<li>Built-in scope: Python&#8217;s core functions and variables.<\/li>\n<li>The LEGB rule determines where Python looks for a variable<\/li>\n<\/ul>\n<h2>Lambda functions<\/h2>\n<h3>What Is a Lambda Function?<\/h3>\n<p>A lambda function is a small, anonymous function defined using the lambda keyword instead of def. It is used to create throwaway functions \u2014 typically for short, simple operations \u2014 without naming them.<\/p>\n<p><strong>Syntax of a Lambda Function:<\/strong><\/p>\n<p style=\"text-align: center\"><em>lambda arguments: expression<\/em><\/p>\n<ul>\n<li>lambda: Keyword that defines the function.<\/li>\n<li>arguments: Input parameters (can be zero or more).<\/li>\n<li>expression: A single expression to evaluate and return.<\/li>\n<\/ul>\n<p><strong>Note:<\/strong> The body of a lambda must be a single expression, not multiple lines or statements.<\/p>\n<p><strong>Example 1:<\/strong> Basic Lambda<\/p>\n<p style=\"text-align: center\"><em>square = lambda x: x * x<\/em><br \/>\n<em>print(square(4)) # Output: 16<\/em><\/p>\n<p>This defines a lambda that returns the square of x. It behaves like:<\/p>\n<p style=\"text-align: center\"><em>def square(x):<\/em><br \/>\n<em>return x * x<\/em><\/p>\n<p><strong>Example 2:<\/strong> Lambda with Multiple Arguments<\/p>\n<p style=\"text-align: center\"><em>add = lambda a, b: a + b<\/em><br \/>\n<em>print(add(3, 5)) # Output: 8<\/em><\/p>\n<p><strong>Example 3:<\/strong> Lambda with No Parameters<\/p>\n<p style=\"text-align: center\"><em>greet = lambda: &#8220;Hello!&#8221;<\/em><br \/>\n<em>print(greet()) # Output: Hello!<\/em><\/p>\n<p><strong>Lambda vs Regular Functions:<\/strong><\/p>\n<table style=\"width: 100%;border-collapse: collapse;font-family: Arial, sans-serif;margin: 20px 0\">\n<thead>\n<tr>\n<th style=\"padding: 12px;text-align: left;border-bottom: 2px solid #000;font-weight: bold\"><strong>Feature<\/strong><\/th>\n<th style=\"padding: 12px;text-align: left;border-bottom: 2px solid #000;font-weight: bold\"><strong>def Function<\/strong><\/th>\n<th style=\"padding: 12px;text-align: left;border-bottom: 2px solid #000;font-weight: bold\"><strong>Lambda Function<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\">Name<\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\">Yes (named function)<\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\">No (anonymous unless assigned to variable)<\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\">Multi-line support<\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\">Yes (multiple statements)<\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\">No (single expression only)<\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\">Use cases<\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\">Reusable, complex logic<\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\">One-time, short operations<\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\">Return<\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\">Explicit <code>return<\/code> statement<\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\">Implicit return of expression result<\/td>\n<\/tr>\n<tr>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\">Best for<\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\">Functions called multiple times<\/td>\n<td style=\"padding: 10px;border-bottom: 1px solid #ddd\">Simple operations passed as arguments<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Where Are Lambda Functions Commonly Used?<\/strong><\/p>\n<p>Lambda functions are often used with other functions that accept functions as arguments (like map(), filter(), sorted(), etc.).<\/p>\n<p><strong>Example 4:<\/strong> Using lambda with map()<\/p>\n<p style=\"text-align: center\"><em>nums = [1, 2, 3, 4]<\/em><br \/>\n<em>squares = list(map(lambda x: x**2, nums))<\/em><br \/>\n<em>print(squares) # Output: [1, 4, 9, 16]<\/em><\/p>\n<p><strong> Example 5:<\/strong> Using lambda with filter()<\/p>\n<p style=\"text-align: center\"><em>nums = [1, 2, 3, 4, 5]<\/em><br \/>\n<em>evens = list(filter(lambda x: x % 2 == 0, nums))<\/em><br \/>\n<em>print(evens) # Output: [2, 4]<\/em><\/p>\n<p><strong>Example 6:<\/strong> Sorting with Lambda<\/p>\n<p style=\"text-align: center\"><em>people = [(&#8220;Alice&#8221;, 30), (&#8220;Bob&#8221;, 25), (&#8220;Eve&#8221;, 35)]<\/em><br \/>\n<em>sorted_people = sorted(people, key=lambda person: person[1])<\/em><br \/>\n<em>print(sorted_people) # Sorted by age<\/em><br \/>\n<em># Output: [(&#8216;Bob&#8217;, 25), (&#8216;Alice&#8217;, 30), (&#8216;Eve&#8217;, 35)]<\/em><\/p>\n<p><strong>When to Use Lambda Functions<\/strong><\/p>\n<p>Use a lambda when:<\/p>\n<ul>\n<li>You need a small, quick function.<\/li>\n<li>It\u2019s used only once or in a limited scope.<\/li>\n<li>You\u2019re passing it as an argument to a higher-order function.<\/li>\n<\/ul>\n<p>Avoid lambda when:<\/p>\n<ul>\n<li>The function is complex or involves multiple steps.<\/li>\n<li>You need to reuse or debug it.<\/li>\n<\/ul>\n<p><strong>Summary:<\/strong><\/p>\n<ul>\n<li>Lambda functions are anonymous, one-line functions.<\/li>\n<li>Use lambda args: expression.<\/li>\n<li>Great for quick tasks, especially with map(), filter(), sorted(), etc.<\/li>\n<li>They simplify short, functional-style code but should not replace full def functions for more complex logic.<\/li>\n<\/ul>\n<p>Want to explore lambda with custom sorting, combining with list comprehensions, or higher-order functions like reduce()? Just say the word!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Defining and calling functions What Are Functions? A function is a block of reusable code that performs a specific task. You define functions to avoid repeating code, enhance readability, and make your code more modular. 1. Defining a Function To define a function, you use<\/p>\n","protected":false},"author":1,"featured_media":768,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-451","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Functions - Python Course<\/title>\n<meta name=\"description\" content=\"Functions are reusable blocks of code that perform specific tasks when called; they help organize code and avoid repetition.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/buhave.com\/courses\/python\/functions\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Functions - Python Course\" \/>\n<meta property=\"og:description\" content=\"Functions are reusable blocks of code that perform specific tasks when called; they help organize code and avoid repetition.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/buhave.com\/courses\/python\/functions\/\" \/>\n<meta property=\"og:site_name\" content=\"BUHAVE\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/BeYouHave\/\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/naveedsafdarawan\/\" \/>\n<meta property=\"article:published_time\" content=\"2025-05-20T04:51:26+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-05-20T12:56:49+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/Functions.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"Naveed Safdar\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Naveed Safdar\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/functions\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/functions\\\/\"},\"author\":{\"name\":\"Naveed Safdar\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#\\\/schema\\\/person\\\/04fe0254e118521c9fbb3da39de5acca\"},\"headline\":\"Functions\",\"datePublished\":\"2025-05-20T04:51:26+00:00\",\"dateModified\":\"2025-05-20T12:56:49+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/functions\\\/\"},\"wordCount\":2011,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/functions\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/04\\\/Functions.webp\",\"articleSection\":[\"Python Course\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/functions\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/functions\\\/\",\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/functions\\\/\",\"name\":\"Functions - Python Course\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/functions\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/functions\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/04\\\/Functions.webp\",\"datePublished\":\"2025-05-20T04:51:26+00:00\",\"dateModified\":\"2025-05-20T12:56:49+00:00\",\"description\":\"Functions are reusable blocks of code that perform specific tasks when called; they help organize code and avoid repetition.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/functions\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/functions\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/functions\\\/#primaryimage\",\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/04\\\/Functions.webp\",\"contentUrl\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/04\\\/Functions.webp\",\"width\":1200,\"height\":628,\"caption\":\"Functions\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/python\\\/functions\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Courses\",\"item\":\"https:\\\/\\\/buhave.com\\\/courses\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Course\",\"item\":\"https:\\\/\\\/buhave.com\\\/courses\\\/learn\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Functions\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#website\",\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/\",\"name\":\"BUHAVE\",\"description\":\"Courses - Learn Online for Free\",\"publisher\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/buhave.com\\\/courses\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#organization\",\"name\":\"BUHAVE\",\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/03\\\/buhave-course.webp\",\"contentUrl\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/03\\\/buhave-course.webp\",\"width\":375,\"height\":75,\"caption\":\"BUHAVE\"},\"image\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/BeYouHave\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/company\\\/buhave\",\"https:\\\/\\\/www.youtube.com\\\/@buhave\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#\\\/schema\\\/person\\\/04fe0254e118521c9fbb3da39de5acca\",\"name\":\"Naveed Safdar\",\"description\":\"I\u2019m Naveed Safdar - SEO Manager with over 10 years of experience in SEO and Digital Marketing. I\u2019ve had the privilege of working with leading national and international companies including Grafdom, PakWheels, Systems Limited, Confiz, Educative, and Dubizzle Labs. My expertise spans technical SEO, content strategy, organic growth, and performance analytics - helping businesses improve visibility, traffic, and ROI.\",\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/in\\\/naveedsafdar\\\/\",\"https:\\\/\\\/www.facebook.com\\\/naveedsafdarawan\\\/\",\"https:\\\/\\\/www.youtube.com\\\/@naveedsafdar\"],\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/author\\\/naveed-safdar\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Functions - Python Course","description":"Functions are reusable blocks of code that perform specific tasks when called; they help organize code and avoid repetition.","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:\/\/buhave.com\/courses\/python\/functions\/","og_locale":"en_US","og_type":"article","og_title":"Functions - Python Course","og_description":"Functions are reusable blocks of code that perform specific tasks when called; they help organize code and avoid repetition.","og_url":"https:\/\/buhave.com\/courses\/python\/functions\/","og_site_name":"BUHAVE","article_publisher":"https:\/\/www.facebook.com\/BeYouHave\/","article_author":"https:\/\/www.facebook.com\/naveedsafdarawan\/","article_published_time":"2025-05-20T04:51:26+00:00","article_modified_time":"2025-05-20T12:56:49+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/Functions.webp","type":"image\/webp"}],"author":"Naveed Safdar","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Naveed Safdar","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/buhave.com\/courses\/python\/functions\/#article","isPartOf":{"@id":"https:\/\/buhave.com\/courses\/python\/functions\/"},"author":{"name":"Naveed Safdar","@id":"https:\/\/buhave.com\/courses\/#\/schema\/person\/04fe0254e118521c9fbb3da39de5acca"},"headline":"Functions","datePublished":"2025-05-20T04:51:26+00:00","dateModified":"2025-05-20T12:56:49+00:00","mainEntityOfPage":{"@id":"https:\/\/buhave.com\/courses\/python\/functions\/"},"wordCount":2011,"commentCount":0,"publisher":{"@id":"https:\/\/buhave.com\/courses\/#organization"},"image":{"@id":"https:\/\/buhave.com\/courses\/python\/functions\/#primaryimage"},"thumbnailUrl":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/Functions.webp","articleSection":["Python Course"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/buhave.com\/courses\/python\/functions\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/buhave.com\/courses\/python\/functions\/","url":"https:\/\/buhave.com\/courses\/python\/functions\/","name":"Functions - Python Course","isPartOf":{"@id":"https:\/\/buhave.com\/courses\/#website"},"primaryImageOfPage":{"@id":"https:\/\/buhave.com\/courses\/python\/functions\/#primaryimage"},"image":{"@id":"https:\/\/buhave.com\/courses\/python\/functions\/#primaryimage"},"thumbnailUrl":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/Functions.webp","datePublished":"2025-05-20T04:51:26+00:00","dateModified":"2025-05-20T12:56:49+00:00","description":"Functions are reusable blocks of code that perform specific tasks when called; they help organize code and avoid repetition.","breadcrumb":{"@id":"https:\/\/buhave.com\/courses\/python\/functions\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/buhave.com\/courses\/python\/functions\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/buhave.com\/courses\/python\/functions\/#primaryimage","url":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/Functions.webp","contentUrl":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/04\/Functions.webp","width":1200,"height":628,"caption":"Functions"},{"@type":"BreadcrumbList","@id":"https:\/\/buhave.com\/courses\/python\/functions\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Courses","item":"https:\/\/buhave.com\/courses\/"},{"@type":"ListItem","position":2,"name":"Python Course","item":"https:\/\/buhave.com\/courses\/learn\/python\/"},{"@type":"ListItem","position":3,"name":"Functions"}]},{"@type":"WebSite","@id":"https:\/\/buhave.com\/courses\/#website","url":"https:\/\/buhave.com\/courses\/","name":"BUHAVE","description":"Courses - Learn Online for Free","publisher":{"@id":"https:\/\/buhave.com\/courses\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/buhave.com\/courses\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/buhave.com\/courses\/#organization","name":"BUHAVE","url":"https:\/\/buhave.com\/courses\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/buhave.com\/courses\/#\/schema\/logo\/image\/","url":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/03\/buhave-course.webp","contentUrl":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/03\/buhave-course.webp","width":375,"height":75,"caption":"BUHAVE"},"image":{"@id":"https:\/\/buhave.com\/courses\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/BeYouHave\/","https:\/\/www.linkedin.com\/company\/buhave","https:\/\/www.youtube.com\/@buhave"]},{"@type":"Person","@id":"https:\/\/buhave.com\/courses\/#\/schema\/person\/04fe0254e118521c9fbb3da39de5acca","name":"Naveed Safdar","description":"I\u2019m Naveed Safdar - SEO Manager with over 10 years of experience in SEO and Digital Marketing. I\u2019ve had the privilege of working with leading national and international companies including Grafdom, PakWheels, Systems Limited, Confiz, Educative, and Dubizzle Labs. My expertise spans technical SEO, content strategy, organic growth, and performance analytics - helping businesses improve visibility, traffic, and ROI.","sameAs":["https:\/\/www.linkedin.com\/in\/naveedsafdar\/","https:\/\/www.facebook.com\/naveedsafdarawan\/","https:\/\/www.youtube.com\/@naveedsafdar"],"url":"https:\/\/buhave.com\/courses\/author\/naveed-safdar\/"}]}},"_links":{"self":[{"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/posts\/451","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/comments?post=451"}],"version-history":[{"count":1,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/posts\/451\/revisions"}],"predecessor-version":[{"id":452,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/posts\/451\/revisions\/452"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/media\/768"}],"wp:attachment":[{"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/media?parent=451"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/categories?post=451"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/tags?post=451"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}