{"id":15887,"date":"2020-12-10T22:53:24","date_gmt":"2020-12-11T06:53:24","guid":{"rendered":"https:\/\/careerkarma.com\/blog\/?p=15887"},"modified":"2023-12-01T04:05:49","modified_gmt":"2023-12-01T12:05:49","slug":"python-functions","status":"publish","type":"post","link":"https:\/\/careerkarma.com\/blog\/python-functions\/","title":{"rendered":"How to Write Python Functions"},"content":{"rendered":"\n<p><em>A Python function is a group of code. To run the code in a function, you must call the function. A function can be called from anywhere after the function is defined. Functions can return a value using a return statement.<\/em><\/p>\n\n\n\n<p>Functions are a common feature among all programming languages. They allow developers to write blocks of code that perform specific tasks. A function can be executed as many times as a developer wants throughout their code.<\/p>\n\n\n\n<p>Functions allow developers to reduce repetition in their code because they can execute the same block of code multiple times over in one program.<\/p>\n\n\n\n<p>This tutorial will discuss, with examples, the basics of Python functions, how to create and call a function, and how to work with arguments. By the end of reading this tutorial, you\u2019ll be an expert at writing functions in Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is a Python Function?<\/h2>\n\n\n\n<p>A function is a block of code that only runs when it is called. Python functions return a value using a return statement, if one is specified. A function can be called anywhere after the function has been declared.<\/p>\n\n\n\n<p>By itself, a function does nothing. But, when you need to use a function, you can call it, and the code within the function will be executed.<\/p>\n\n\n\n<p>In Python, there are two types of functions: user-defined and built-in. Built-in functions are functions like:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><em>print()<\/em>, which prints a statement to the console<\/li><li><a href=\"https:\/\/careerkarma.com\/blog\/python-len\/\">Python <em>len()<\/em><\/a>, which calculates the length of a list<\/li><li>Python <em>str()<\/em>, which converts a value to a string<\/li><\/ul>\n\n\n\n<p>User-defined functions are reusable blocks of code written by you, the developer. These blocks of code allow you to organize your code more efficiently. This is important because the more organized your code is, the easier it will be to maintain.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to Define a Python Function<\/h2>\n\n\n\n<p>Defining a function refers to creating the function. This involves writing a block of code that we can call by referencing the name of our function. A function is denoted by the def keyword, followed by a function name, and a set of parenthesis.<\/p>\n\n\n\n<p>For this example, we\u2019re going to create a simple function that prints out the statement <em>It\u2019s Monday!<\/em> to the console. To do so, we can use this code:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>def print_monday():\n\tprint(&quot;It's Monday!&quot;)<\/pre><\/div>\n\n\n\n<p>When we run our code, nothing happens. This is because, in order for our function to run, we need to call it. To do so, we can reference our function name like so:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>def print_monday():\n\tprint(&quot;It's Monday!&quot;)\n\nprint_monday()<\/pre><\/div>\n\n\n\n<p>Our code returns: <\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>It's Monday!<\/pre><\/div>\n\n\n\n<p>Let\u2019s break down the main components of our function:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>the <strong>def<\/strong> keyword is used to indicate that we want to create a function.<\/li><li><strong>print_monday<\/strong> is the name of our function. This must be unique.<\/li><li><strong>()<\/strong> is where our parameters will be stored. We\u2019ll talk about this later.<\/li><li><strong>:<\/strong> marks the end of the header of our function.<\/li><\/ul>\n\n\n\n<p>Now, our functions can get as complex as we want them to be. Suppose we want to write a program that tells a user how many letters are in their name. We could do so using this code:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>def calculate_name_length():\n\tname = str(input(&quot;What is your name? &quot;))\n\tname_length = len(name)\n\tprint(&quot;The length of your name is &quot;, name_length, &quot; letters.&quot;)\n\ncalculateNameLength()<\/pre><\/div>\n\n\n\n<p>If we run our code and type in the name \u201cElizabeth\u201d, the following response is returned: <\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>The length of your name is 9 letters.<\/pre><\/div>\n\n\n\n<p>We define a function called <em>calculate_name_length()<\/em>. In the function body, we ask the user for their name, then use <em>len()<\/em> to calculate the length of the user\u2019s name. Finally, we print \u201cThe length of your name is [length] letters.\u201d, where length is the length of the user\u2019s name, to the console.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Function Parameters and Arguments<\/h2>\n\n\n\n<p>In our first examples, we used empty parenthesis with our functions. This means that our functions did not accept any arguments.<\/p>\n\n\n\n<p>Arguments allow you to pass information into a function that the function can read. The arguments for a function are enclosed within the parentheses that follow the function\u2019s name.<\/p>\n\n\n\n<p>Let\u2019s walk through a basic example to illustrate how arguments work.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Python Parameters and Arguments Example<\/h3>\n\n\n\n<p>Suppose we want to create a program that multiplies two numbers. We could do so using this code:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>def multiply_numbers(number1, number2):\n\tanswer = number1 * number2\n\tprint(number1, &quot; x &quot;, number2, &quot; = &quot;, answer)\n\nmultiply_numbers(5, 10)\nmultiply_numbers(15, 2)<\/pre><\/div>\n\n\n\n<p>Our Python program returns:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>5 x 10 = 50\n15 x 2 = 30<\/pre><\/div>\n\n\n\n<p>First, we define a function called <em>multiply_numbers<\/em>. The parameter names in the function that our code accepts are: number1 and number2. We define these between brackets which is where the parameter list is defined.<\/p>\n\n\n\n<p>Next, we declare a <a href=\"https:\/\/careerkarma.com\/blog\/python-variables\/\">Python variable<\/a> called \u201canswer\u201d which multiplies the values of number1 and number2. Then, we print a statement to the console with the full mathematical sum written out, followed by the answer to the math problem.<\/p>\n\n\n\n<p>We have specified required arguments. This is because we have set no default value for each argument. We must specify a number of arguments equal to those in the parameter list otherwise the Python interpreter will return an error.<\/p>\n\n\n\n<p>Toward the end of our program, we call our <em>multiply_numbers<\/em> function twice.<\/p>\n\n\n\n<p>First, we specify the arguments 5 and 10. Our program multiplies these values together to calculate 50. Then, our program prints \u201c5 x 10 = 50\u201d to the console. Next, we specify the arguments 15 and 2, which our program multiplies. Then, our program prints \u201c15 x 2 = 30\u201d to the console.<\/p>\n\n\n\n<p>By default, the order of the arguments you pass in a function is the order in which they are processed by your program. When we run \u201cmultiply_numbers(5, 10)\u201d, the value of \u201cnumber1\u201d becomes 5. The value of \u201cnumber2\u201d becomes 10. We\u2019ll talk about how to override this in the \u201ckeyword arguments\u201d section.<\/p>\n\n\n\n<p>For more information on arguments, check out our <a href=\"https:\/\/careerkarma.com\/blog\/python-optional-arguments\/\">Python optional arguments tutorial<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">A Note: Parameters vs. Arguments<\/h2>\n\n\n\n<p>The terms <em>parameter<\/em> and <em>argument<\/em> refer to the same thing: passing information to a function. But, there is a subtle difference between the two.<\/p>\n\n\n\n<p>A parameter is the variable inside the parenthesis in a function. An argument is the value that is passed to a function when it is called. So, in our last example, \u201cnumber1\u201d and \u201cnumber2\u201d are parameters, and 5 and 10 are arguments.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Function Keyword Arguments<\/h2>\n\n\n\n<p>As we discussed, the order in which you pass arguments is the order in which your program will process them. So, the first parameter will be assigned to the first argument, and so on. However, there is a way to override this rule.<\/p>\n\n\n\n<p>You can use keyword arguments in a function call, which allows you to assign the value of an argument based on its parameter name. Using keyword arguments allows you to specify the value of keywords in any order you want.<\/p>\n\n\n\n<p>Keyword arguments work because you\u2019ll use keywords to match values to parameters, instead of relying on the order of the arguments to pass values.<\/p>\n\n\n\n<p>Suppose we are creating a program that prints out the name and email address of someone who has signed up to a mailing list. We could write this program using the following code:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>def print_info(name, email):\n\tprint(&quot;Name: &quot;, name)\n\tprint(&quot;Email: &quot;, email)\n\nprint_info(email=&quot;alex.hammond@gmail.com&quot;, name=&quot;Alex Hammond&quot;)<\/pre><\/div>\n\n\n\n<p>Our code returns: <\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>Name: Alex Hammond\nEmail: alex.hammond@gmail.com<\/pre><\/div>\n\n\n\n<p>We declare a function that accepts two parameters: name and email. We print &#8220;<em>Name:&#8221;<\/em> to the console, followed by the value in the <em>name<\/em> parameter. Then, we print &#8220;_Email:&#8221;_to the console, followed by by the value in the <em>email<\/em> parameter. We use <a href=\"https:\/\/careerkarma.com\/blog\/python-print-without-new-line\/\">Python print() statements<\/a> to print these values to the console.<\/p>\n\n\n\n<p>Next, we call our function and specify two arguments. The <em>email<\/em> argument is made equal to <a href=\"mailto:alex.hammond@gmail.com\" target=\"_blank\" rel=\"noopener\" rel=\"nofollow\"><em>alex.hammond@gmail.com<\/em><\/a>, and the <em>name<\/em> argument is made equal to <em>Alex Hammond<\/em>.<\/p>\n\n\n\n<p>In our code, we separated the name of the argument and its value using an equals sign (=). This meant that we no longer had to specify our arguments in the order our parameters appear (name, email). We could use any order we wanted.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Default Argument Values<\/h2>\n\n\n\n<p>In addition, you can specify a default argument value for a parameter in a function.<\/p>\n\n\n\n<p>Suppose we want the value of <em>email<\/em> to be <a href=\"mailto:default@gmail.com\" target=\"_blank\" rel=\"noopener\" rel=\"nofollow\"><em>default@gmail.com<\/em><\/a> by default. We could make this happen using the following code:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>def print_info(name, email=&quot;default@gmail.com&quot;):\n\tprint(&quot;Name: &quot;, name)\n\tprint(&quot;Email: &quot;, email)\n\nprint_info(&quot;Alex Hammond&quot;)<\/pre><\/div>\n\n\n\n<p>Our Python code returns: <\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>Name: Alex Hammond\nEmail: default@gmail.com<\/pre><\/div>\n\n\n\n<p>We set the default value of the <em>email<\/em> parameter to be <a href=\"mailto:default@gmail.com\" target=\"_blank\" rel=\"noopener\" rel=\"nofollow\"><em>default@gmail.com<\/em><\/a>. When we run our code and call the <em>print_info()<\/em> function, we don\u2019t need to specify a value for the <em>email<\/em> argument. In this example, when we execute <em>print_info()<\/em>, we only specify one argument: the name of the user.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Returning Values to the Main Program<\/h2>\n\n\n\n<p>So far, we\u2019ve discussed how we can pass values <em>into<\/em> a function. But a function can also be used to pass values <em>to<\/em> the rest of a program.<\/p>\n\n\n\n<p>The <em>return<\/em> statement exits a function and allows you to pass a value back to the main program. If you use the <em>return<\/em> statement without an argument, the function will return the value <em>None<\/em>.<\/p>\n\n\n\n<p>Suppose we want to create a program that multiplies two numbers. Then, when those two numbers have been multiplied, we want to return them to our main program. We could do so using this code:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>def multiply_numbers(number1, number2):\n\tanswer = number1 * number2\n\treturn answer\n\nans = multiply_numbers(5, 6)\nprint(ans)<\/pre><\/div>\n\n\n\n<p>Our code returns: <\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>30<\/pre><\/div>\n\n\n\n<p>First, we define a function called <em>multiply_numbers<\/em>. This function accepts two parameters: number1 and number2. When this function is called, the values of \u201cnumber1\u201d and \u201cnumber2\u201d are multiplied. Then, we use the <em>return<\/em> statement to pass the multiplied number to the main program.<\/p>\n\n\n\n<p>We call the <em>multiply_numbers()<\/em> function and specify two arguments: 5 and 6. Notice that we also assign the function\u2019s result to the variable \u201cans\u201d. When this line of code is run, our function is called, and its result is assigned to \u201cans\u201d. Then, our code prints out the value of \u201cans\u201d, which is 30 in this case.<\/p>\n\n\n\n<p>The <a href=\"https:\/\/careerkarma.com\/blog\/python-return\/\">Python <\/a><em><a href=\"https:\/\/careerkarma.com\/blog\/python-return\/\">return<\/a><\/em><a href=\"https:\/\/careerkarma.com\/blog\/python-return\/\"> statement<\/a> stops a function from executing, even if it is not returning a value. Here\u2019s an example of this behavior in action:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>def run_ten():\n\tfor i in range(0, 10):\n\t\tif i == 4:\n\t\t\treturn\n\tprint(&quot;Finished&quot;)\n\nrun_ten()<\/pre><\/div>\n\n\n\n<p>Our code prints nothing to the console. Although there is a \u201cprint(\u201cFinished\u201d)\u201d statement in our code, it does not run.<\/p>\n\n\n\n<p>This is because, when our for loop executes four times (when <em>i<\/em> is equal to 4), the return statement is executed. This causes our function to halt execution and stops our loop from running.<\/p>\n\n\n\n<p>After our function stops running, the code in our main program will continue to run.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Python functions are blocks of code that execute a certain action. Functions can be called as many times as you want in a program. This means that you can run the same block of code multiple times without having to repeat your code.<\/p>\n\n\n\n<p>Functions allow you to reduce repetition in your code, thereby making your programs easier for both you and other coders to read.<\/p>\n\n\n\n<p>For a challenge, write a function that prints every number between 1 and 10 to the console (inclusive of 10). This function should contain a for loop. When the function is done, you should print &#8220;Done!&#8221; to the console. Call your function once at the end of your program.<\/p>\n\n\n\n<p>The output should be:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>1\n2\n3\n4\n5\n6\n7\n8\n9\n10<\/pre><\/div>\n\n\n\n<p>This tutorial discussed the basics of functions in Python, how to write and call a function, and how to work with arguments and parameters. Now you\u2019re ready to start writing functions in Python like an expert!<\/p>\n\n\n\n<p>For advice on the top Python courses, books, and learning resources, check out our comprehensive <a href=\"https:\/\/careerkarma.com\/blog\/how-to-learn-python\/\">How to Learn Python guide<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"A Python function is a group of code. To run the code in a function, you must call the function. A function can be called from anywhere after the function is defined. Functions can return a value using a return statement. Functions are a common feature among all programming languages. They allow developers to write&hellip;","protected":false},"author":240,"featured_media":15888,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[16578],"tags":[],"class_list":{"0":"post-15887","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-python"},"acf":{"post_sub_title":"","sprint_id":"","query_class":"Python","school_sft":"","parent_sft":"","school_privacy_policy":"","has_review":null,"is_sponser_post":"","is_guest_post":""},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.0 (Yoast SEO v27.0) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>How to Write Python Functions | Career Karma<\/title>\n<meta name=\"description\" content=\"A function is a block of code that is used to perform a specific action when it is called. On Career Karma, learn how to write your own Python functions.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/careerkarma.com\/blog\/python-functions\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Write Python Functions\" \/>\n<meta property=\"og:description\" content=\"A function is a block of code that is used to perform a specific action when it is called. On Career Karma, learn how to write your own Python functions.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/careerkarma.com\/blog\/python-functions\/\" \/>\n<meta property=\"og:site_name\" content=\"Career Karma\" \/>\n<meta property=\"article:publisher\" content=\"http:\/\/facebook.com\/careerkarmaapp\" \/>\n<meta property=\"article:published_time\" content=\"2020-12-11T06:53:24+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-01T12:05:49+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/turned-on-black-flat-screen-computer-monitor-and-keyboard-2585916.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1020\" \/>\n\t<meta property=\"og:image:height\" content=\"680\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"James Gallagher\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@career_karma\" \/>\n<meta name=\"twitter:site\" content=\"@career_karma\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"James Gallagher\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-functions\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-functions\/\"},\"author\":{\"name\":\"James Gallagher\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\"},\"headline\":\"How to Write Python Functions\",\"datePublished\":\"2020-12-11T06:53:24+00:00\",\"dateModified\":\"2023-12-01T12:05:49+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-functions\/\"},\"wordCount\":1824,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-functions\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/turned-on-black-flat-screen-computer-monitor-and-keyboard-2585916.jpg\",\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/careerkarma.com\/blog\/python-functions\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-functions\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/python-functions\/\",\"name\":\"How to Write Python Functions | Career Karma\",\"isPartOf\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-functions\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-functions\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/turned-on-black-flat-screen-computer-monitor-and-keyboard-2585916.jpg\",\"datePublished\":\"2020-12-11T06:53:24+00:00\",\"dateModified\":\"2023-12-01T12:05:49+00:00\",\"author\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\"},\"description\":\"A function is a block of code that is used to perform a specific action when it is called. On Career Karma, learn how to write your own Python functions.\",\"breadcrumb\":{\"@id\":\"https:\/\/careerkarma.com\/blog\/python-functions\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/careerkarma.com\/blog\/python-functions\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-functions\/#primaryimage\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/turned-on-black-flat-screen-computer-monitor-and-keyboard-2585916.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/turned-on-black-flat-screen-computer-monitor-and-keyboard-2585916.jpg\",\"width\":1020,\"height\":680},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/careerkarma.com\/blog\/python-functions\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\/\/careerkarma.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python\",\"item\":\"https:\/\/careerkarma.com\/blog\/python\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"How to Write Python Functions\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#website\",\"url\":\"https:\/\/careerkarma.com\/blog\/\",\"name\":\"Career Karma\",\"description\":\"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/careerkarma.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94\",\"name\":\"James Gallagher\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg\",\"contentUrl\":\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg\",\"caption\":\"James Gallagher\"},\"description\":\"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.\",\"url\":\"https:\/\/careerkarma.com\/blog\/author\/jamesgallagher\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"How to Write Python Functions | Career Karma","description":"A function is a block of code that is used to perform a specific action when it is called. On Career Karma, learn how to write your own Python functions.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/careerkarma.com\/blog\/python-functions\/","og_locale":"en_US","og_type":"article","og_title":"How to Write Python Functions","og_description":"A function is a block of code that is used to perform a specific action when it is called. On Career Karma, learn how to write your own Python functions.","og_url":"https:\/\/careerkarma.com\/blog\/python-functions\/","og_site_name":"Career Karma","article_publisher":"http:\/\/facebook.com\/careerkarmaapp","article_published_time":"2020-12-11T06:53:24+00:00","article_modified_time":"2023-12-01T12:05:49+00:00","og_image":[{"width":1020,"height":680,"url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/turned-on-black-flat-screen-computer-monitor-and-keyboard-2585916.jpg","type":"image\/jpeg"}],"author":"James Gallagher","twitter_card":"summary_large_image","twitter_creator":"@career_karma","twitter_site":"@career_karma","twitter_misc":{"Written by":"James Gallagher","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/careerkarma.com\/blog\/python-functions\/#article","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/python-functions\/"},"author":{"name":"James Gallagher","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"headline":"How to Write Python Functions","datePublished":"2020-12-11T06:53:24+00:00","dateModified":"2023-12-01T12:05:49+00:00","mainEntityOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/python-functions\/"},"wordCount":1824,"commentCount":0,"image":{"@id":"https:\/\/careerkarma.com\/blog\/python-functions\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/turned-on-black-flat-screen-computer-monitor-and-keyboard-2585916.jpg","articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/careerkarma.com\/blog\/python-functions\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/careerkarma.com\/blog\/python-functions\/","url":"https:\/\/careerkarma.com\/blog\/python-functions\/","name":"How to Write Python Functions | Career Karma","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/python-functions\/#primaryimage"},"image":{"@id":"https:\/\/careerkarma.com\/blog\/python-functions\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/turned-on-black-flat-screen-computer-monitor-and-keyboard-2585916.jpg","datePublished":"2020-12-11T06:53:24+00:00","dateModified":"2023-12-01T12:05:49+00:00","author":{"@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"description":"A function is a block of code that is used to perform a specific action when it is called. On Career Karma, learn how to write your own Python functions.","breadcrumb":{"@id":"https:\/\/careerkarma.com\/blog\/python-functions\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/careerkarma.com\/blog\/python-functions\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/python-functions\/#primaryimage","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/turned-on-black-flat-screen-computer-monitor-and-keyboard-2585916.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/05\/turned-on-black-flat-screen-computer-monitor-and-keyboard-2585916.jpg","width":1020,"height":680},{"@type":"BreadcrumbList","@id":"https:\/\/careerkarma.com\/blog\/python-functions\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/careerkarma.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Python","item":"https:\/\/careerkarma.com\/blog\/python\/"},{"@type":"ListItem","position":3,"name":"How to Write Python Functions"}]},{"@type":"WebSite","@id":"https:\/\/careerkarma.com\/blog\/#website","url":"https:\/\/careerkarma.com\/blog\/","name":"Career Karma","description":"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/careerkarma.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94","name":"James Gallagher","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","caption":"James Gallagher"},"description":"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.","url":"https:\/\/careerkarma.com\/blog\/author\/jamesgallagher\/"}]}},"_links":{"self":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/15887","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/users\/240"}],"replies":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/comments?post=15887"}],"version-history":[{"count":0,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/15887\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media\/15888"}],"wp:attachment":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media?parent=15887"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/categories?post=15887"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/tags?post=15887"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}