{"id":3433,"date":"2026-08-24T09:55:47","date_gmt":"2026-08-24T09:55:47","guid":{"rendered":"https:\/\/us.allassignmentsupport.com\/blog\/?p=3433"},"modified":"2026-08-24T10:39:24","modified_gmt":"2026-08-24T10:39:24","slug":"python-functions-explained-parameters-args-kwargs-and-return-values","status":"publish","type":"post","link":"https:\/\/us.allassignmentsupport.com\/blog\/python-functions-explained-parameters-args-kwargs-and-return-values\/","title":{"rendered":"Python Functions Explained: Parameters, *args, **kwargs, and Return Values"},"content":{"rendered":"<p dir=\"ltr\">Functions are usually introduced early in a Python course as &#8220;reusable blocks of code,&#8221; which makes them sound simple \u2014 and the basic syntax is. What actually derails students is everything that shows up a few weeks later: the difference between positional and keyword arguments, what <code>*args<\/code> and <code>**kwargs<\/code> actually do, why a variable defined inside a function disappears the moment the function ends, and why <code>return<\/code> and <code>print<\/code> are not interchangeable, despite often looking like they produce the same output. This guide works through all of it, in the order these confusions tend to appear, with fully worked examples at every step.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_69_1 counter-hierarchy ez-toc-counter ez-toc-light-blue ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title \" >Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/us.allassignmentsupport.com\/blog\/python-functions-explained-parameters-args-kwargs-and-return-values\/#The_Absolute_Basics_Defining_and_Calling_a_Function\" title=\"The Absolute Basics: Defining and Calling a Function\">The Absolute Basics: Defining and Calling a Function<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/us.allassignmentsupport.com\/blog\/python-functions-explained-parameters-args-kwargs-and-return-values\/#return_vs_print_The_Single_Most_Common_Beginner_Confusion\" title=\"return vs. print: The Single Most Common Beginner Confusion\">return vs. print: The Single Most Common Beginner Confusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/us.allassignmentsupport.com\/blog\/python-functions-explained-parameters-args-kwargs-and-return-values\/#Positional_vs_Keyword_Arguments\" title=\"Positional vs. Keyword Arguments\">Positional vs. Keyword Arguments<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/us.allassignmentsupport.com\/blog\/python-functions-explained-parameters-args-kwargs-and-return-values\/#Default_Parameter_Values\" title=\"Default Parameter Values\">Default Parameter Values<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/us.allassignmentsupport.com\/blog\/python-functions-explained-parameters-args-kwargs-and-return-values\/#args_Accepting_Any_Number_of_Positional_Arguments\" title=\"*args: Accepting Any Number of Positional Arguments\">*args: Accepting Any Number of Positional Arguments<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/us.allassignmentsupport.com\/blog\/python-functions-explained-parameters-args-kwargs-and-return-values\/#kwargs_Accepting_Any_Number_of_Keyword_Arguments\" title=\"**kwargs: Accepting Any Number of Keyword Arguments\">**kwargs: Accepting Any Number of Keyword Arguments<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/us.allassignmentsupport.com\/blog\/python-functions-explained-parameters-args-kwargs-and-return-values\/#Variable_Scope_Why_Your_Variable_%E2%80%9CDisappears%E2%80%9D_After_a_Function_Runs\" title=\"Variable Scope: Why Your Variable &#8220;Disappears&#8221; After a Function Runs\">Variable Scope: Why Your Variable &#8220;Disappears&#8221; After a Function Runs<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/us.allassignmentsupport.com\/blog\/python-functions-explained-parameters-args-kwargs-and-return-values\/#Returning_Multiple_Values\" title=\"Returning Multiple Values\">Returning Multiple Values<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/us.allassignmentsupport.com\/blog\/python-functions-explained-parameters-args-kwargs-and-return-values\/#A_Step-by-Step_Checklist_for_Students_Stuck_on_a_Functions_Assignment\" title=\"A Step-by-Step Checklist for Students Stuck on a Functions Assignment\">A Step-by-Step Checklist for Students Stuck on a Functions Assignment<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/us.allassignmentsupport.com\/blog\/python-functions-explained-parameters-args-kwargs-and-return-values\/#FAQs\" title=\"FAQs\">FAQs<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/us.allassignmentsupport.com\/blog\/python-functions-explained-parameters-args-kwargs-and-return-values\/#Related_Reading_Woven_Into_This_Guide\" title=\"Related Reading Woven Into This Guide\">Related Reading Woven Into This Guide<\/a><\/li><\/ul><\/nav><\/div>\n<h2 dir=\"ltr\"><span class=\"ez-toc-section\" id=\"The_Absolute_Basics_Defining_and_Calling_a_Function\"><\/span>The Absolute Basics: Defining and Calling a Function<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<div tabindex=\"0\" role=\"group\" aria-label=\"python code\">\n<div>\n<div><\/div>\n<\/div>\n<div>python<\/div>\n<div>\n<pre><code class=\"language-python\">def greet(name):\r\n    return f\"Hello, {name}!\"\r\n\r\nmessage = greet(\"Amara\")\r\nprint(message)  # Hello, Amara!<\/code><\/pre>\n<\/div>\n<\/div>\n<p dir=\"ltr\">Three parts matter here: the <code>def<\/code> keyword starts the definition, <code>name<\/code> is a <strong>parameter<\/strong> (a placeholder the function expects), and <code>\"Amara\"<\/code> is the <strong>argument<\/strong> (the actual value supplied when the function is called). Students often use &#8220;parameter&#8221; and &#8220;argument&#8221; interchangeably in casual conversation, but assignments \u2014 especially ones asking you to explain your code \u2014 sometimes specifically test whether you know parameters belong to the function definition and arguments belong to the function call.<\/p>\n<h2 dir=\"ltr\"><span class=\"ez-toc-section\" id=\"return_vs_print_The_Single_Most_Common_Beginner_Confusion\"><\/span>return vs. print: The Single Most Common Beginner Confusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p dir=\"ltr\">This is worth addressing before anything else, because it causes downstream bugs in almost every other topic in this guide.<\/p>\n<div tabindex=\"0\" role=\"group\" aria-label=\"python code\">\n<div>\n<div><\/div>\n<\/div>\n<div>python<\/div>\n<div>\n<pre><code class=\"language-python\">def add_v1(a, b):\r\n    print(a + b)\r\n\r\ndef add_v2(a, b):\r\n    return a + b\r\n\r\nresult1 = add_v1(3, 4)   # prints 7, but result1 is None\r\nresult2 = add_v2(3, 4)   # prints nothing, but result2 is 7\r\n\r\nprint(result1)  # None\r\nprint(result2)  # 7<\/code><\/pre>\n<\/div>\n<\/div>\n<p dir=\"ltr\"><strong>What&#8217;s actually happening:<\/strong> <code>print()<\/code> displays a value on the screen and hands nothing back to the code that called the function \u2014 the function&#8217;s actual return value defaults to <code>None<\/code> if there&#8217;s no explicit <code>return<\/code> statement. <code>return<\/code> sends a value back to wherever the function was called, so it can be stored in a variable, used in a calculation, or passed into another function.<\/p>\n<p dir=\"ltr\"><strong>Common assignment mistake:<\/strong> Writing a function that prints a calculated value, then trying to use that function&#8217;s output in a later calculation (<code>total = add_v1(3, 4) + 10<\/code>), which fails silently or throws a <code>TypeError<\/code> because <code>add_v1(3, 4)<\/code> actually evaluates to <code>None<\/code>, not <code>7<\/code>. If your function&#8217;s result needs to be used anywhere else in your program, it must use <code>return<\/code>, not just <code>print<\/code>.<\/p>\n<h2 dir=\"ltr\"><span class=\"ez-toc-section\" id=\"Positional_vs_Keyword_Arguments\"><\/span>Positional vs. Keyword Arguments<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<div tabindex=\"0\" role=\"group\" aria-label=\"python code\">\n<div>\n<div><\/div>\n<\/div>\n<div>python<\/div>\n<div>\n<pre><code class=\"language-python\">def describe_student(name, age, major):\r\n    return f\"{name} is {age} years old, studying {major}.\"\r\n\r\n# Positional arguments \u2014 order matters\r\nprint(describe_student(\"Kai\", 20, \"Physics\"))\r\n\r\n# Keyword arguments \u2014 order doesn't matter, since names are specified\r\nprint(describe_student(major=\"Physics\", name=\"Kai\", age=20))\r\n\r\n# Mixing both \u2014 positional arguments must come first\r\nprint(describe_student(\"Kai\", major=\"Physics\", age=20))<\/code><\/pre>\n<\/div>\n<\/div>\n<p dir=\"ltr\"><strong>Common assignment mistake:<\/strong> Trying to place a positional argument <em>after<\/em> a keyword argument, such as <code>describe_student(name=\"Kai\", 20, \"Physics\")<\/code>, which raises a <code>SyntaxError<\/code>. Python requires all positional arguments to appear before any keyword arguments in a function call \u2014 once you start naming arguments, every argument after that point must also be named.<\/p>\n<h2 dir=\"ltr\"><span class=\"ez-toc-section\" id=\"Default_Parameter_Values\"><\/span>Default Parameter Values<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<div tabindex=\"0\" role=\"group\" aria-label=\"python code\">\n<div>\n<div><\/div>\n<\/div>\n<div>python<\/div>\n<div>\n<pre><code class=\"language-python\">def describe_student(name, age, major=\"Undeclared\"):\r\n    return f\"{name} is {age} years old, studying {major}.\"\r\n\r\nprint(describe_student(\"Priya\", 19))              # Priya is 19 years old, studying Undeclared.\r\nprint(describe_student(\"Priya\", 19, \"Chemistry\"))  # Priya is 19 years old, studying Chemistry.<\/code><\/pre>\n<\/div>\n<\/div>\n<p dir=\"ltr\">Default values let a caller omit an argument, in which case the specified default is used instead. This is genuinely useful for optional settings, but has one famous trap that shows up constantly in assignments.<\/p>\n<p dir=\"ltr\"><strong>The mutable default argument trap (a classic assignment &#8220;gotcha&#8221;):<\/strong><\/p>\n<div tabindex=\"0\" role=\"group\" aria-label=\"python code\">\n<div>\n<div><\/div>\n<\/div>\n<div>python<\/div>\n<div>\n<pre><code class=\"language-python\">def add_item(item, cart=[]):  # DANGEROUS: mutable default argument\r\n    cart.append(item)\r\n    return cart\r\n\r\nprint(add_item(\"apple\"))   # ['apple']\r\nprint(add_item(\"banana\"))  # ['apple', 'banana']  &lt;- unexpected! Not a fresh list.<\/code><\/pre>\n<\/div>\n<\/div>\n<p dir=\"ltr\"><strong>What&#8217;s happening:<\/strong> Default argument values are evaluated only <em>once<\/em>, when the function is defined \u2014 not every time the function is called. Since <code>cart=[]<\/code> creates one single list object at definition time, every call that doesn&#8217;t supply its own <code>cart<\/code> argument shares and mutates that <em>same<\/em> list, rather than getting a fresh empty list each time.<\/p>\n<p dir=\"ltr\"><strong>The fix \u2014 use <code>None<\/code> as a sentinel default:<\/strong><\/p>\n<div tabindex=\"0\" role=\"group\" aria-label=\"python code\">\n<div>\n<div><\/div>\n<\/div>\n<div>python<\/div>\n<div>\n<pre><code class=\"language-python\">def add_item(item, cart=None):\r\n    if cart is None:\r\n        cart = []\r\n    cart.append(item)\r\n    return cart<\/code><\/pre>\n<\/div>\n<\/div>\n<p dir=\"ltr\"><strong>Why this matters for assignments:<\/strong> This exact bug appears constantly in intro Python courses because it looks completely reasonable at first glance, and the resulting behavior is genuinely surprising even to careful students. If your code involves a default list, dictionary, or set argument, always use the <code>None<\/code> sentinel pattern shown above.<\/p>\n<h2 dir=\"ltr\"><span class=\"ez-toc-section\" id=\"args_Accepting_Any_Number_of_Positional_Arguments\"><\/span>*args: Accepting Any Number of Positional Arguments<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<div tabindex=\"0\" role=\"group\" aria-label=\"python code\">\n<div>\n<div><\/div>\n<\/div>\n<div>python<\/div>\n<div>\n<pre><code class=\"language-python\">def total_score(*scores):\r\n    return sum(scores)\r\n\r\nprint(total_score(85, 90, 78))         # 253\r\nprint(total_score(85, 90, 78, 92, 88)) # 433<\/code><\/pre>\n<\/div>\n<\/div>\n<p dir=\"ltr\"><code>*args<\/code> collects any number of positional arguments into a <strong>tuple<\/strong> inside the function. The name <code>args<\/code> is just convention \u2014 the <code>*<\/code> is what matters syntactically, not the word &#8220;args&#8221; itself.<\/p>\n<p dir=\"ltr\"><strong>Worked example showing what&#8217;s actually happening under the hood:<\/strong><\/p>\n<div tabindex=\"0\" role=\"group\" aria-label=\"python code\">\n<div>\n<div><\/div>\n<\/div>\n<div>python<\/div>\n<div>\n<pre><code class=\"language-python\">def show_args(*args):\r\n    print(type(args))  # &lt;class 'tuple'&gt;\r\n    print(args)\r\n\r\nshow_args(1, 2, 3)  # (1, 2, 3)<\/code><\/pre>\n<\/div>\n<\/div>\n<p dir=\"ltr\"><strong>Common assignment mistake:<\/strong> Trying to access <code>*args<\/code> by index the way you would a regular parameter, like <code>scores[0]<\/code> when the function was defined without <code>*<\/code>, or forgetting that <code>*args<\/code> must come after all regular positional parameters in a function&#8217;s definition, never before.<\/p>\n<h2 dir=\"ltr\"><span class=\"ez-toc-section\" id=\"kwargs_Accepting_Any_Number_of_Keyword_Arguments\"><\/span>**kwargs: Accepting Any Number of Keyword Arguments<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<div tabindex=\"0\" role=\"group\" aria-label=\"python code\">\n<div>\n<div><\/div>\n<\/div>\n<div>python<\/div>\n<div>\n<pre><code class=\"language-python\">def student_profile(**details):\r\n    for key, value in details.items():\r\n        print(f\"{key}: {value}\")\r\n\r\nstudent_profile(name=\"Leo\", age=21, major=\"Biology\")<\/code><\/pre>\n<\/div>\n<\/div>\n<p dir=\"ltr\"><strong>Output:<\/strong><\/p>\n<div tabindex=\"0\" role=\"group\" aria-label=\"Code\">\n<div>\n<div><\/div>\n<\/div>\n<div>\n<pre><code>name: Leo\r\nage: 21\r\nmajor: Biology<\/code><\/pre>\n<\/div>\n<\/div>\n<p dir=\"ltr\"><code>**kwargs<\/code> collects any number of keyword arguments into a <strong>dictionary<\/strong> inside the function, where each argument&#8217;s name becomes a key and its value becomes the corresponding dictionary value.<\/p>\n<p dir=\"ltr\">**Worked example combining regular parameters, *args, and <strong>kwargs together:<\/strong><\/p>\n<div tabindex=\"0\" role=\"group\" aria-label=\"python code\">\n<div>\n<div><\/div>\n<\/div>\n<div>python<\/div>\n<div>\n<pre><code class=\"language-python\">def register_student(student_id, *courses, **contact_info):\r\n    print(f\"Student ID: {student_id}\")\r\n    print(f\"Courses: {courses}\")\r\n    print(f\"Contact info: {contact_info}\")\r\n\r\nregister_student(1024, \"Math\", \"Physics\", \"History\", email=\"leo@example.com\", phone=\"555-0142\")<\/code><\/pre>\n<\/div>\n<\/div>\n<p dir=\"ltr\"><strong>Output:<\/strong><\/p>\n<div tabindex=\"0\" role=\"group\" aria-label=\"Code\">\n<div>\n<div><\/div>\n<\/div>\n<div>\n<pre><code>Student ID: 1024\r\nCourses: ('Math', 'Physics', 'History')\r\nContact info: {'email': 'leo@example.com', 'phone': '555-0142'}<\/code><\/pre>\n<\/div>\n<\/div>\n<p dir=\"ltr\"><strong>The order requirement worth memorizing for assignments:<\/strong> regular positional parameters, then <code>*args<\/code>, then regular keyword parameters (if any), then <code>**kwargs<\/code> \u2014 in that exact order in the function definition. Getting this order wrong produces a <code>SyntaxError<\/code> before your code even runs.<\/p>\n<p dir=\"ltr\">Once functions start involving multiple parameters, flexible arguments, and more complex logic, applying the concepts correctly in a real project can become much harder than understanding the individual syntax. If you&#8217;re working through a larger university task, <strong data-start=\"535\" data-end=\"641\"><a class=\"decorated-link\" href=\"https:\/\/us.allassignmentsupport.com\/blog\/python-assignment-help\/\" target=\"_new\" rel=\"noopener\" data-start=\"537\" data-end=\"639\">help with Python assignments<\/a><\/strong> can be useful when you need to work through the implementation as well as understand why the code works.<\/p>\n<h2 dir=\"ltr\"><span class=\"ez-toc-section\" id=\"Variable_Scope_Why_Your_Variable_%E2%80%9CDisappears%E2%80%9D_After_a_Function_Runs\"><\/span>Variable Scope: Why Your Variable &#8220;Disappears&#8221; After a Function Runs<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<div tabindex=\"0\" role=\"group\" aria-label=\"python code\">\n<div>\n<div><\/div>\n<\/div>\n<div>python<\/div>\n<div>\n<pre><code class=\"language-python\">def calculate_total():\r\n    subtotal = 100  # local variable\r\n    tax = subtotal * 0.08\r\n    return subtotal + tax\r\n\r\nresult = calculate_total()\r\nprint(result)     # 108.0\r\nprint(subtotal)   # NameError: name 'subtotal' is not defined<\/code><\/pre>\n<\/div>\n<\/div>\n<p dir=\"ltr\"><strong>What&#8217;s happening:<\/strong> <code>subtotal<\/code> is a <strong>local variable<\/strong> \u2014 it only exists inside <code>calculate_total()<\/code>&#8216;s own scope, and is destroyed the moment the function finishes running. This is one of the most common sources of <code>NameError<\/code> for students new to functions, who expect a variable created inside a function to remain accessible afterward the way a variable defined at the top level of a script does.<\/p>\n<p dir=\"ltr\"><strong>Local vs. global scope, and the trap of assuming you can freely modify a global variable:<\/strong><\/p>\n<div tabindex=\"0\" role=\"group\" aria-label=\"python code\">\n<div>\n<div><\/div>\n<\/div>\n<div>python<\/div>\n<div>\n<pre><code class=\"language-python\">counter = 0\r\n\r\ndef increment():\r\n    counter += 1  # UnboundLocalError!\r\n    return counter\r\n\r\nincrement()<\/code><\/pre>\n<\/div>\n<\/div>\n<p dir=\"ltr\">This raises an <code>UnboundLocalError<\/code>, which confuses many students because it seems like <code>counter<\/code> should simply refer to the global variable defined above. <strong>What&#8217;s actually happening:<\/strong> the moment Python sees an assignment to <code>counter<\/code> <em>anywhere<\/em> inside the function (<code>counter += 1<\/code> is shorthand for <code>counter = counter + 1<\/code>, an assignment), it treats <code>counter<\/code> as a local variable for the <em>entire<\/em> function body \u2014 and a local variable can&#8217;t be read before it&#8217;s been assigned within that same function, which is exactly what <code>counter += 1<\/code> tries to do.<\/p>\n<p dir=\"ltr\"><strong>The fix, using the <code>global<\/code> keyword:<\/strong><\/p>\n<div tabindex=\"0\" role=\"group\" aria-label=\"python code\">\n<div>\n<div><\/div>\n<\/div>\n<div>python<\/div>\n<div>\n<pre><code class=\"language-python\">counter = 0\r\n\r\ndef increment():\r\n    global counter\r\n    counter += 1\r\n    return counter\r\n\r\nprint(increment())  # 1\r\nprint(increment())  # 2<\/code><\/pre>\n<\/div>\n<\/div>\n<p dir=\"ltr\"><strong>Why this matters for assignments:<\/strong> modifying global state from inside a function is often discouraged in good coding practice anyway (a cleaner alternative is to pass the value in and return the updated value), but understanding <em>why<\/em> the <code>UnboundLocalError<\/code> happens \u2014 because Python decides a variable&#8217;s scope for the whole function based on whether it&#8217;s assigned anywhere in that function \u2014 is a concept assignments frequently test directly, since it trips up even students who otherwise understand scope conceptually.<\/p>\n<h2 dir=\"ltr\"><span class=\"ez-toc-section\" id=\"Returning_Multiple_Values\"><\/span>Returning Multiple Values<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<div tabindex=\"0\" role=\"group\" aria-label=\"python code\">\n<div>\n<div><\/div>\n<\/div>\n<div>python<\/div>\n<div>\n<pre><code class=\"language-python\">def get_min_max(numbers):\r\n    return min(numbers), max(numbers)\r\n\r\nlowest, highest = get_min_max([4, 8, 15, 16, 23, 42])\r\nprint(lowest, highest)  # 4 42<\/code><\/pre>\n<\/div>\n<\/div>\n<p dir=\"ltr\"><strong>What&#8217;s actually happening:<\/strong> Python doesn&#8217;t truly return &#8220;two values&#8221; \u2014 it packages them into a single tuple <code>(4, 42)<\/code>, and the assignment <code>lowest, highest = ...<\/code> unpacks that tuple into two separate variables. Understanding this helps explain behavior that otherwise looks like magic, and connects directly to how Python handles iteration more generally, covered in more depth in <strong><a href=\"https:\/\/us.allassignmentsupport.com\/blog\/python-loops-list-comprehensions-and-iterators-a-complete-guide-with-worked-examples\/\">Python Loops, List Comprehensions, and Iterators<\/a><\/strong>.<\/p>\n<p dir=\"ltr\"><strong>Worked example applying this to a statistics-style assignment:<\/strong><\/p>\n<div tabindex=\"0\" role=\"group\" aria-label=\"python code\">\n<div>\n<div><\/div>\n<\/div>\n<div>python<\/div>\n<div>\n<pre><code class=\"language-python\">def summarize(data):\r\n    mean_val = sum(data) \/ len(data)\r\n    sorted_data = sorted(data)\r\n    n = len(sorted_data)\r\n    median_val = (sorted_data[n\/\/2] if n % 2 != 0\r\n                  else (sorted_data[n\/\/2 - 1] + sorted_data[n\/\/2]) \/ 2)\r\n    return mean_val, median_val\r\n\r\nscores = [72, 85, 90, 65, 78, 88]\r\navg, med = summarize(scores)\r\nprint(f\"Mean: {avg:.2f}, Median: {med}\")<\/code><\/pre>\n<\/div>\n<\/div>\n<p dir=\"ltr\">This is exactly the kind of reusable function worth building before running a full statistical test \u2014 see <strong><a href=\"https:\/\/us.allassignmentsupport.com\/blog\/how-to-perform-hypothesis-testing-in-python-a-step-by-step-guide-with-worked-examples\/\">How to Perform Hypothesis Testing in Python<\/a><\/strong> for how functions like this feed directly into <code>scipy.stats<\/code> workflows once your data is properly summarized and cleaned.<\/p>\n<h2 dir=\"ltr\"><span class=\"ez-toc-section\" id=\"A_Step-by-Step_Checklist_for_Students_Stuck_on_a_Functions_Assignment\"><\/span>A Step-by-Step Checklist for Students Stuck on a Functions Assignment<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<ol dir=\"ltr\">\n<li>Decide whether your function needs to <code>return<\/code> a value for later use, or simply <code>print<\/code> something for immediate display \u2014 they are not interchangeable, and using the wrong one is the most common reason a working-looking function produces <code>None<\/code> downstream.<\/li>\n<li>Check your parameter order: positional parameters, then <code>*args<\/code>, then keyword parameters with defaults, then <code>**kwargs<\/code>.<\/li>\n<li>Never use a mutable object (list, dict, set) as a default parameter value \u2014 use <code>None<\/code> as a sentinel and create the mutable object inside the function body instead.<\/li>\n<li>If you&#8217;re getting an <code>UnboundLocalError<\/code> on a variable you&#8217;re sure is global, check whether your function assigns to that variable anywhere \u2014 if it does, add the <code>global<\/code> keyword, or better, pass the value in and return the updated result instead.<\/li>\n<li>If a function needs to return more than one value, remember it&#8217;s actually returning a single tuple, and unpack it into separate variables when you call the function.<\/li>\n<\/ol>\n<h2 dir=\"ltr\"><span class=\"ez-toc-section\" id=\"FAQs\"><\/span>FAQs<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p dir=\"ltr\"><strong>Q1: What&#8217;s the actual difference between an argument and a parameter?<\/strong> A parameter is the placeholder name listed in a function&#8217;s definition (<code>def greet(name):<\/code> \u2014 <code>name<\/code> is the parameter), while an argument is the actual value supplied when the function is called (<code>greet(\"Amara\")<\/code> \u2014 <code>\"Amara\"<\/code> is the argument). Assignments that ask you to &#8220;explain your function&#8221; sometimes specifically check whether you use these two terms correctly.<\/p>\n<p dir=\"ltr\"><strong>Q2: Why does my function return None even though it looks like it calculates something?<\/strong> This almost always means your function uses <code>print()<\/code> to display the result instead of <code>return<\/code> to send it back to the caller. Every Python function returns <code>None<\/code> by default unless it hits an explicit <code>return<\/code> statement \u2014 a function that only prints, with no <code>return<\/code>, will always evaluate to <code>None<\/code> if you try to store or use its result elsewhere.<\/p>\n<p dir=\"ltr\">**Q3: What&#8217;s the actual difference between *args and <strong>kwargs?<\/strong> <code>*args<\/code> collects any extra <em>positional<\/em> arguments into a tuple, while <code>**kwargs<\/code> collects any extra <em>keyword<\/em> (named) arguments into a dictionary. Both let a function accept a flexible, unspecified number of inputs, but they capture different types of arguments and must appear in a specific order in the function definition (regular parameters, then <code>*args<\/code>, then <code>**kwargs<\/code>).<\/p>\n<p dir=\"ltr\"><strong>Q4: Why shouldn&#8217;t I use a list or dictionary as a default parameter value?<\/strong> Because default argument values are created only once, when the function is defined, not each time the function is called \u2014 this means every call that relies on that default shares and mutates the exact same list or dictionary object, causing data to unexpectedly persist and accumulate across separate function calls. Use <code>None<\/code> as the default and create a fresh mutable object inside the function body instead.<\/p>\n<p dir=\"ltr\"><strong>Q5: Do I need to use the <code>global<\/code> keyword every time I want to read a global variable inside a function?<\/strong> No \u2014 you only need <code>global<\/code> when you want to <em>assign<\/em> a new value to a global variable from inside a function. Simply reading a global variable&#8217;s current value inside a function works without any special keyword, since Python only requires <code>global<\/code> when it needs to know you intend to modify, not just read, that variable.<\/p>\n<h2 dir=\"ltr\"><span class=\"ez-toc-section\" id=\"Related_Reading_Woven_Into_This_Guide\"><\/span>Related Reading Woven Into This Guide<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p dir=\"ltr\">Functions are the building blocks used constantly once you move into <strong><a href=\"https:\/\/us.allassignmentsupport.com\/blog\/object-oriented-programming-in-python-classes-inheritance-and-polymorphism-explained\/\">Object-Oriented Programming in Python<\/a><\/strong>, where methods are really just functions defined inside a class. If a function you&#8217;ve written isn&#8217;t behaving as expected, working through the systematic troubleshooting process in <strong><a href=\"https:\/\/us.allassignmentsupport.com\/blog\/debugging-python-code-common-errors-exceptions-and-how-to-fix-them\/\">Debugging Python Code: Common Errors, Exceptions, and How to Fix Them<\/a><\/strong> will help you isolate exactly where the logic breaks down.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Functions are usually introduced early in a Python course as &#8220;reusable blocks of code,&#8221; which makes them sound simple \u2014 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":3436,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_seopress_robots_primary_cat":"none","_seopress_titles_title":"Python Functions Explained: Parameters, *args, **kwargs, and Return Values","_seopress_titles_desc":"A complete university-level guide to Python functions \u2014 positional and keyword arguments, default values, *args, **kwargs, scope, and return values, with worked examples.","_seopress_robots_index":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[5],"tags":[1487,684,1488,1486,1489,1490],"class_list":["post-3433","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-academics","tag-args-kwargs-python","tag-python-assignment-help","tag-python-function-parameters","tag-python-functions-explained","tag-python-scope-local-global","tag-return-values-python"],"_links":{"self":[{"href":"https:\/\/us.allassignmentsupport.com\/blog\/wp-json\/wp\/v2\/posts\/3433"}],"collection":[{"href":"https:\/\/us.allassignmentsupport.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/us.allassignmentsupport.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/us.allassignmentsupport.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/us.allassignmentsupport.com\/blog\/wp-json\/wp\/v2\/comments?post=3433"}],"version-history":[{"count":4,"href":"https:\/\/us.allassignmentsupport.com\/blog\/wp-json\/wp\/v2\/posts\/3433\/revisions"}],"predecessor-version":[{"id":3464,"href":"https:\/\/us.allassignmentsupport.com\/blog\/wp-json\/wp\/v2\/posts\/3433\/revisions\/3464"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/us.allassignmentsupport.com\/blog\/wp-json\/wp\/v2\/media\/3436"}],"wp:attachment":[{"href":"https:\/\/us.allassignmentsupport.com\/blog\/wp-json\/wp\/v2\/media?parent=3433"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/us.allassignmentsupport.com\/blog\/wp-json\/wp\/v2\/categories?post=3433"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/us.allassignmentsupport.com\/blog\/wp-json\/wp\/v2\/tags?post=3433"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}