{"id":94,"date":"2020-10-03T01:29:33","date_gmt":"2020-10-03T01:29:33","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=94"},"modified":"2025-03-26T12:42:34","modified_gmt":"2025-03-26T12:42:34","slug":"python-functions","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-basics\/python-functions\/","title":{"rendered":"Python Functions"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn to develop Python functions by using the <code>def<\/code> keyword.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='what-is-a-function'>What is a function <a href=\"#what-is-a-function\" class=\"anchor\" id=\"what-is-a-function\" title=\"Anchor for What is a function\">#<\/a><\/h2>\n\n\n\n<p>A function is a named code block that performs a job or returns a value.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='why-do-you-need-functions-in-python'>Why do you need functions in Python <a href=\"#why-do-you-need-functions-in-python\" class=\"anchor\" id=\"why-do-you-need-functions-in-python\" title=\"Anchor for Why do you need functions in Python\">#<\/a><\/h2>\n\n\n\n<p>Sometimes, you need to perform a task multiple times in a program. And you don&#8217;t want to copy the code for that same task all over places. <\/p>\n\n\n\n<p>To do so, you wrap the code in a function and use this function to perform the task whenever you need it.<\/p>\n\n\n\n<p>For example, whenever you want to display a value on the screen, you need to call the <code>print()<\/code> function. Behind the scene, Python runs the code inside the <code>print()<\/code> function to display a value on the screen.<\/p>\n\n\n\n<p>In practice, you use functions to divide a large program into smaller and more manageable parts. The functions will make your program easier to develop, read, test, and maintain.<\/p>\n\n\n\n<p>The <code>print()<\/code> function is one of many built-in functions in Python. It means that these functions are available everywhere in the program.<\/p>\n\n\n\n<p>In this tutorial, you&#8217;ll learn how to define user-defined Python functions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='defining-a-python-function'>Defining a Python function <a href=\"#defining-a-python-function\" class=\"anchor\" id=\"defining-a-python-function\" title=\"Anchor for Defining a Python function\">#<\/a><\/h2>\n\n\n\n<p>Here&#8217;s a simple function that shows a greeting:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">greet<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    <span class=\"hljs-string\">\"\"\" Display a greeting to users \"\"\"<\/span>\n    print(<span class=\"hljs-string\">'Hi'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This example shows the simplest structure of a function. A function has two main parts: a function definition and body.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='1-function-definition'>1) Function definition <a href=\"#1-function-definition\" class=\"anchor\" id=\"1-function-definition\" title=\"Anchor for 1) Function definition\">#<\/a><\/h3>\n\n\n\n<p>A function definition starts with the <code>def<\/code> keyword and the name of the function (<code>greet<\/code>).<\/p>\n\n\n\n<p>If the function needs some information to do its job, you need to specify it inside the parentheses <code>()<\/code>. The <code>greet<\/code> function in this example doesn&#8217;t need any information, so its parentheses are empty.<\/p>\n\n\n\n<p>The function definition always ends in a colon (<code>:<\/code>).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='2-function-body'>2) Function body <a href=\"#2-function-body\" class=\"anchor\" id=\"2-function-body\" title=\"Anchor for 2) Function body\">#<\/a><\/h3>\n\n\n\n<p>All the indented lines that follow the function definition make up the function&#8217;s body.<\/p>\n\n\n\n<p>The text string surrounded by triple quotes is called a <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-function-docstrings\/\">docstring<\/a>. It describes what the function does. Python uses the docstring to generate documentation for the function automatically.<\/p>\n\n\n\n<p>The line <code>print('Hi')<\/code> is the only line of actual code in the function body. The <code>greet()<\/code> function does one task: <code>print('Hi')<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='calling-a-function'>Calling a function <a href=\"#calling-a-function\" class=\"anchor\" id=\"calling-a-function\" title=\"Anchor for Calling a function\">#<\/a><\/h2>\n\n\n\n<p>When you want to use a function, you need to call it. A function call instructs Python to execute the code inside the function. <\/p>\n\n\n\n<p>To call a function, you write the function&#8217;s name, followed by the information that the function needs in parentheses.<\/p>\n\n\n\n<p>The following example calls the <code>greet()<\/code> function. Since the <code>greet()<\/code> function doesn&#8217;t need any information, you need to specify empty parentheses like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">greet()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If you run the program: <\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">def greet():\n    <span class=\"hljs-string\">\"\"<\/span><span class=\"hljs-string\">\" Display a greeting to users \"<\/span><span class=\"hljs-string\">\"\"<\/span>\n    <span class=\"hljs-keyword\">print<\/span>(<span class=\"hljs-string\">'Hi'<\/span>)\n    \ngreet()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=ZGVmIGdyZWV0KCk6CiAgICAiIiIgRGlzcGxheSBhIGdyZWV0aW5nIHRvIHVzZXJzICIiIgogICAgcHJpbnQoJ0hpJykKICAgIApncmVldCgp\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>it&#8217;ll show a greeting on the screen:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">Hi<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='passing-information-to-python-functions'>Passing information to Python functions <a href=\"#passing-information-to-python-functions\" class=\"anchor\" id=\"passing-information-to-python-functions\" title=\"Anchor for Passing information to Python functions\">#<\/a><\/h2>\n\n\n\n<p>Suppose that you want to greet users by their names. To do it, you need to specify a name in parentheses of the function definition as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">greet<\/span><span class=\"hljs-params\">(name)<\/span>:<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>name<\/code> is called a function parameter or simply a parameter.<\/p>\n\n\n\n<p>When you add a parameter to the function definition, you can use it as a variable inside the function body:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">greet<\/span><span class=\"hljs-params\">(name)<\/span>:<\/span>\n    print(<span class=\"hljs-string\">f\"Hi <span class=\"hljs-subst\">{name}<\/span>\"<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>And you can access the <code>name<\/code> parameter only within the body of the <code>greet()<\/code> function, not the outside.<\/p>\n\n\n\n<p>When you call a function with a parameter, you need to pass the information. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">greet(<span class=\"hljs-string\">'John'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Here&#8217;s the complete program:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">def greet(name):\n    <span class=\"hljs-keyword\">print<\/span>(f<span class=\"hljs-string\">\"Hi {name}\"<\/span>)\n    \ngreet(<span class=\"hljs-string\">'John'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=ZGVmIGdyZWV0KG5hbWUpOgogICAgcHJpbnQoZiJIaSB7bmFtZX0iKQogICAgCmdyZWV0KCdKb2huJyk%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">Hi John<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The value that you pass into a function is called an <strong>argument<\/strong>. In this example <code>'John'<\/code> is an argument.<\/p>\n\n\n\n<p>Alternatively, you can call the function by passing a variable into it:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">greet<\/span><span class=\"hljs-params\">(name)<\/span>:<\/span>\n    print(<span class=\"hljs-string\">f\"Hi <span class=\"hljs-subst\">{name}<\/span>\"<\/span>)\n    \nfirst_name = <span class=\"hljs-string\">'Jane'<\/span>\ngreet(first_name)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=ZGVmIGdyZWV0KG5hbWUpOgogICAgcHJpbnQoZiJIaSB7bmFtZX0iKQogICAgCmZpcnN0X25hbWUgPSAnSmFuZScKZ3JlZXQoZmlyc3RfbmFtZSk%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">Hi Jane<\/code><\/span><\/pre>\n\n\n<p>In this example, the <code>first_name<\/code> variable is also the argument of the <code>greet()<\/code> function.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='parameters-vs-arguments'>Parameters vs. Arguments <a href=\"#parameters-vs-arguments\" class=\"anchor\" id=\"parameters-vs-arguments\" title=\"Anchor for Parameters vs. Arguments\">#<\/a><\/h3>\n\n\n\n<p>Sometimes, parameters and arguments are used interchangeably. It&#8217;s important to distinguish between the parameters and arguments of a function. <\/p>\n\n\n\n<p>A parameter is a piece of information that a function needs. And you specify the parameter in the function definition. For example, the <code>greet()<\/code> function has a parameter called <code>name<\/code>.<\/p>\n\n\n\n<p>An argument is a piece of data that you pass into the function. For example, the text string <code>'John'<\/code> or the variable <code>jane<\/code> is the function argument.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='returning-a-value'>Returning a value <a href=\"#returning-a-value\" class=\"anchor\" id=\"returning-a-value\" title=\"Anchor for Returning a value\">#<\/a><\/h2>\n\n\n\n<p>A function can perform a task like the <code>greet()<\/code> function. Or it can return a value. The value that a function returns is called a <strong>return value<\/strong>.<\/p>\n\n\n\n<p>To return a value from a function, you use the <code>return<\/code> statement inside the function body.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">return<\/span> value<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following example modifies the <code>greet()<\/code> function to return a greeting instead of displaying it on the screen:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">greet<\/span><span class=\"hljs-params\">(name)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">f\"Hi <span class=\"hljs-subst\">{name}<\/span>\"<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>When you call the <code>greet()<\/code> function, you can assign its return value to a variable:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">greeting = greet(<span class=\"hljs-string\">'John'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>And show it on the screen:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">print(greeting)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The new <code>greet()<\/code> function is better than the old one because it doesn&#8217;t depend on the <code>print()<\/code> function.<\/p>\n\n\n\n<p>Later, you can reuse the <code>greet()<\/code> function in other applications. For example, you can use it in a web application to greet users after they log in.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">def greet(name):\n    <span class=\"hljs-keyword\">return<\/span> f<span class=\"hljs-string\">\"Hi {name}\"<\/span>\n    \ngreeting = greet(<span class=\"hljs-string\">'John'<\/span>)\n<span class=\"hljs-keyword\">print<\/span>(greeting)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=ZGVmIGdyZWV0KG5hbWUpOgogICAgcmV0dXJuIGYiSGkge25hbWV9IgogICAgCmdyZWV0aW5nID0gZ3JlZXQoJ0pvaG4nKQpwcmludChncmVldGluZyk%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">Hi John<\/code><\/span><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='python-functions-with-multiple-parameters'>Python functions with multiple parameters <a href=\"#python-functions-with-multiple-parameters\" class=\"anchor\" id=\"python-functions-with-multiple-parameters\" title=\"Anchor for Python functions with multiple parameters\">#<\/a><\/h2>\n\n\n\n<p>A function can have zero, one, or multiple parameters. <\/p>\n\n\n\n<p>The following example defines a function called <code>sum()<\/code> that calculates the sum of two numbers:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">sum<\/span><span class=\"hljs-params\">(a, b)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">return<\/span> a + b\n\n\ntotal = sum(<span class=\"hljs-number\">10<\/span>,<span class=\"hljs-number\">20<\/span>)\nprint(total)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=ZGVmIHN1bShhLCBiKToKICAgIHJldHVybiBhICsgYgoKCnRvdGFsID0gc3VtKDEwLDIwKQpwcmludCh0b3RhbCk%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-number\">30<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, the <code>sum()<\/code> function has two parameters <code>a<\/code> and <code>b<\/code>, and returns the sum of them.<\/p>\n\n\n\n<p>When a function has multiple parameters, you need to use a comma to separate them. <\/p>\n\n\n\n<p>When you call the function, you need to pass all the arguments. If you pass more or fewer arguments to the function, you&#8217;ll get an error.<\/p>\n\n\n\n<p>In the following function call, <code>a<\/code> will be <code>10<\/code> and <code>b<\/code> will be <code>20<\/code> inside the function body:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">total = sum(<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">20<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A Python function is a reusable named block of code that performs a task or returns a value.<\/li>\n\n\n\n<li>Use the <code>def<\/code> keyword to define a new function. A function consists of function definition and body.<\/li>\n\n\n\n<li>A function can have zero or more parameters. If a function has one or more parameters, you need to pass the same number of arguments into it.<\/li>\n\n\n\n<li>A function can perform a job or return a value. Use the <code>return<\/code> statement to return a value from a function.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='quiz'>Quiz <a href=\"#quiz\" class=\"anchor\" id=\"quiz\" title=\"Anchor for Quiz\">#<\/a><\/h2>\n\n\n\n<iframe loading=\"lazy\"\n  name=\"quiz\"\n  src=\"\/quiz\/?quiz=function\"\n  height=\"700\"\n  width=\"600\"\n  class=\"iframe\"\n><\/iframe>\n\n\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"94\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-basics\/python-functions\/\"\n\t\t\t\tdata-post-title=\"Python Functions\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"94\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-basics\/python-functions\/\"\n\t\t\t\tdata-post-title=\"Python Functions\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn to develop Python functions by using the def keyword.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":37,"menu_order":19,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-94","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/94","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=94"}],"version-history":[{"count":1,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/94\/revisions"}],"predecessor-version":[{"id":6987,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/94\/revisions\/6987"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/37"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=94"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}