{"id":7272,"date":"2024-03-19T06:47:28","date_gmt":"2024-03-19T06:47:28","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=7272"},"modified":"2024-03-19T06:47:28","modified_gmt":"2024-03-19T06:47:28","slug":"python-functions","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/python-functions\/","title":{"rendered":"Python Functions"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#what-are-python-functions\">What are Python Functions?<\/a><\/li><li ><a href=\"#what-is-the-syntax-of-python-function-declaration\">What is the Syntax of Python Function Declaration?<\/a><\/li><li ><a href=\"#what-are-the-types-of-functions-in-python\">What are the types of Functions in Python?<\/a><\/li><li ><a href=\"#creating-a-function-in-python\">Creating a Function in Python<\/a><ul><li ><a href=\"#calling-a-python-function\">Calling a Python Function<\/a><\/li><li ><a href=\"#python-function-with-parameters\">Python Function with Parameters<\/a><\/li><\/ul><\/li><li ><a href=\"#defining-and-calling-a-function-with-parameters\">Defining and Calling a function with Parameters<\/a><\/li><li ><a href=\"#what-is-python-function-arguments\">What is Python Function Arguments<\/a><\/li><li ><a href=\"#what-are-the-types-of-python-function-arguments\">What are the types of Python Function Arguments?<\/a><\/li><li ><a href=\"#what-is-a-default-argument\">What is a Default Argument?<\/a><\/li><li ><a href=\"#what-is-keyword-argument\">What is Keyword Argument?<\/a><\/li><li ><a href=\"#what-are-positional-arguments\">What are Positional Arguments?<\/a><\/li><li ><a href=\"#what-is-docstring\">What is Docstring?<\/a><\/li><li ><a href=\"#what-are-recursive-functions-in-python\">What are Recursive Functions in Python?<\/a><\/li><li ><a href=\"#what-is-a-return-statement-in-python-function\">What is a Return Statement in Python Function?<\/a><\/li><li ><a href=\"#what-is-pass-by-reference-and-pass-by-value\">What is Pass by Reference and Pass by Value?<\/a><\/li><li ><a href=\"#conclusion\">Conclusion<\/a><\/li><li ><a href=\"#python-functions-fa-qs\">Python Functions- FAQs<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">A function is considered a block that will run a particular task. Let&#8217;s look into the article to learn more about Python Functions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-are-python-functions\">What are Python Functions?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Python Functions are the blocks of statements that will return the specific task. The main goal is to put some repeatedly done tasks together and thus, create a function. This will allow us to write the code repeatedly for several inputs. Hence, we do this function calls to reuse code in it repeatedly.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-the-syntax-of-python-function-declaration\">What is the Syntax of Python Function Declaration?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The syntax of the<a href=\"https:\/\/www.skillvertex.com\/blog\/python-comments\/\" data-type=\"post\" data-id=\"7009\"> Python<\/a> Function declaration is given below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def functionname( parameters ):\n   \"function_docstring\"\n   function_suite\n   return &#91;expression]<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-are-the-types-of-functions-in-python\">What are the types of Functions in Python?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">1. Built-in Library Function- This is known as the standard function in Python which is available to utilize.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2. User-defined Function- Through this, it is possible to make our functions depending on the requirements.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"creating-a-function-in-python\">Creating a Function in Python<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Python function can be defined using the<a href=\"https:\/\/www.skillvertex.com\/blog\/which-of-the-following-is-not-a-keyword-in-python-language\/\" data-type=\"post\" data-id=\"1791\"> def keyword<\/a>.  According to the requirements, you can add any types of functionalities and properties to it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Look at the example given below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># A simple Python function \n \ndef fun():\n  print(\"Welcome to SV\")<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"calling-a-python-function\">Calling a Python Function<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Thus, after creating the function. It can be named with the name of the function and is followed by the parenthesis that has parameters of the specific function.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Define a function\ndef greet(name):\n    return f\"Hello, {name}!\"\n\n# Call the function\nresult = greet(\"John\")\n\n# Print the result\nprint(result)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Hello, John!\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"python-function-with-parameters\">Python Function with Parameters<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In Python, the return type of the function and data type of arguments is possible. This function is possible in Python and in several <a href=\"https:\/\/www.skillvertex.com\/blog\/python-history-and-versions\/\" data-type=\"post\" data-id=\"6864\">versions of Python i<\/a>ncluding Python 3.5 and above.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"defining-and-calling-a-function-with-parameters\">Defining and Calling a function with Parameters<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The syntax is provided below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> def function_name(parameter: data_type) -&gt; return_type:\n    \"\"\"Docstring\"\"\"\n    # body of the function\n    return expression<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example <\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Define a function with parameters\ndef add_numbers(a, b):\n    result = a + b\n    return result\n\n# Call the function with arguments\nsum_result = add_numbers(5, 7)\n\n# Print the result\nprint(\"Sum:\", sum_result)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Sum: 12\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-python-function-arguments\">What is Python Function Arguments<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/www.skillvertex.com\/blog\/default-arguments-in-python\/\" data-type=\"post\" data-id=\"7267\">Arguments <\/a>are referred to as the value that will be passed inside the parenthesis of the function. The function consists of the number of arguments that are separated by a comma.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Look into the example given below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Function to check if a number is even or odd\ndef even_or_odd(number):\n    if number % 2 == 0:\n        return \"Even\"\n    else:\n        return \"Odd\"\n\n# Function with default argument (exponent) using even or odd numbers\ndef power(base, exponent=2):\n    return base ** exponent\n\n# Calling functions with even and odd numbers\nnumber_to_check = 7\nresult_even_odd = even_or_odd(number_to_check)\npower_result = power(number_to_check, exponent=3)  # Using odd number with a different exponent\n\n# Printing the results\nprint(f\"{number_to_check} is {result_even_odd}\")\nprint(f\"Power of {number_to_check} to the 3rd exponent:\", power_result)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>7 is Odd\nPower of 7 to the 3rd exponent: 343\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-are-the-types-of-python-function-arguments\">What are the types of Python Function Arguments?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">It is known that Python will allow several types of arguments that will be passed at the time of a function call. In Python, there are 4 types of function calls.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">a. Default argument<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">b.Keyword argument<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">c. Positional argument<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">d. Arbitrary argument<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-a-default-argument\">What is a Default Argument?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The default argument is referred to as a parameter that will predict a default value when the value is not assigned in the function call for the argument.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Function with default argument\ndef greet(name, greeting=\"Hello\"):\n    return f\"{greeting}, {name}!\"\n\n# Calling the function with and without providing the default argument\nresult1 = greet(\"Alice\")          # Uses the default greeting\nresult2 = greet(\"Bob\", \"Hi\")      # Provides a custom greeting\n\n# Printing the results\nprint(result1)\nprint(result2)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Hello, Alice!\nHi, Bob!\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-keyword-argument\">What is Keyword Argument?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In Python, the keyword Argument will allow the caller to mention the argument name along with the values, in which it is not required to remember the order of parameters.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Python program to demonstrate Keyword Arguments\ndef student(firstname, lastname):\n    print(firstname, lastname)\n \n \n# Keyword arguments\nstudent(firstname='Skill', lastname='Vertex')\nstudent(lastname='Vertex', firstname='Skill')<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-are-positional-arguments\">What are Positional Arguments?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The Position Arguments is used during the function call as the first argument will be given a name, and the second argument will be provided to age.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Function with positional arguments\ndef add_numbers(a, b):\n    result = a + b\n    return result\n\n# Calling the function with positional arguments\nsum_result = add_numbers(5, 7)\n\n# Printing the result\nprint(\"Sum:\", sum_result)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Sum: 12\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-docstring\">What is Docstring?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The first string after the function is referred to as the document string. The main goal of this Docstring is to define the functionality of the function. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The syntax of Docstring is given below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Syntax: print(function_name.__doc__)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def greet(name):\n    \"\"\"\n    This function takes a name as a parameter and returns a greeting message.\n\n    Parameters:\n    - name (str): The name of the person to greet.\n\n    Returns:\n    str: A greeting message.\n    \"\"\"\n    return f\"Hello, {name}!\"\n\n# Accessing the docstring\nprint(greet.__doc__)\n\n# Calling the function\nresult = greet(\"Alice\")\n\n# Printing the result\nprint(result)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>This function takes a name as a parameter and returns a greeting message.\n\nParameters:\n- name (str): The name of the person to greet.\n\nReturns:\nstr: A greeting message.\n\nHello, Alice!\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-are-recursive-functions-in-python\">What are Recursive Functions in Python?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Recursive in Python will be used when a function calls by itself. Hence, it states that it is required to create the recursive function to solve the Mathematical and Recursive Problems.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def factorial(n):\n    \"\"\"\n    Recursive function to calculate the factorial of a number.\n\n    Parameters:\n    - n (int): The number for which to calculate the factorial.\n\n    Returns:\n    int: The factorial of the input number.\n    \"\"\"\n    if n == 0 or n == 1:\n        return 1\n    else:\n        return n * factorial(n - 1)\n\n# Calling the recursive function\nresult = factorial(5)\n\n# Printing the result\nprint(\"Factorial:\", result)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Factorial: 120\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Note: Use the Recursive function with a warning, as it will turn into a\/ non-terminating loop.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-a-return-statement-in-python-function\">What is a Return Statement in Python Function?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The return statement functions to exit from the function and return back to the function caller . Thus it will get a specified value<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The syntax is provided below<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>return &#91;expression_list]\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def add_numbers(a, b):\n    \"\"\"\n    This function takes two numbers as parameters and returns their sum.\n\n    Parameters:\n    - a (int): The first number.\n    - b (int): The second number.\n\n    Returns:\n    int: The sum of the two numbers.\n    \"\"\"\n    result = a + b\n    return result\n\n# Calling the function and storing the result\nsum_result = add_numbers(3, 7)\n\n# Printing the result\nprint(\"Sum:\", sum_result)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Sum: 10\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-pass-by-reference-and-pass-by-value\">What is Pass by Reference and Pass by Value?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In Python, every variable name has a reference. In which, when the variable is passed, then the new reference of the object is made. Parameter passing in Python is considered similar to that of reference passing in Java.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Pass by assignment example\n\n# Function modifying a mutable object (list)\ndef modify_list(my_list):\n    my_list.append(4)\n    my_list&#91;0] = 99\n\n# Function modifying an immutable object (integer)\ndef modify_integer(my_integer):\n    my_integer = 10\n\n# Original list and integer\noriginal_list = &#91;1, 2, 3]\noriginal_integer = 5\n\n# Call functions with the original objects\nmodify_list(original_list)\nmodify_integer(original_integer)\n\n# Print the original objects after function calls\nprint(\"Modified List:\", original_list)      # &#91;1, 2, 3, 4, 99]\nprint(\"Modified Integer:\", original_integer) # 5\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Modified List: &#91;1, 2, 3, 4, 99]\nModified Integer: 5\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In summary, Python functions are versatile tools that help organize code into reusable and manageable blocks. They allow developers to define specific tasks or operations that can be executed with different inputs. With parameters, default values, and return statements, functions provide flexibility in handling various scenarios.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"> Additionally, docstrings serve as valuable documentation to understand a function&#8217;s purpose and usage.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"python-functions-fa-qs\">Python Functions- FAQs<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1708498786899\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1.What is a Python function?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. It is referred to as a block of statements that does a particular task.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1708498794805\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. How to define a function?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. The relation between a set of inputs having one output each will define the function.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1708498803032\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. What is a def in Python example?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans.  def is referred to as a block keyword that is followed by the function name and the block name.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>A function is considered a block that will run a particular task. Let&#8217;s look into the article to learn more about Python Functions. What are Python Functions? Python Functions are the blocks of statements that will return the specific task. The main goal is to put some repeatedly done tasks together and thus, create a &#8230; <a title=\"Python Functions\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/python-functions\/\" aria-label=\"More on Python Functions\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":7275,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[864],"tags":[933,57,932],"class_list":["post-7272","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorial","tag-keywords-in-python","tag-python","tag-python-functions","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/7272","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=7272"}],"version-history":[{"count":11,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/7272\/revisions"}],"predecessor-version":[{"id":8274,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/7272\/revisions\/8274"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/7275"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=7272"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=7272"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=7272"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}