{"id":130172,"date":"2022-04-13T14:59:56","date_gmt":"2022-04-13T18:59:56","guid":{"rendered":"https:\/\/brainstation.io\/?post_type=codex-reference&#038;p=130172"},"modified":"2025-02-04T14:59:07","modified_gmt":"2025-02-04T19:59:07","slug":"function","status":"publish","type":"codex-reference","link":"https:\/\/brainstation.io\/learn\/python\/function","title":{"rendered":"Function"},"content":{"rendered":"\n<header class=\"wp-block-bstn-blocks-hero-with-overview bstn-blocks-hero-with-overview\"><div class=\"bstn-blocks-hero-with-overview__full-width-wrapper bstn-blocks-hero-with-overview__full-width-wrapper--main\"><svg fill=\"none\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"bstn-blocks-hero-with-overview__bg-flair--full\"><g opacity=\"0.35\"><path d=\"M-103.887 96.613H499.5\" stroke=\"#3C4556\" strokelinecap=\"square\"><\/path><path cliprule=\"evenodd\" d=\"M174.55-136.427v254.863-254.863zm139.219 0v254.863-254.863zm139.219 0v254.863-254.863zM35.332-232v350.436V-232z\" stroke=\"#3C4556\" strokelinecap=\"square\"><\/path><path fillrule=\"evenodd\" cliprule=\"evenodd\" d=\"M35.361 100.134c1.76 0 3.187-1.446 3.187-3.23 0-1.783-1.427-3.23-3.187-3.23-1.76 0-3.187 1.447-3.187 3.23 0 1.784 1.427 3.23 3.187 3.23zm139.17 0c1.76 0 3.187-1.446 3.187-3.23 0-1.783-1.427-3.23-3.187-3.23-1.76 0-3.187 1.447-3.187 3.23 0 1.784 1.427 3.23 3.187 3.23zm139.169 0c1.761 0 3.187-1.446 3.187-3.23 0-1.783-1.426-3.23-3.187-3.23-1.76 0-3.187 1.447-3.187 3.23 0 1.784 1.427 3.23 3.187 3.23zm139.17 0c1.76 0 3.187-1.446 3.187-3.23 0-1.783-1.427-3.23-3.187-3.23-1.76 0-3.187 1.447-3.187 3.23 0 1.784 1.427 3.23 3.187 3.23z\" fill=\"#3C4556\"><\/path><path d=\"M-244.061 214.487H499.5\" stroke=\"#3C4556\" strokelinecap=\"square\"><\/path><path cliprule=\"evenodd\" d=\"M174.55 214.009v254.863-254.863zm139.219 0v254.863-254.863zm139.219 0v254.863-254.863zM35.332 118.436v350.436-350.436z\" stroke=\"#3C4556\" strokelinecap=\"square\"><\/path><path fillrule=\"evenodd\" cliprule=\"evenodd\" d=\"M35.361 218.03c1.76 0 3.187-1.446 3.187-3.23 0-1.783-1.427-3.229-3.187-3.229-1.76 0-3.187 1.446-3.187 3.229 0 1.784 1.427 3.23 3.187 3.23zm139.17 0c1.76 0 3.187-1.446 3.187-3.23 0-1.783-1.427-3.229-3.187-3.229-1.76 0-3.187 1.446-3.187 3.229 0 1.784 1.427 3.23 3.187 3.23zm139.169 0c1.761 0 3.187-1.446 3.187-3.23 0-1.783-1.426-3.229-3.187-3.229-1.76 0-3.187 1.446-3.187 3.229 0 1.784 1.427 3.23 3.187 3.23zm139.17 0c1.76 0 3.187-1.446 3.187-3.23 0-1.783-1.427-3.229-3.187-3.229-1.76 0-3.187 1.446-3.187 3.229 0 1.784 1.427 3.23 3.187 3.23z\" fill=\"#3C4556\"><\/path><path d=\"M534.544 96.613h140.811m138.263 0H1417\" stroke=\"#3C4556\" strokelinecap=\"square\"><\/path><path cliprule=\"evenodd\" d=\"M674.718 96.772v21.664-21.664zM813.618-232v350.436V-232zm139.218 0v350.436V-232z\" stroke=\"#3C4556\" strokelinecap=\"square\"><\/path><path fillrule=\"evenodd\" cliprule=\"evenodd\" d=\"M535.357 100.134c1.761 0 3.187-1.446 3.187-3.23 0-1.783-1.426-3.23-3.187-3.23-1.76 0-3.187 1.447-3.187 3.23 0 1.784 1.427 3.23 3.187 3.23zm139.17 0c1.76 0 3.187-1.446 3.187-3.23 0-1.783-1.427-3.23-3.187-3.23-1.76 0-3.187 1.447-3.187 3.23 0 1.784 1.427 3.23 3.187 3.23zm139.169 0c1.761 0 3.187-1.446 3.187-3.23 0-1.783-1.426-3.23-3.187-3.23-1.76 0-3.187 1.447-3.187 3.23 0 1.784 1.427 3.23 3.187 3.23zm139.17 0c1.76 0 3.187-1.446 3.187-3.23 0-1.783-1.427-3.23-3.187-3.23-1.76 0-3.187 1.447-3.187 3.23 0 1.784 1.427 3.23 3.187 3.23z\" fill=\"#3C4556\"><\/path><path d=\"M673.444 214.487H1417\" stroke=\"#3C4556\" strokelinecap=\"square\"><\/path><path cliprule=\"evenodd\" d=\"M674.718 118.436v228.739-228.739zm138.9 0v350.436-350.436zm139.218 0v350.436-350.436z\" stroke=\"#3C4556\" strokelinecap=\"square\"><\/path><path fillrule=\"evenodd\" cliprule=\"evenodd\" d=\"M813.696 218.03c1.761 0 3.187-1.446 3.187-3.23 0-1.783-1.426-3.229-3.187-3.229-1.76 0-3.187 1.446-3.187 3.229 0 1.784 1.427 3.23 3.187 3.23zm139.17 0c1.76 0 3.187-1.446 3.187-3.23 0-1.783-1.427-3.229-3.187-3.229-1.76 0-3.187 1.446-3.187 3.229 0 1.784 1.427 3.23 3.187 3.23zm-278.339 0c1.76 0 3.187-1.446 3.187-3.23 0-1.783-1.427-3.229-3.187-3.229-1.76 0-3.187 1.446-3.187 3.229 0 1.784 1.427 3.23 3.187 3.23z\" fill=\"#3C4556\"><\/path><\/g><\/svg><div class=\"bstn-blocks-hero-with-overview__container\"><div class=\"bstn-blocks-hero-with-overview__copy-container\"><p class=\"bstn-blocks-hero-with-overview__subtitle\">python tutorial<\/p><h1 class=\"bstn-blocks-hero-with-overview__title\">Python Function<\/h1><p class=\"bstn-blocks-hero-with-overview__description\">Learn more about Python, one of the world\u2019s most versatile and popular programming languages.<\/p><\/div><div class=\"bstn-blocks-hero-with-overview__image-container\"><img decoding=\"async\" class=\"bstn-blocks-hero-with-overview__image\" src=\"https:\/\/d3ghupt9z9s6o0.cloudfront.net\/app\/uploads\/2022\/04\/12115006\/Python-Light-Transparent.svg\" alt=\"\"\/><\/div><\/div><\/div><div class=\"bstn-blocks-hero-with-overview__full-width-wrapper bstn-blocks-hero-with-overview__full-width-wrapper--offset\"><div class=\"bstn-blocks-hero-with-overview__container\"><div class=\"bstn-blocks-hero-with-overview__card-container\"><div class=\"bstn-blocks-hero-with-overview__copy-card\"><nav class=\"bstn-blocks-hero-overview\"><h2 class=\"bstn-blocks-hero-overview__title\"><meta charset=\"utf-8\">Tutorial Contents<\/h2><ol class=\"bstn-blocks-hero-overview__list\">\n<li class=\"wp-block-bstn-blocks-overview-link bstn-blocks-hero-overview-link bstn-blocks-hero-overview-link--indent-0\" style=\"margin-left:0px\"><a class=\"bstn-blocks-hero-overview-link__anchor\" href=\"#what-are-functions-in-python\">What Are Functions in Python?<\/a><\/li>\n\n\n\n<li class=\"wp-block-bstn-blocks-overview-link bstn-blocks-hero-overview-link bstn-blocks-hero-overview-link--indent-0\" style=\"margin-left:0px\"><a class=\"bstn-blocks-hero-overview-link__anchor\" href=\"#how-to-define-a-function-in-python\">How to Define a Function in Python<\/a><\/li>\n\n\n\n<li class=\"wp-block-bstn-blocks-overview-link bstn-blocks-hero-overview-link bstn-blocks-hero-overview-link--indent-0\" style=\"margin-left:0px\"><a class=\"bstn-blocks-hero-overview-link__anchor\" href=\"#how-to-call-a-function-in-python\">How to Call a Function in Python<\/a><\/li>\n\n\n\n<li class=\"wp-block-bstn-blocks-overview-link bstn-blocks-hero-overview-link bstn-blocks-hero-overview-link--indent-0\" style=\"margin-left:0px\"><a class=\"bstn-blocks-hero-overview-link__anchor\" href=\"#python-function-syntax\">Python Function Syntax<\/a><\/li>\n<\/ol><\/nav><\/div><\/div><\/div><\/div><\/header>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-1 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:66.66%\">\n<p class=\"is-style-bstn-paragraph-documentation-body\">Functions are the most powerful features of any programming language, so much so that you won\u2019t be able to write proficient code without mastering the concept and syntax of functions. So what are functions and what makes them such an integral part of learning how to write code with Python? Let\u2019s dive in.<\/p>\n\n\n\n<p class=\"is-style-bstn-paragraph-documentation-body\">In order to write functions, it is very important to first understand why they are used. Imagine that you are given the task of developing a calculator using Python.<\/p>\n\n\n\n<p class=\"is-style-bstn-paragraph-documentation-body\">Your calculator needs to take two operand values and also one of the four mathematical operations &#8211; add, subtract, multiply or divide. A user of your calculator could enter any values for the two numbers and could also choose any of the four mathematical operations. The calculator should work for any of these combinations and should always return the correct value to the user.<\/p>\n\n\n\n<p class=\"is-style-bstn-paragraph-documentation-body\">How can we develop this calculator that can take a wide range of values and could use any mathematical operation? This is where functions come to the rescue.<\/p>\n\n\n\n<hr class=\"wp-block-bstn-blocks-section-divider bstn-blocks-section-divider bstn-blocks-section-divider--grey bstn-blocks-section-divider--32\"\/>\n\n\n\n<div class=\"wp-block-bstn-blocks-heading bstn-blocks-heading bstn-blocks-heading--h3 bstn-blocks-heading--mt-0 has-text-align-left\" id=\"what-are-functions-in-python\"><h2 class=\"bstn-blocks-heading__heading-element\">What Are Functions in Python?<\/h2><\/div>\n\n\n\n<p class=\"is-style-bstn-paragraph-documentation-body\"><br>Functions are used when you need to execute a block of code or logic for a wide range of possible values. Functions are used to do a repetitive task by grouping the logic of the task into a function and then just using that logic multiple times with different sets of values. It allows us to write our logic once and use it as many times as we need.<\/p>\n\n\n\n<p class=\"is-style-bstn-paragraph-documentation-body\">Function usage is divided into two parts:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Defining a function<\/li><li>Using\/calling a function<\/li><\/ol>\n\n\n\n<hr class=\"wp-block-bstn-blocks-section-divider bstn-blocks-section-divider bstn-blocks-section-divider--grey bstn-blocks-section-divider--32\"\/>\n\n\n\n<div class=\"wp-block-bstn-blocks-heading bstn-blocks-heading bstn-blocks-heading--h3 bstn-blocks-heading--mt-0 has-text-align-left\" id=\"how-to-define-a-function-in-python\"><h2 class=\"bstn-blocks-heading__heading-element\">How to Define a Function in Python<\/h2><\/div>\n\n\n\n<ul class=\"wp-block-list\"><li>A function in Python is defined using a\u00a0<code>def<\/code>\u00a0keyword followed by the name of the function, then followed by a set of round parentheses. We&#8217;ll see an example below.<\/li><li>A function may or may not need an input.<ul><li>Input, if any, is defined for a function using\u00a0<code>parameters<\/code>.<\/li><li>Multiple parameters should be separated by a comma.<\/li><li>Input parameters can be assigned a default value using the equal sign.<\/li><\/ul><\/li><li>A function may or may not provide any output.<ul><li>Output from a function should be provided using a\u00a0<code>return<\/code>\u00a0keyword followed by values that need to be output or returned.<\/li><li>If there is a need to return or output multiple values from a function, they need to be separated by a comma.<\/li><\/ul><\/li><\/ul>\n\n\n\n<hr class=\"wp-block-bstn-blocks-section-divider bstn-blocks-section-divider bstn-blocks-section-divider--grey bstn-blocks-section-divider--32\"\/>\n\n\n\n<div class=\"wp-block-bstn-blocks-heading bstn-blocks-heading bstn-blocks-heading--h3 bstn-blocks-heading--mt-0 has-text-align-left\" id=\"how-to-call-a-function-in-python\"><h2 class=\"bstn-blocks-heading__heading-element\">How to Call a Function in Python<\/h2><\/div>\n\n\n\n<ul class=\"wp-block-list\"><li>Functions in Python can be called by using the name of the function followed by round parentheses.<\/li><li>When defined, if the function had input parameters, the values for those parameters can now be provided inside the round parentheses while calling the function. When these values are provided while calling the function, they are called&nbsp;<code>arguments<\/code>.<\/li><li>The order in which the arguments are provided should match the order in which the parameters were listed in function definition.<\/li><li>If the function definition returns a value or output, it can be stored for future use by assigning the function call to a variable. This variable will be assigned to the returned value of the function.<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-bstn-blocks-section-divider bstn-blocks-section-divider bstn-blocks-section-divider--grey bstn-blocks-section-divider--32\"\/>\n\n\n\n<div class=\"wp-block-bstn-blocks-heading bstn-blocks-heading bstn-blocks-heading--h3 bstn-blocks-heading--mt-0 has-text-align-left\" id=\"python-function-syntax\"><h2 class=\"bstn-blocks-heading__heading-element\">Python Function Syntax<\/h2><\/div>\n\n\n\n<div class=\"wp-block-bstn-blocks-code-snippet bstn-blocks-code-snippet\" data-code-snippet=\"true\"><div class=\"bstn-blocks-code-snippet__title-bar\"><span class=\"bstn-blocks-code-snippet__title\">Code Example<\/span><button class=\"bstn-blocks-code-snippet__copy-trigger\" aria-label=\"Copy code to clipboard\" data-copy-to-clipboard=\"true\"><svg viewbox=\"0 0 12 13\" fill=\"none\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"bstn-blocks-code-snippet__copy-trigger-icon\"><path d=\"M10.875.5h-7.5c-.633 0-1.125.516-1.125 1.125V2.75H1.125C.492 2.75 0 3.266 0 3.875v7.5A1.11 1.11 0 001.125 12.5h7.5c.61 0 1.125-.492 1.125-1.125V10.25h1.125c.61 0 1.125-.492 1.125-1.125v-7.5A1.14 1.14 0 0010.875.5zm-2.39 10.875h-7.22c-.093 0-.14-.047-.14-.14v-7.22c0-.07.047-.14.14-.14h.985v5.25a1.11 1.11 0 001.125 1.125h5.25v.984c0 .094-.07.141-.14.141zm2.25-2.25h-7.22c-.093 0-.14-.047-.14-.14v-7.22c0-.07.047-.14.14-.14h7.22c.07 0 .14.07.14.14v7.22c0 .093-.07.14-.14.14z\" fill=\"currentColor\"><\/path><\/svg><span data-copy-to-clipboard-text=\"true\">Copy<\/span><\/button><\/div><pre class=\"bstn-blocks-code-snippet__code-snippet-area match-braces line-numbers language-python\" data-language=\"python\" aria-readonly=\"true\"><code># function definition\ndef function_name(optional_parameters_separated_by_comma):\n    Function logic goes here. Indentation is very crucial to separate\n    Function definition from the body of the function. \n        Use indentation to group a block of code together. This indented block of code is different from the previous one. \n    return optional_return_value_1, optional_return_value_2, so on\u2026\n\n# calling the function\nreturned_value_from_fn = function_name(optional_argument_one, optional_argument_two)\nprint(returned_value_from_fn)<\/code><\/pre><\/div>\n\n\n\n<p class=\"is-style-bstn-paragraph-documentation-body\">That was a lot of information. It can be challenging to understand this information without having a look at an actual function. Functions are a bit challenging to understand and use if you have never written any code before. To make this much simpler, let\u2019s take a look at a couple of examples in Python starting with a basic function with no inputs or outputs. We&#8217;ll then look at something a bit more complex.<\/p>\n\n\n\n<div class=\"wp-block-bstn-blocks-code-snippet bstn-blocks-code-snippet\" data-code-snippet=\"true\"><div class=\"bstn-blocks-code-snippet__title-bar\"><span class=\"bstn-blocks-code-snippet__title\">Code Example<\/span><button class=\"bstn-blocks-code-snippet__copy-trigger\" aria-label=\"Copy code to clipboard\" data-copy-to-clipboard=\"true\"><svg viewbox=\"0 0 12 13\" fill=\"none\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"bstn-blocks-code-snippet__copy-trigger-icon\"><path d=\"M10.875.5h-7.5c-.633 0-1.125.516-1.125 1.125V2.75H1.125C.492 2.75 0 3.266 0 3.875v7.5A1.11 1.11 0 001.125 12.5h7.5c.61 0 1.125-.492 1.125-1.125V10.25h1.125c.61 0 1.125-.492 1.125-1.125v-7.5A1.14 1.14 0 0010.875.5zm-2.39 10.875h-7.22c-.093 0-.14-.047-.14-.14v-7.22c0-.07.047-.14.14-.14h.985v5.25a1.11 1.11 0 001.125 1.125h5.25v.984c0 .094-.07.141-.14.141zm2.25-2.25h-7.22c-.093 0-.14-.047-.14-.14v-7.22c0-.07.047-.14.14-.14h7.22c.07 0 .14.07.14.14v7.22c0 .093-.07.14-.14.14z\" fill=\"currentColor\"><\/path><\/svg><span data-copy-to-clipboard-text=\"true\">Copy<\/span><\/button><\/div><pre class=\"bstn-blocks-code-snippet__code-snippet-area match-braces line-numbers language-python\" data-language=\"python\" aria-readonly=\"true\"><code># function definition - greet_hello_world\n# no parameters\n>>> def greet_hello_world(): \n\u2026\tprint('hello world')\n\u2026\n\n# calling the function - greet_hello_world\n# no arguments\n>>> greet_hello_world()\nhello world\n<\/code><\/pre><\/div>\n\n\n\n<p class=\"is-style-bstn-paragraph-documentation-body\">The first function we wrote is&nbsp;<code>greet_hello_world<\/code>. This function does not take any parameters or input and does not return any values as output from the function. All it does when called is print&nbsp;<code>hello world<\/code>. Pay attention that for every function, we need two steps &#8211; defining a function, and then calling that function in the proper way.<\/p>\n\n\n\n<div class=\"wp-block-bstn-blocks-code-snippet bstn-blocks-code-snippet\" data-code-snippet=\"true\"><div class=\"bstn-blocks-code-snippet__title-bar\"><span class=\"bstn-blocks-code-snippet__title\">Code Example<\/span><button class=\"bstn-blocks-code-snippet__copy-trigger\" aria-label=\"Copy code to clipboard\" data-copy-to-clipboard=\"true\"><svg viewbox=\"0 0 12 13\" fill=\"none\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"bstn-blocks-code-snippet__copy-trigger-icon\"><path d=\"M10.875.5h-7.5c-.633 0-1.125.516-1.125 1.125V2.75H1.125C.492 2.75 0 3.266 0 3.875v7.5A1.11 1.11 0 001.125 12.5h7.5c.61 0 1.125-.492 1.125-1.125V10.25h1.125c.61 0 1.125-.492 1.125-1.125v-7.5A1.14 1.14 0 0010.875.5zm-2.39 10.875h-7.22c-.093 0-.14-.047-.14-.14v-7.22c0-.07.047-.14.14-.14h.985v5.25a1.11 1.11 0 001.125 1.125h5.25v.984c0 .094-.07.141-.14.141zm2.25-2.25h-7.22c-.093 0-.14-.047-.14-.14v-7.22c0-.07.047-.14.14-.14h7.22c.07 0 .14.07.14.14v7.22c0 .093-.07.14-.14.14z\" fill=\"currentColor\"><\/path><\/svg><span data-copy-to-clipboard-text=\"true\">Copy<\/span><\/button><\/div><pre class=\"bstn-blocks-code-snippet__code-snippet-area match-braces line-numbers language-python\" data-language=\"python\" aria-readonly=\"true\"><code># function definition - multiplier\n# takes two parameters, and returns their product\n>>> def multiplier(input_num, input_multiplier):\n\u2026 \tresult = input_num * input_multiplier\n\u2026 \treturn result\n\u2026\n# calling the function - multiplier\n# needs two argument values\n>>> returned_value_from_fn = multiplier(3, 5)\n>>> print(returned_value_from_fn)\n15\n# calling the multiplier function again - a function can be called as many time as needed\n# passing different arguments this time\n>>> returned_value_from_fn = multiplier(10, 5)\n>>> print(returned_value_from_fn)\n50\n<\/code><\/pre><\/div>\n\n\n\n<p class=\"is-style-bstn-paragraph-documentation-body\">The next function we wrote is a&nbsp;<code>multiplier<\/code>. This function takes two parameters &#8211;&nbsp;<code>input_num<\/code>&nbsp;and&nbsp;<code>input_multiplier<\/code>. It multiplies both the parameters and stores the multiplication in a variable called&nbsp;<code>result<\/code>. Then we return the result variable from the function.<\/p>\n\n\n\n<p class=\"is-style-bstn-paragraph-documentation-body\">Later, when we call the function multiplier, we make sure to pass actual values to both the parameters in the function definition. When these values are passed as part of a function call, they become&nbsp;<strong>function arguments<\/strong>. We also called the same function again but now with a different set of arguments. As you can see, we wrote the function definition once but we can call the function as many times as we need.<\/p>\n\n\n\n<div class=\"wp-block-bstn-blocks-code-snippet bstn-blocks-code-snippet\" data-code-snippet=\"true\"><div class=\"bstn-blocks-code-snippet__title-bar\"><span class=\"bstn-blocks-code-snippet__title\">Code Example<\/span><button class=\"bstn-blocks-code-snippet__copy-trigger\" aria-label=\"Copy code to clipboard\" data-copy-to-clipboard=\"true\"><svg viewbox=\"0 0 12 13\" fill=\"none\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"bstn-blocks-code-snippet__copy-trigger-icon\"><path d=\"M10.875.5h-7.5c-.633 0-1.125.516-1.125 1.125V2.75H1.125C.492 2.75 0 3.266 0 3.875v7.5A1.11 1.11 0 001.125 12.5h7.5c.61 0 1.125-.492 1.125-1.125V10.25h1.125c.61 0 1.125-.492 1.125-1.125v-7.5A1.14 1.14 0 0010.875.5zm-2.39 10.875h-7.22c-.093 0-.14-.047-.14-.14v-7.22c0-.07.047-.14.14-.14h.985v5.25a1.11 1.11 0 001.125 1.125h5.25v.984c0 .094-.07.141-.14.141zm2.25-2.25h-7.22c-.093 0-.14-.047-.14-.14v-7.22c0-.07.047-.14.14-.14h7.22c.07 0 .14.07.14.14v7.22c0 .093-.07.14-.14.14z\" fill=\"currentColor\"><\/path><\/svg><span data-copy-to-clipboard-text=\"true\">Copy<\/span><\/button><\/div><pre class=\"bstn-blocks-code-snippet__code-snippet-area match-braces line-numbers language-python\" data-language=\"python\" aria-readonly=\"true\"><code># function definition - power fn\n# takes one parameter &amp; returns the squared and cubed value of the parameter\n\n>>> def power_fn(input_num=10):\n\u2026 \tsquared = input_num ** 2\n\u2026 \tcubed = input_num ** 3\n\u2026 \treturn squared, cubed\n\u2026\n\n# calling the function - power_fn\n# needs one argument value\n>>> squared_returned_value, cubed_returned_value = power_fn(5)\n>>> print(squared_returned_value, cubed_returned_value)\n25 125\n\n# calling the power_fn function again\n# passing no arguments this time\n# if no arguments are passed, it will use the default value if any\n# provided in function definition, in this case - 10\n>>> squared_returned_value, cubed_returned_value = power_fn()\n>>> print(squared_returned_value, cubed_returned_value)\n100 1000\n<\/code><\/pre><\/div>\n\n\n\n<p class=\"is-style-bstn-paragraph-documentation-body\">The final function we wrote is&nbsp;<code>power_fn<\/code>. This function takes a parameter called&nbsp;<code>input_num<\/code>&nbsp;which has a default value of&nbsp;<code>10<\/code>, if no arguments are provided when calling the function. We perform a power of 2 and power of 3 operation on&nbsp;<code>input_num<\/code>&nbsp;and store the results in two different variables&nbsp;<code>squared<\/code>&nbsp;and&nbsp;<code>cubed<\/code>.<\/p>\n\n\n\n<p class=\"is-style-bstn-paragraph-documentation-body\"><strong>Next, instead of returning one value from the function, we return both squared and cubed variables from the function.<\/strong>&nbsp;We then call the function with an argument value of&nbsp;<code>5<\/code>&nbsp;and&nbsp;<strong>we unpack both the returned values from the function into separate variables<\/strong>&nbsp;<code>squared_returned_value<\/code>&nbsp;and&nbsp;<code>cubed_returned_value<\/code>.<\/p>\n\n\n\n<p class=\"is-style-bstn-paragraph-documentation-body\">We call the function again, but this time we don\u2019t pass any arguments.&nbsp;<strong>Because the function parameter has a default value of 10 if no arguments are passed, it will work and give us the output of 100 and 1000.<\/strong><\/p>\n\n\n\n<p class=\"is-style-bstn-paragraph-documentation-body\">Don&#8217;t worry if functions aren&#8217;t completely clear yet. Being aware of functions and revising these examples are a great first start in Python. <\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:25%\"><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-bstn-blocks-content-navigation-footer bstn-blocks-content-nav-footer\"><div class=\"bstn-blocks-content-nav-footer__links-container\"><div class=\"bstn-blocks-content-nav-footer__link-container\"><p class=\"bstn-blocks-content-nav-footer__link-title bstn-blocks-content-nav-footer__link--desktop\">Previous<\/p><a class=\"bstn-blocks-content-nav-footer__link\" href=\"https:\/\/brainstation.io\/learn\/python\/while-loop\"><span class=\"bstn-blocks-content-nav-footer__link-icon-wrapper bstn-blocks-content-nav-footer__link-icon-wrapper--left\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewbox=\"0 0 18 18\" fill=\"none\" class=\"bstn-blocks-content-nav-footer__link-icon bstn-blocks-content-nav-footer__link-icon--left\"><path fill=\"currentColor\" d=\"M6.345 12.743a1 1 0 001.31 1.511l5.477-4.749a1 1 0 000-1.511l-5.477-4.75a1.001 1.001 0 00-1.31 1.512l4.606 3.993-4.606 3.994z\"><\/path><\/svg><\/span><span class=\"bstn-blocks-content-nav-footer__link-text bstn-blocks-content-nav-footer__link--desktop\">Python While Loop<\/span><\/a><\/div><div class=\"bstn-blocks-content-nav-footer__link-container\"><p class=\"bstn-blocks-content-nav-footer__link-title\">Next<\/p><a class=\"bstn-blocks-content-nav-footer__link\" href=\"https:\/\/brainstation.io\/learn\/python\/lambda\"><span class=\"bstn-blocks-content-nav-footer__link-text\">Python Lambda<\/span><span class=\"bstn-blocks-content-nav-footer__link-icon-wrapper bstn-blocks-content-nav-footer__link-icon-wrapper--right\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewbox=\"0 0 18 18\" fill=\"none\" class=\"bstn-blocks-content-nav-footer__link-icon bstn-blocks-content-nav-footer__link-icon--right\"><path fill=\"currentColor\" d=\"M6.345 12.743a1 1 0 001.31 1.511l5.477-4.749a1 1 0 000-1.511l-5.477-4.75a1.001 1.001 0 00-1.31 1.512l4.606 3.993-4.606 3.994z\"><\/path><\/svg><\/span><\/a><\/div><\/div><\/div>\n\n\n\n<div class=\"wp-block-bstn-blocks-container bstn-blocks-container bstn-blocks-container--dark-blue bstn-blocks-container--64 bstn-blocks-container--negative-container bstn-blocks-container--overflow-hidden\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewbox=\"0 0 1140 621\" fill=\"none\" class=\"bstn-blocks-container__bg-flair\"><g opacity=\"0.55\"><path stroke=\"#3C4556\" strokelinecap=\"square\" d=\"M60.026 736.434V203.02\"><\/path><path stroke=\"#3C4556\" strokelinecap=\"square\" d=\"M60.364 436.132L-120 436.131l180.364.001zm0-99.873H-120 60.364zm0-99.872H-120 60.364zM128 536.004h-248 248z\" cliprule=\"evenodd\"><\/path><path fill=\"#3C4556\" fillrule=\"evenodd\" d=\"M57.518 535.982a2.286 2.286 0 114.572 0 2.286 2.286 0 01-4.572 0zm0-99.836a2.286 2.286 0 114.572-.002 2.286 2.286 0 01-4.572.002zm0-99.837a2.286 2.286 0 114.572-.002 2.286 2.286 0 01-4.572.002zm0-99.837a2.286 2.286 0 114.572-.002 2.286 2.286 0 01-4.572.002z\" cliprule=\"evenodd\"><\/path><\/g><g opacity=\"0.55\"><path stroke=\"#3C4556\" strokelinecap=\"square\" d=\"M979.195-28.206V397.98m81.185-524.745V397.98\"><\/path><path stroke=\"#3C4556\" strokelinecap=\"square\" d=\"M1141.45 168.46H964h177.45zm0 98.334H964h177.45zm0 98.333H964h177.45zm66.55-295H964h244z\" cliprule=\"evenodd\"><\/path><path fill=\"#3C4556\" fillrule=\"evenodd\" d=\"M1138.66 70.148a2.244 2.244 0 104.49 0 2.246 2.246 0 10-4.49-.001zm-80.21 0c0 1.243 1.01 2.251 2.25 2.251s2.25-1.008 2.25-2.251c0-1.243-1.01-2.251-2.25-2.251s-2.25 1.008-2.25 2.25zm-81.707 0a2.25 2.25 0 104.5.004 2.25 2.25 0 00-4.5-.005zm81.707 98.299a2.252 2.252 0 002.25 2.251 2.252 2.252 0 000-4.503 2.253 2.253 0 00-2.25 2.252zm-81.707 0a2.25 2.25 0 104.498 0 2.25 2.25 0 00-2.249-2.252 2.25 2.25 0 00-2.249 2.252zm161.917-.001a2.244 2.244 0 104.49 0 2.246 2.246 0 10-4.49 0zm0 98.299a2.244 2.244 0 104.49 0 2.246 2.246 0 10-4.49 0zm-80.21 0c0 1.243 1.01 2.251 2.25 2.251s2.25-1.008 2.25-2.251c0-1.243-1.01-2.251-2.25-2.251s-2.25 1.008-2.25 2.251zm-81.707 0a2.25 2.25 0 104.5.002 2.25 2.25 0 00-4.5-.002zm81.707 98.299c0 1.243 1.01 2.251 2.25 2.251s2.25-1.008 2.25-2.251c0-1.243-1.01-2.251-2.25-2.251s-2.25 1.008-2.25 2.251zm-81.707 0a2.25 2.25 0 104.5.002 2.25 2.25 0 00-4.5-.002zm161.917 0a2.244 2.244 0 104.49 0 2.246 2.246 0 10-4.49 0z\" cliprule=\"evenodd\"><\/path><\/g><\/svg><div class=\"bstn-blocks-container__wrapper bstn-blocks-container__wrapper--readable-width bstn-blocks-container__wrapper--negative-container\">\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-2 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-bstn-blocks-heading bstn-blocks-heading bstn-blocks-heading--h2 has-text-align-left bstn-blocks-heading--divider\"><h2 class=\"bstn-blocks-heading__heading-element\">Learn Python Today<\/h2><\/div>\n\n\n\n<p>Get hands-on experience writing code with interactive tutorials in our free online learning platform. <\/p>\n\n\n\n<div style=\"height:16px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<ul class=\"wp-block-list is-style-kg-list-checklist-alternate\"><li>Free and fun<\/li><li>Designed for beginners<\/li><li>No downloads or setup required<\/li><\/ul>\n\n\n\n<div style=\"height:16px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button is-style-bstn-button-primary\"><a class=\"wp-block-button__link\" href=\"https:\/\/brainstation.io\/learn\/python\" target=\"_blank\" rel=\"noreferrer noopener\">Get Started<\/a><\/div>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-video\"><video autoplay loop muted src=\"https:\/\/d3ghupt9z9s6o0.cloudfront.net\/app\/uploads\/2022\/04\/13110643\/Python-promo-clip.mp4\" playsinline><\/video><\/figure>\n<\/div>\n<\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Functions are the most powerful features of any programming language, so much so that you won\u2019t be able to write proficient code without mastering the concept and syntax of functions. So what are functions and what makes them such an integral part of learning how to write code with Python? Let\u2019s dive in. In order [&hellip;]<\/p>\n","protected":false},"featured_media":0,"parent":130131,"menu_order":0,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-130172","codex-reference","type-codex-reference","status-publish","format-standard","hentry"],"acf":[],"_links":{"self":[{"href":"https:\/\/brainstation.io\/wp\/api\/wp\/v2\/codex-reference\/130172","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/brainstation.io\/wp\/api\/wp\/v2\/codex-reference"}],"about":[{"href":"https:\/\/brainstation.io\/wp\/api\/wp\/v2\/types\/codex-reference"}],"version-history":[{"count":9,"href":"https:\/\/brainstation.io\/wp\/api\/wp\/v2\/codex-reference\/130172\/revisions"}],"predecessor-version":[{"id":133058,"href":"https:\/\/brainstation.io\/wp\/api\/wp\/v2\/codex-reference\/130172\/revisions\/133058"}],"up":[{"embeddable":true,"href":"https:\/\/brainstation.io\/wp\/api\/wp\/v2\/codex-reference\/130131"}],"wp:attachment":[{"href":"https:\/\/brainstation.io\/wp\/api\/wp\/v2\/media?parent=130172"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/brainstation.io\/wp\/api\/wp\/v2\/categories?post=130172"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/brainstation.io\/wp\/api\/wp\/v2\/tags?post=130172"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}