[{"data":1,"prerenderedAt":1996},["ShallowReactive",2],{"doc-\u002Flearn\u002Fpython-functions-explained":3},{"id":4,"title":5,"body":6,"description":1989,"extension":1990,"meta":1991,"navigation":258,"path":1992,"seo":1993,"stem":1994,"__hash__":1995},"content\u002Flearn\u002Fpython-functions-explained.md","Python Functions Explained",{"type":7,"value":8,"toc":1953},"minimark",[9,13,17,20,23,42,51,56,59,62,73,76,79,90,93,143,146,150,153,156,170,173,223,226,284,287,291,297,300,324,327,355,360,386,389,392,419,422,448,452,455,458,495,498,508,511,514,540,547,550,599,601,618,622,625,628,630,705,707,730,733,750,757,764,768,775,778,789,791,868,870,879,888,891,901,904,1027,1029,1042,1045,1059,1065,1075,1126,1128,1143,1150,1154,1157,1169,1172,1235,1237,1246,1250,1282,1285,1391,1393,1406,1413,1417,1420,1434,1437,1440,1447,1451,1454,1458,1461,1487,1490,1501,1505,1508,1549,1552,1568,1570,1582,1597,1606,1612,1617,1630,1634,1637,1640,1644,1647,1698,1703,1706,1764,1768,1771,1820,1823,1855,1865,1869,1873,1876,1880,1883,1887,1894,1898,1901,1905,1910,1914,1949],[10,11,5],"h1",{"id":12},"python-functions-explained",[14,15,16],"p",{},"Functions are one of the most useful parts of Python.",[14,18,19],{},"A function is a named block of code that does a specific job. You define it once, then call it whenever you need it. This helps you avoid repeating the same code and makes programs easier to read.",[14,21,22],{},"In this guide, you will learn:",[24,25,26,30,33,36,39],"ul",{},[27,28,29],"li",{},"what a function is",[27,31,32],{},"why functions are useful",[27,34,35],{},"how to define and call a function",[27,37,38],{},"how parameters work at a basic level",[27,40,41],{},"how return values work",[14,43,44,45,50],{},"If you want a shorter definition first, see ",[46,47,49],"a",{"href":48},"\u002Fglossary\u002Fwhat-is-a-function-in-python\u002F","what is a function in Python",".",[52,53,55],"h2",{"id":54},"what-a-function-is","What a function is",[14,57,58],{},"A function is a reusable block of code.",[14,60,61],{},"That means:",[24,63,64,67,70],{},[27,65,66],{},"you write the code once",[27,68,69],{},"you give it a name",[27,71,72],{},"you run it when needed",[14,74,75],{},"Functions are useful because they let you group related code into one place.",[14,77,78],{},"A function can:",[24,80,81,84,87],{},[27,82,83],{},"take input values",[27,85,86],{},"do some work",[27,88,89],{},"produce an output value",[14,91,92],{},"Here is a very simple function:",[94,95,100],"pre",{"className":96,"code":97,"language":98,"meta":99,"style":99},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","def say_hello():\n    print(\"Hello\")\n","python","",[101,102,103,120],"code",{"__ignoreMap":99},[104,105,108,112,116],"span",{"class":106,"line":107},"line",1,[104,109,111],{"class":110},"sbsja","def",[104,113,115],{"class":114},"sGLFI"," say_hello",[104,117,119],{"class":118},"sP7_E","():\n",[104,121,123,127,130,134,138,140],{"class":106,"line":122},2,[104,124,126],{"class":125},"sptTA","    print",[104,128,129],{"class":118},"(",[104,131,133],{"class":132},"sjJ54","\"",[104,135,137],{"class":136},"s_sjI","Hello",[104,139,133],{"class":132},[104,141,142],{"class":118},")\n",[14,144,145],{},"This creates the function, but it does not run yet.",[52,147,149],{"id":148},"why-functions-are-useful","Why functions are useful",[14,151,152],{},"Functions help you write better code.",[14,154,155],{},"They are useful because they:",[24,157,158,161,164,167],{},[27,159,160],{},"make code easier to read",[27,162,163],{},"reduce repetition",[27,165,166],{},"break large programs into smaller parts",[27,168,169],{},"make testing and debugging easier",[14,171,172],{},"For example, without a function you might repeat the same lines many times:",[94,174,176],{"className":96,"code":175,"language":98,"meta":99,"style":99},"print(\"Welcome!\")\nprint(\"Welcome!\")\nprint(\"Welcome!\")\n",[101,177,178,194,208],{"__ignoreMap":99},[104,179,180,183,185,187,190,192],{"class":106,"line":107},[104,181,182],{"class":125},"print",[104,184,129],{"class":118},[104,186,133],{"class":132},[104,188,189],{"class":136},"Welcome!",[104,191,133],{"class":132},[104,193,142],{"class":118},[104,195,196,198,200,202,204,206],{"class":106,"line":122},[104,197,182],{"class":125},[104,199,129],{"class":118},[104,201,133],{"class":132},[104,203,189],{"class":136},[104,205,133],{"class":132},[104,207,142],{"class":118},[104,209,211,213,215,217,219,221],{"class":106,"line":210},3,[104,212,182],{"class":125},[104,214,129],{"class":118},[104,216,133],{"class":132},[104,218,189],{"class":136},[104,220,133],{"class":132},[104,222,142],{"class":118},[14,224,225],{},"With a function, you can reuse the same code:",[94,227,229],{"className":96,"code":228,"language":98,"meta":99,"style":99},"def show_welcome():\n    print(\"Welcome!\")\n\nshow_welcome()\nshow_welcome()\nshow_welcome()\n",[101,230,231,240,254,260,270,277],{"__ignoreMap":99},[104,232,233,235,238],{"class":106,"line":107},[104,234,111],{"class":110},[104,236,237],{"class":114}," show_welcome",[104,239,119],{"class":118},[104,241,242,244,246,248,250,252],{"class":106,"line":122},[104,243,126],{"class":125},[104,245,129],{"class":118},[104,247,133],{"class":132},[104,249,189],{"class":136},[104,251,133],{"class":132},[104,253,142],{"class":118},[104,255,256],{"class":106,"line":210},[104,257,259],{"emptyLinePlaceholder":258},true,"\n",[104,261,263,267],{"class":106,"line":262},4,[104,264,266],{"class":265},"slqww","show_welcome",[104,268,269],{"class":118},"()\n",[104,271,273,275],{"class":106,"line":272},5,[104,274,266],{"class":265},[104,276,269],{"class":118},[104,278,280,282],{"class":106,"line":279},6,[104,281,266],{"class":265},[104,283,269],{"class":118},[14,285,286],{},"This is easier to change later. If you want to update the message, you only change it in one place.",[52,288,290],{"id":289},"how-to-define-a-function","How to define a function",[14,292,293,294,296],{},"To define a function in Python, use the ",[101,295,111],{}," keyword.",[14,298,299],{},"A basic function definition has these parts:",[24,301,302,306,309,315,321],{},[27,303,304],{},[101,305,111],{},[27,307,308],{},"the function name",[27,310,311,312],{},"parentheses ",[101,313,314],{},"()",[27,316,317,318],{},"a colon ",[101,319,320],{},":",[27,322,323],{},"an indented block of code",[14,325,326],{},"Example:",[94,328,330],{"className":96,"code":329,"language":98,"meta":99,"style":99},"def greet():\n    print(\"Hello\")\n",[101,331,332,341],{"__ignoreMap":99},[104,333,334,336,339],{"class":106,"line":107},[104,335,111],{"class":110},[104,337,338],{"class":114}," greet",[104,340,119],{"class":118},[104,342,343,345,347,349,351,353],{"class":106,"line":122},[104,344,126],{"class":125},[104,346,129],{"class":118},[104,348,133],{"class":132},[104,350,137],{"class":136},[104,352,133],{"class":132},[104,354,142],{"class":118},[356,357,359],"h3",{"id":358},"what-each-part-means","What each part means",[24,361,362,367,373,378,383],{},[27,363,364,366],{},[101,365,111],{}," tells Python you are creating a function",[27,368,369,372],{},[101,370,371],{},"greet"," is the function name",[27,374,375,377],{},[101,376,314],{}," means this function takes no input values",[27,379,380,382],{},[101,381,320],{}," starts the function body",[27,384,385],{},"the indented line is the code that belongs to the function",[14,387,388],{},"Indentation matters in Python. The code inside the function must be indented.",[14,390,391],{},"This will cause an error:",[94,393,395],{"className":96,"code":394,"language":98,"meta":99,"style":99},"def greet():\nprint(\"Hello\")\n",[101,396,397,405],{"__ignoreMap":99},[104,398,399,401,403],{"class":106,"line":107},[104,400,111],{"class":110},[104,402,338],{"class":114},[104,404,119],{"class":118},[104,406,407,409,411,413,415,417],{"class":106,"line":122},[104,408,182],{"class":125},[104,410,129],{"class":118},[104,412,133],{"class":132},[104,414,137],{"class":136},[104,416,133],{"class":132},[104,418,142],{"class":118},[14,420,421],{},"This works correctly:",[94,423,424],{"className":96,"code":329,"language":98,"meta":99,"style":99},[101,425,426,434],{"__ignoreMap":99},[104,427,428,430,432],{"class":106,"line":107},[104,429,111],{"class":110},[104,431,338],{"class":114},[104,433,119],{"class":118},[104,435,436,438,440,442,444,446],{"class":106,"line":122},[104,437,126],{"class":125},[104,439,129],{"class":118},[104,441,133],{"class":132},[104,443,137],{"class":136},[104,445,133],{"class":132},[104,447,142],{"class":118},[52,449,451],{"id":450},"how-to-call-a-function","How to call a function",[14,453,454],{},"Calling a function means running it.",[14,456,457],{},"To call a function, write its name followed by parentheses:",[94,459,461],{"className":96,"code":460,"language":98,"meta":99,"style":99},"def greet():\n    print(\"Hello\")\n\ngreet()\n",[101,462,463,471,485,489],{"__ignoreMap":99},[104,464,465,467,469],{"class":106,"line":107},[104,466,111],{"class":110},[104,468,338],{"class":114},[104,470,119],{"class":118},[104,472,473,475,477,479,481,483],{"class":106,"line":122},[104,474,126],{"class":125},[104,476,129],{"class":118},[104,478,133],{"class":132},[104,480,137],{"class":136},[104,482,133],{"class":132},[104,484,142],{"class":118},[104,486,487],{"class":106,"line":210},[104,488,259],{"emptyLinePlaceholder":258},[104,490,491,493],{"class":106,"line":262},[104,492,371],{"class":265},[104,494,269],{"class":118},[14,496,497],{},"Output:",[94,499,501],{"className":96,"code":500,"language":98,"meta":99,"style":99},"Hello\n",[101,502,503],{"__ignoreMap":99},[104,504,505],{"class":106,"line":107},[104,506,500],{"class":507},"su5hD",[14,509,510],{},"A function does nothing until it is called.",[14,512,513],{},"In this example, the function is defined but never used:",[94,515,516],{"className":96,"code":329,"language":98,"meta":99,"style":99},[101,517,518,526],{"__ignoreMap":99},[104,519,520,522,524],{"class":106,"line":107},[104,521,111],{"class":110},[104,523,338],{"class":114},[104,525,119],{"class":118},[104,527,528,530,532,534,536,538],{"class":106,"line":122},[104,529,126],{"class":125},[104,531,129],{"class":118},[104,533,133],{"class":132},[104,535,137],{"class":136},[104,537,133],{"class":132},[104,539,142],{"class":118},[14,541,542,543,546],{},"Since there is no ",[101,544,545],{},"greet()"," call, nothing is printed.",[14,548,549],{},"You can call the same function many times:",[94,551,553],{"className":96,"code":552,"language":98,"meta":99,"style":99},"def greet():\n    print(\"Hello\")\n\ngreet()\ngreet()\ngreet()\n",[101,554,555,563,577,581,587,593],{"__ignoreMap":99},[104,556,557,559,561],{"class":106,"line":107},[104,558,111],{"class":110},[104,560,338],{"class":114},[104,562,119],{"class":118},[104,564,565,567,569,571,573,575],{"class":106,"line":122},[104,566,126],{"class":125},[104,568,129],{"class":118},[104,570,133],{"class":132},[104,572,137],{"class":136},[104,574,133],{"class":132},[104,576,142],{"class":118},[104,578,579],{"class":106,"line":210},[104,580,259],{"emptyLinePlaceholder":258},[104,582,583,585],{"class":106,"line":262},[104,584,371],{"class":265},[104,586,269],{"class":118},[104,588,589,591],{"class":106,"line":272},[104,590,371],{"class":265},[104,592,269],{"class":118},[104,594,595,597],{"class":106,"line":279},[104,596,371],{"class":265},[104,598,269],{"class":118},[14,600,497],{},[94,602,604],{"className":96,"code":603,"language":98,"meta":99,"style":99},"Hello\nHello\nHello\n",[101,605,606,610,614],{"__ignoreMap":99},[104,607,608],{"class":106,"line":107},[104,609,500],{"class":507},[104,611,612],{"class":106,"line":122},[104,613,500],{"class":507},[104,615,616],{"class":106,"line":210},[104,617,500],{"class":507},[52,619,621],{"id":620},"functions-with-parameters","Functions with parameters",[14,623,624],{},"Parameters are names for input values inside a function.",[14,626,627],{},"They let one function work with different data.",[14,629,326],{},[94,631,633],{"className":96,"code":632,"language":98,"meta":99,"style":99},"def greet(name):\n    print(\"Hello,\", name)\n\ngreet(\"Ava\")\ngreet(\"Noah\")\n",[101,634,635,650,671,675,690],{"__ignoreMap":99},[104,636,637,639,641,643,647],{"class":106,"line":107},[104,638,111],{"class":110},[104,640,338],{"class":114},[104,642,129],{"class":118},[104,644,646],{"class":645},"sFwrP","name",[104,648,649],{"class":118},"):\n",[104,651,652,654,656,658,661,663,666,669],{"class":106,"line":122},[104,653,126],{"class":125},[104,655,129],{"class":118},[104,657,133],{"class":132},[104,659,660],{"class":136},"Hello,",[104,662,133],{"class":132},[104,664,665],{"class":118},",",[104,667,668],{"class":265}," name",[104,670,142],{"class":118},[104,672,673],{"class":106,"line":210},[104,674,259],{"emptyLinePlaceholder":258},[104,676,677,679,681,683,686,688],{"class":106,"line":262},[104,678,371],{"class":265},[104,680,129],{"class":118},[104,682,133],{"class":132},[104,684,685],{"class":136},"Ava",[104,687,133],{"class":132},[104,689,142],{"class":118},[104,691,692,694,696,698,701,703],{"class":106,"line":272},[104,693,371],{"class":265},[104,695,129],{"class":118},[104,697,133],{"class":132},[104,699,700],{"class":136},"Noah",[104,702,133],{"class":132},[104,704,142],{"class":118},[14,706,497],{},[94,708,710],{"className":96,"code":709,"language":98,"meta":99,"style":99},"Hello, Ava\nHello, Noah\n",[101,711,712,721],{"__ignoreMap":99},[104,713,714,716,718],{"class":106,"line":107},[104,715,137],{"class":507},[104,717,665],{"class":118},[104,719,720],{"class":507}," Ava\n",[104,722,723,725,727],{"class":106,"line":122},[104,724,137],{"class":507},[104,726,665],{"class":118},[104,728,729],{"class":507}," Noah\n",[14,731,732],{},"In this example:",[24,734,735,740],{},[27,736,737,739],{},[101,738,646],{}," is the parameter",[27,741,742,745,746,749],{},[101,743,744],{},"\"Ava\""," and ",[101,747,748],{},"\"Noah\""," are arguments",[14,751,752,753,756],{},"A parameter is the variable name in the function definition.",[754,755],"br",{},"\nAn argument is the value you pass when calling the function.",[14,758,759,760,50],{},"This is just the basic idea. For a full beginner guide, see ",[46,761,763],{"href":762},"\u002Flearn\u002Ffunction-parameters-and-arguments-in-python\u002F","function parameters and arguments in Python",[52,765,767],{"id":766},"return-values","Return values",[14,769,770,771,774],{},"Use ",[101,772,773],{},"return"," when you want a function to send a value back.",[14,776,777],{},"That returned value can then be:",[24,779,780,783,786],{},[27,781,782],{},"stored in a variable",[27,784,785],{},"used in another expression",[27,787,788],{},"printed later",[14,790,326],{},[94,792,794],{"className":96,"code":793,"language":98,"meta":99,"style":99},"def add_numbers(a, b):\n    return a + b\n\nresult = add_numbers(3, 4)\nprint(result)\n",[101,795,796,814,830,834,857],{"__ignoreMap":99},[104,797,798,800,803,805,807,809,812],{"class":106,"line":107},[104,799,111],{"class":110},[104,801,802],{"class":114}," add_numbers",[104,804,129],{"class":118},[104,806,46],{"class":645},[104,808,665],{"class":118},[104,810,811],{"class":645}," b",[104,813,649],{"class":118},[104,815,816,820,823,827],{"class":106,"line":122},[104,817,819],{"class":818},"sVHd0","    return",[104,821,822],{"class":507}," a ",[104,824,826],{"class":825},"smGrS","+",[104,828,829],{"class":507}," b\n",[104,831,832],{"class":106,"line":210},[104,833,259],{"emptyLinePlaceholder":258},[104,835,836,839,842,844,846,850,852,855],{"class":106,"line":262},[104,837,838],{"class":507},"result ",[104,840,841],{"class":825},"=",[104,843,802],{"class":265},[104,845,129],{"class":118},[104,847,849],{"class":848},"srdBf","3",[104,851,665],{"class":118},[104,853,854],{"class":848}," 4",[104,856,142],{"class":118},[104,858,859,861,863,866],{"class":106,"line":272},[104,860,182],{"class":125},[104,862,129],{"class":118},[104,864,865],{"class":265},"result",[104,867,142],{"class":118},[14,869,497],{},[94,871,873],{"className":96,"code":872,"language":98,"meta":99,"style":99},"7\n",[101,874,875],{"__ignoreMap":99},[104,876,877],{"class":106,"line":107},[104,878,872],{"class":848},[356,880,882,745,885,887],{"id":881},"print-and-return-are-not-the-same",[101,883,884],{},"print()",[101,886,773],{}," are not the same",[14,889,890],{},"This is a very common beginner mistake.",[14,892,893,895,896,898,900],{},[101,894,884],{}," shows something on the screen.",[754,897],{},[101,899,773],{}," sends a value back from the function.",[14,902,903],{},"Compare these two functions:",[94,905,907],{"className":96,"code":906,"language":98,"meta":99,"style":99},"def show_sum(a, b):\n    print(a + b)\n\ndef get_sum(a, b):\n    return a + b\n\nshow_sum(3, 4)\n\nresult = get_sum(3, 4)\nprint(result)\n",[101,908,909,926,941,945,962,972,976,992,997,1016],{"__ignoreMap":99},[104,910,911,913,916,918,920,922,924],{"class":106,"line":107},[104,912,111],{"class":110},[104,914,915],{"class":114}," show_sum",[104,917,129],{"class":118},[104,919,46],{"class":645},[104,921,665],{"class":118},[104,923,811],{"class":645},[104,925,649],{"class":118},[104,927,928,930,932,935,937,939],{"class":106,"line":122},[104,929,126],{"class":125},[104,931,129],{"class":118},[104,933,934],{"class":265},"a ",[104,936,826],{"class":825},[104,938,811],{"class":265},[104,940,142],{"class":118},[104,942,943],{"class":106,"line":210},[104,944,259],{"emptyLinePlaceholder":258},[104,946,947,949,952,954,956,958,960],{"class":106,"line":262},[104,948,111],{"class":110},[104,950,951],{"class":114}," get_sum",[104,953,129],{"class":118},[104,955,46],{"class":645},[104,957,665],{"class":118},[104,959,811],{"class":645},[104,961,649],{"class":118},[104,963,964,966,968,970],{"class":106,"line":272},[104,965,819],{"class":818},[104,967,822],{"class":507},[104,969,826],{"class":825},[104,971,829],{"class":507},[104,973,974],{"class":106,"line":279},[104,975,259],{"emptyLinePlaceholder":258},[104,977,979,982,984,986,988,990],{"class":106,"line":978},7,[104,980,981],{"class":265},"show_sum",[104,983,129],{"class":118},[104,985,849],{"class":848},[104,987,665],{"class":118},[104,989,854],{"class":848},[104,991,142],{"class":118},[104,993,995],{"class":106,"line":994},8,[104,996,259],{"emptyLinePlaceholder":258},[104,998,1000,1002,1004,1006,1008,1010,1012,1014],{"class":106,"line":999},9,[104,1001,838],{"class":507},[104,1003,841],{"class":825},[104,1005,951],{"class":265},[104,1007,129],{"class":118},[104,1009,849],{"class":848},[104,1011,665],{"class":118},[104,1013,854],{"class":848},[104,1015,142],{"class":118},[104,1017,1019,1021,1023,1025],{"class":106,"line":1018},10,[104,1020,182],{"class":125},[104,1022,129],{"class":118},[104,1024,865],{"class":265},[104,1026,142],{"class":118},[14,1028,497],{},[94,1030,1032],{"className":96,"code":1031,"language":98,"meta":99,"style":99},"7\n7\n",[101,1033,1034,1038],{"__ignoreMap":99},[104,1035,1036],{"class":106,"line":107},[104,1037,872],{"class":848},[104,1039,1040],{"class":106,"line":122},[104,1041,872],{"class":848},[14,1043,1044],{},"The output looks similar, but the functions behave differently.",[24,1046,1047,1053],{},[27,1048,1049,1052],{},[101,1050,1051],{},"show_sum()"," prints the result, but does not return it",[27,1054,1055,1058],{},[101,1056,1057],{},"get_sum()"," returns the result, so you can store and reuse it",[356,1060,1062,1063],{"id":1061},"what-happens-if-there-is-no-return","What happens if there is no ",[101,1064,773],{},[14,1066,1067,1068,1070,1071,1074],{},"If a function does not use ",[101,1069,773],{},", Python returns ",[101,1072,1073],{},"None"," by default.",[94,1076,1078],{"className":96,"code":1077,"language":98,"meta":99,"style":99},"def greet():\n    print(\"Hello\")\n\nresult = greet()\nprint(result)\n",[101,1079,1080,1088,1102,1106,1116],{"__ignoreMap":99},[104,1081,1082,1084,1086],{"class":106,"line":107},[104,1083,111],{"class":110},[104,1085,338],{"class":114},[104,1087,119],{"class":118},[104,1089,1090,1092,1094,1096,1098,1100],{"class":106,"line":122},[104,1091,126],{"class":125},[104,1093,129],{"class":118},[104,1095,133],{"class":132},[104,1097,137],{"class":136},[104,1099,133],{"class":132},[104,1101,142],{"class":118},[104,1103,1104],{"class":106,"line":210},[104,1105,259],{"emptyLinePlaceholder":258},[104,1107,1108,1110,1112,1114],{"class":106,"line":262},[104,1109,838],{"class":507},[104,1111,841],{"class":825},[104,1113,338],{"class":265},[104,1115,269],{"class":118},[104,1117,1118,1120,1122,1124],{"class":106,"line":272},[104,1119,182],{"class":125},[104,1121,129],{"class":118},[104,1123,865],{"class":265},[104,1125,142],{"class":118},[14,1127,497],{},[94,1129,1131],{"className":96,"code":1130,"language":98,"meta":99,"style":99},"Hello\nNone\n",[101,1132,1133,1137],{"__ignoreMap":99},[104,1134,1135],{"class":106,"line":107},[104,1136,500],{"class":507},[104,1138,1139],{"class":106,"line":122},[104,1140,1142],{"class":1141},"s39Yj","None\n",[14,1144,1145,1146,50],{},"If you want to learn this in more detail, see ",[46,1147,1149],{"href":1148},"\u002Flearn\u002Freturn-values-in-python-functions\u002F","return values in Python functions",[52,1151,1153],{"id":1152},"simple-example-flow","Simple example flow",[14,1155,1156],{},"A good way to understand functions is to follow the usual order:",[1158,1159,1160,1163,1166],"ol",{},[27,1161,1162],{},"define the function",[27,1164,1165],{},"call the function",[27,1167,1168],{},"see the result",[14,1170,1171],{},"Here is a simple example:",[94,1173,1175],{"className":96,"code":1174,"language":98,"meta":99,"style":99},"def multiply_by_two(number):\n    return number * 2\n\nanswer = multiply_by_two(5)\nprint(answer)\n",[101,1176,1177,1191,1204,1208,1224],{"__ignoreMap":99},[104,1178,1179,1181,1184,1186,1189],{"class":106,"line":107},[104,1180,111],{"class":110},[104,1182,1183],{"class":114}," multiply_by_two",[104,1185,129],{"class":118},[104,1187,1188],{"class":645},"number",[104,1190,649],{"class":118},[104,1192,1193,1195,1198,1201],{"class":106,"line":122},[104,1194,819],{"class":818},[104,1196,1197],{"class":507}," number ",[104,1199,1200],{"class":825},"*",[104,1202,1203],{"class":848}," 2\n",[104,1205,1206],{"class":106,"line":210},[104,1207,259],{"emptyLinePlaceholder":258},[104,1209,1210,1213,1215,1217,1219,1222],{"class":106,"line":262},[104,1211,1212],{"class":507},"answer ",[104,1214,841],{"class":825},[104,1216,1183],{"class":265},[104,1218,129],{"class":118},[104,1220,1221],{"class":848},"5",[104,1223,142],{"class":118},[104,1225,1226,1228,1230,1233],{"class":106,"line":272},[104,1227,182],{"class":125},[104,1229,129],{"class":118},[104,1231,1232],{"class":265},"answer",[104,1234,142],{"class":118},[14,1236,497],{},[94,1238,1240],{"className":96,"code":1239,"language":98,"meta":99,"style":99},"10\n",[101,1241,1242],{"__ignoreMap":99},[104,1243,1244],{"class":106,"line":107},[104,1245,1239],{"class":848},[356,1247,1249],{"id":1248},"step-by-step","Step by step",[24,1251,1252,1258,1263,1269,1277],{},[27,1253,1254,1257],{},[101,1255,1256],{},"def multiply_by_two(number):"," defines the function",[27,1259,1260,1262],{},[101,1261,1188],{}," is the input",[27,1264,1265,1268],{},[101,1266,1267],{},"return number * 2"," sends back the result",[27,1270,1271,1274,1275],{},[101,1272,1273],{},"multiply_by_two(5)"," calls the function with ",[101,1276,1221],{},[27,1278,1279,1280],{},"the returned value is stored in ",[101,1281,1232],{},[14,1283,1284],{},"Now compare a print-based function and a return-based function:",[94,1286,1288],{"className":96,"code":1287,"language":98,"meta":99,"style":99},"def print_double(number):\n    print(number * 2)\n\ndef return_double(number):\n    return number * 2\n\nprint_double(5)\n\nvalue = return_double(5)\nprint(value)\n",[101,1289,1290,1303,1319,1323,1336,1346,1350,1361,1365,1380],{"__ignoreMap":99},[104,1291,1292,1294,1297,1299,1301],{"class":106,"line":107},[104,1293,111],{"class":110},[104,1295,1296],{"class":114}," print_double",[104,1298,129],{"class":118},[104,1300,1188],{"class":645},[104,1302,649],{"class":118},[104,1304,1305,1307,1309,1312,1314,1317],{"class":106,"line":122},[104,1306,126],{"class":125},[104,1308,129],{"class":118},[104,1310,1311],{"class":265},"number ",[104,1313,1200],{"class":825},[104,1315,1316],{"class":848}," 2",[104,1318,142],{"class":118},[104,1320,1321],{"class":106,"line":210},[104,1322,259],{"emptyLinePlaceholder":258},[104,1324,1325,1327,1330,1332,1334],{"class":106,"line":262},[104,1326,111],{"class":110},[104,1328,1329],{"class":114}," return_double",[104,1331,129],{"class":118},[104,1333,1188],{"class":645},[104,1335,649],{"class":118},[104,1337,1338,1340,1342,1344],{"class":106,"line":272},[104,1339,819],{"class":818},[104,1341,1197],{"class":507},[104,1343,1200],{"class":825},[104,1345,1203],{"class":848},[104,1347,1348],{"class":106,"line":279},[104,1349,259],{"emptyLinePlaceholder":258},[104,1351,1352,1355,1357,1359],{"class":106,"line":978},[104,1353,1354],{"class":265},"print_double",[104,1356,129],{"class":118},[104,1358,1221],{"class":848},[104,1360,142],{"class":118},[104,1362,1363],{"class":106,"line":994},[104,1364,259],{"emptyLinePlaceholder":258},[104,1366,1367,1370,1372,1374,1376,1378],{"class":106,"line":999},[104,1368,1369],{"class":507},"value ",[104,1371,841],{"class":825},[104,1373,1329],{"class":265},[104,1375,129],{"class":118},[104,1377,1221],{"class":848},[104,1379,142],{"class":118},[104,1381,1382,1384,1386,1389],{"class":106,"line":1018},[104,1383,182],{"class":125},[104,1385,129],{"class":118},[104,1387,1388],{"class":265},"value",[104,1390,142],{"class":118},[14,1392,497],{},[94,1394,1396],{"className":96,"code":1395,"language":98,"meta":99,"style":99},"10\n10\n",[101,1397,1398,1402],{"__ignoreMap":99},[104,1399,1400],{"class":106,"line":107},[104,1401,1239],{"class":848},[104,1403,1404],{"class":106,"line":122},[104,1405,1239],{"class":848},[14,1407,1408,1409,1412],{},"Again, the screen output is similar, but only ",[101,1410,1411],{},"return_double()"," gives you a value you can save and use later.",[52,1414,1416],{"id":1415},"when-to-use-a-function","When to use a function",[14,1418,1419],{},"Use a function when:",[24,1421,1422,1425,1428,1431],{},[27,1423,1424],{},"the same task appears more than once",[27,1426,1427],{},"one part of the program should do one clear job",[27,1429,1430],{},"you want code that is easier to reuse",[27,1432,1433],{},"a long script needs to be split into smaller pieces",[14,1435,1436],{},"For example, if your program needs to calculate tax in several places, a function is better than repeating the same formula again and again.",[14,1438,1439],{},"Functions also make your code easier to understand. A good function name can explain what that part of the program does.",[14,1441,1442,1443,50],{},"If you want to practice writing one yourself, see ",[46,1444,1446],{"href":1445},"\u002Fhow-to\u002Fhow-to-create-a-simple-function-in-python\u002F","how to create a simple function in Python",[52,1448,1450],{"id":1449},"common-mistakes","Common mistakes",[14,1452,1453],{},"Beginners often run into the same problems when learning functions.",[356,1455,1457],{"id":1456},"forgetting-to-call-the-function","Forgetting to call the function",[14,1459,1460],{},"Defining a function does not run it.",[94,1462,1463],{"className":96,"code":329,"language":98,"meta":99,"style":99},[101,1464,1465,1473],{"__ignoreMap":99},[104,1466,1467,1469,1471],{"class":106,"line":107},[104,1468,111],{"class":110},[104,1470,338],{"class":114},[104,1472,119],{"class":118},[104,1474,1475,1477,1479,1481,1483,1485],{"class":106,"line":122},[104,1476,126],{"class":125},[104,1478,129],{"class":118},[104,1480,133],{"class":132},[104,1482,137],{"class":136},[104,1484,133],{"class":132},[104,1486,142],{"class":118},[14,1488,1489],{},"Nothing happens until you do this:",[94,1491,1493],{"className":96,"code":1492,"language":98,"meta":99,"style":99},"greet()\n",[101,1494,1495],{"__ignoreMap":99},[104,1496,1497,1499],{"class":106,"line":107},[104,1498,371],{"class":265},[104,1500,269],{"class":118},[356,1502,1504],{"id":1503},"missing-parentheses-when-calling-a-function","Missing parentheses when calling a function",[14,1506,1507],{},"This prints the function object, not the result:",[94,1509,1511],{"className":96,"code":1510,"language":98,"meta":99,"style":99},"def greet():\n    print(\"Hello\")\n\nprint(greet)\n",[101,1512,1513,1521,1535,1539],{"__ignoreMap":99},[104,1514,1515,1517,1519],{"class":106,"line":107},[104,1516,111],{"class":110},[104,1518,338],{"class":114},[104,1520,119],{"class":118},[104,1522,1523,1525,1527,1529,1531,1533],{"class":106,"line":122},[104,1524,126],{"class":125},[104,1526,129],{"class":118},[104,1528,133],{"class":132},[104,1530,137],{"class":136},[104,1532,133],{"class":132},[104,1534,142],{"class":118},[104,1536,1537],{"class":106,"line":210},[104,1538,259],{"emptyLinePlaceholder":258},[104,1540,1541,1543,1545,1547],{"class":106,"line":262},[104,1542,182],{"class":125},[104,1544,129],{"class":118},[104,1546,371],{"class":265},[104,1548,142],{"class":118},[14,1550,1551],{},"You need parentheses to call it:",[94,1553,1555],{"className":96,"code":1554,"language":98,"meta":99,"style":99},"print(greet())\n",[101,1556,1557],{"__ignoreMap":99},[104,1558,1559,1561,1563,1565],{"class":106,"line":107},[104,1560,182],{"class":125},[104,1562,129],{"class":118},[104,1564,371],{"class":265},[104,1566,1567],{"class":118},"())\n",[14,1569,497],{},[94,1571,1572],{"className":96,"code":1130,"language":98,"meta":99,"style":99},[101,1573,1574,1578],{"__ignoreMap":99},[104,1575,1576],{"class":106,"line":107},[104,1577,500],{"class":507},[104,1579,1580],{"class":106,"line":122},[104,1581,1142],{"class":1141},[14,1583,1584,1585,1587,1588,1590,1591,1593,1594,1596],{},"The ",[101,1586,137],{}," comes from inside the function.",[754,1589],{},"\nThe ",[101,1592,1073],{}," appears because ",[101,1595,545],{}," does not return a value.",[356,1598,1600,1601,1603,1604],{"id":1599},"confusing-print-with-return","Confusing ",[101,1602,884],{}," with ",[101,1605,773],{},[14,1607,1608,1609,1611],{},"If you need to use the result later, ",[101,1610,884],{}," is not enough.",[14,1613,770,1614,1616],{},[101,1615,773],{}," instead.",[14,1618,1619,1620,1622,1623,50],{},"If you are unsure how ",[101,1621,884],{}," works, see ",[46,1624,1626,1627,1629],{"href":1625},"\u002Freference\u002Fpython-print-function-explained\u002F","Python ",[101,1628,884],{}," function explained",[356,1631,1633],{"id":1632},"using-bad-indentation-inside-the-function","Using bad indentation inside the function",[14,1635,1636],{},"Python uses indentation to know which lines belong to the function.",[14,1638,1639],{},"Incorrect indentation will cause an error or change how your code behaves.",[356,1641,1643],{"id":1642},"trying-to-use-a-variable-from-inside-the-function-outside-of-it","Trying to use a variable from inside the function outside of it",[14,1645,1646],{},"This does not work:",[94,1648,1650],{"className":96,"code":1649,"language":98,"meta":99,"style":99},"def make_message():\n    message = \"Hello\"\n\nmake_message()\nprint(message)\n",[101,1651,1652,1661,1676,1680,1687],{"__ignoreMap":99},[104,1653,1654,1656,1659],{"class":106,"line":107},[104,1655,111],{"class":110},[104,1657,1658],{"class":114}," make_message",[104,1660,119],{"class":118},[104,1662,1663,1666,1668,1671,1673],{"class":106,"line":122},[104,1664,1665],{"class":507},"    message ",[104,1667,841],{"class":825},[104,1669,1670],{"class":132}," \"",[104,1672,137],{"class":136},[104,1674,1675],{"class":132},"\"\n",[104,1677,1678],{"class":106,"line":210},[104,1679,259],{"emptyLinePlaceholder":258},[104,1681,1682,1685],{"class":106,"line":262},[104,1683,1684],{"class":265},"make_message",[104,1686,269],{"class":118},[104,1688,1689,1691,1693,1696],{"class":106,"line":272},[104,1690,182],{"class":125},[104,1692,129],{"class":118},[104,1694,1695],{"class":265},"message",[104,1697,142],{"class":118},[14,1699,1700,1702],{},[101,1701,1695],{}," only exists inside the function.",[14,1704,1705],{},"A simple fix is to return it:",[94,1707,1709],{"className":96,"code":1708,"language":98,"meta":99,"style":99},"def make_message():\n    message = \"Hello\"\n    return message\n\ntext = make_message()\nprint(text)\n",[101,1710,1711,1719,1731,1738,1742,1753],{"__ignoreMap":99},[104,1712,1713,1715,1717],{"class":106,"line":107},[104,1714,111],{"class":110},[104,1716,1658],{"class":114},[104,1718,119],{"class":118},[104,1720,1721,1723,1725,1727,1729],{"class":106,"line":122},[104,1722,1665],{"class":507},[104,1724,841],{"class":825},[104,1726,1670],{"class":132},[104,1728,137],{"class":136},[104,1730,1675],{"class":132},[104,1732,1733,1735],{"class":106,"line":210},[104,1734,819],{"class":818},[104,1736,1737],{"class":507}," message\n",[104,1739,1740],{"class":106,"line":262},[104,1741,259],{"emptyLinePlaceholder":258},[104,1743,1744,1747,1749,1751],{"class":106,"line":272},[104,1745,1746],{"class":507},"text ",[104,1748,841],{"class":825},[104,1750,1658],{"class":265},[104,1752,269],{"class":118},[104,1754,1755,1757,1759,1762],{"class":106,"line":279},[104,1756,182],{"class":125},[104,1758,129],{"class":118},[104,1760,1761],{"class":265},"text",[104,1763,142],{"class":118},[356,1765,1767],{"id":1766},"simple-debugging-checks","Simple debugging checks",[14,1769,1770],{},"These commands can help you understand what is happening:",[94,1772,1774],{"className":96,"code":1773,"language":98,"meta":99,"style":99},"print(my_function)\nprint(my_function())\nhelp(print)\ntype(my_function)\n",[101,1775,1776,1787,1797,1808],{"__ignoreMap":99},[104,1777,1778,1780,1782,1785],{"class":106,"line":107},[104,1779,182],{"class":125},[104,1781,129],{"class":118},[104,1783,1784],{"class":265},"my_function",[104,1786,142],{"class":118},[104,1788,1789,1791,1793,1795],{"class":106,"line":122},[104,1790,182],{"class":125},[104,1792,129],{"class":118},[104,1794,1784],{"class":265},[104,1796,1567],{"class":118},[104,1798,1799,1802,1804,1806],{"class":106,"line":210},[104,1800,1801],{"class":125},"help",[104,1803,129],{"class":118},[104,1805,182],{"class":125},[104,1807,142],{"class":118},[104,1809,1810,1814,1816,1818],{"class":106,"line":262},[104,1811,1813],{"class":1812},"sZMiF","type",[104,1815,129],{"class":118},[104,1817,1784],{"class":265},[104,1819,142],{"class":118},[14,1821,1822],{},"What they do:",[24,1824,1825,1834,1840,1849],{},[27,1826,1827,1830,1831,1833],{},[101,1828,1829],{},"print(my_function)"," shows that ",[101,1832,1784],{}," is a function",[27,1835,1836,1839],{},[101,1837,1838],{},"print(my_function())"," runs the function and prints its return value",[27,1841,1842,1845,1846,1848],{},[101,1843,1844],{},"help(print)"," shows help for the built-in ",[101,1847,884],{}," function",[27,1850,1851,1854],{},[101,1852,1853],{},"type(my_function)"," shows the object type",[14,1856,1857,1858,50],{},"If a function call gives an argument error, you may also need ",[46,1859,1861,1862],{"href":1860},"\u002Ferrors\u002Ftypeerror-missing-required-positional-argument-fix\u002F","how to fix ",[101,1863,1864],{},"TypeError: missing required positional argument",[52,1866,1868],{"id":1867},"faq","FAQ",[356,1870,1872],{"id":1871},"what-is-a-function-in-python","What is a function in Python?",[14,1874,1875],{},"A function is a named block of code that runs when you call it.",[356,1877,1879],{"id":1878},"why-should-i-use-functions","Why should I use functions?",[14,1881,1882],{},"Functions help you reuse code, reduce repetition, and keep programs organized.",[356,1884,1886],{"id":1885},"what-is-the-difference-between-print-and-return","What is the difference between print and return?",[14,1888,1889,1891,1892,900],{},[101,1890,182],{}," shows a value on the screen. ",[101,1893,773],{},[356,1895,1897],{"id":1896},"can-a-function-run-without-parameters","Can a function run without parameters?",[14,1899,1900],{},"Yes. Some functions take no input values.",[356,1902,1904],{"id":1903},"what-happens-if-i-do-not-use-return","What happens if I do not use return?",[14,1906,1907,1908,1074],{},"The function returns ",[101,1909,1073],{},[52,1911,1913],{"id":1912},"see-also","See also",[24,1915,1916,1921,1926,1931,1936,1942],{},[27,1917,1918],{},[46,1919,1920],{"href":48},"What is a function in Python",[27,1922,1923],{},[46,1924,1925],{"href":762},"Function parameters and arguments in Python",[27,1927,1928],{},[46,1929,1930],{"href":1148},"Return values in Python functions",[27,1932,1933],{},[46,1934,1935],{"href":1445},"How to create a simple function in Python",[27,1937,1938],{},[46,1939,1626,1940,1629],{"href":1625},[101,1941,884],{},[27,1943,1944],{},[46,1945,1946,1947],{"href":1860},"How to fix ",[101,1948,1864],{},[1950,1951,1952],"style",{},"html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sGLFI, html code.shiki .sGLFI{--shiki-light:#6182B8;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sFwrP, html code.shiki .sFwrP{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#24292E;--shiki-default-font-style:inherit;--shiki-dark:#E1E4E8;--shiki-dark-font-style:inherit}html pre.shiki code .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":99,"searchDepth":122,"depth":122,"links":1954},[1955,1956,1957,1960,1961,1962,1968,1971,1972,1981,1988],{"id":54,"depth":122,"text":55},{"id":148,"depth":122,"text":149},{"id":289,"depth":122,"text":290,"children":1958},[1959],{"id":358,"depth":210,"text":359},{"id":450,"depth":122,"text":451},{"id":620,"depth":122,"text":621},{"id":766,"depth":122,"text":767,"children":1963},[1964,1966],{"id":881,"depth":210,"text":1965},"print() and return are not the same",{"id":1061,"depth":210,"text":1967},"What happens if there is no return",{"id":1152,"depth":122,"text":1153,"children":1969},[1970],{"id":1248,"depth":210,"text":1249},{"id":1415,"depth":122,"text":1416},{"id":1449,"depth":122,"text":1450,"children":1973},[1974,1975,1976,1978,1979,1980],{"id":1456,"depth":210,"text":1457},{"id":1503,"depth":210,"text":1504},{"id":1599,"depth":210,"text":1977},"Confusing print() with return",{"id":1632,"depth":210,"text":1633},{"id":1642,"depth":210,"text":1643},{"id":1766,"depth":210,"text":1767},{"id":1867,"depth":122,"text":1868,"children":1982},[1983,1984,1985,1986,1987],{"id":1871,"depth":210,"text":1872},{"id":1878,"depth":210,"text":1879},{"id":1885,"depth":210,"text":1886},{"id":1896,"depth":210,"text":1897},{"id":1903,"depth":210,"text":1904},{"id":1912,"depth":122,"text":1913},"Master python functions explained in our comprehensive Python beginner guide.","md",{},"\u002Flearn\u002Fpython-functions-explained",{"title":5,"description":1989},"learn\u002Fpython-functions-explained","QIoG9mhR-grFFnDK5mfIGJ7M989eJQze6BGFR5TaBJI",1777585495405]