{"id":794,"date":"2021-05-04T21:39:28","date_gmt":"2021-05-04T16:09:28","guid":{"rendered":"https:\/\/cbsepython.in\/?p=794"},"modified":"2026-01-05T13:40:44","modified_gmt":"2026-01-05T08:10:44","slug":"functions-in-python","status":"publish","type":"post","link":"https:\/\/cbsepython.in\/functions-in-python\/","title":{"rendered":"Working with Functions in Python Class 12 Notes"},"content":{"rendered":"<h1><span style=\"color: #000000;\">Working with Functions in Python Class 12 Notes<\/span><\/h1>\n<p><strong><span style=\"color: #000000;\">Working with Functions in Python Class 12 Notes:\u00a0 <\/span><\/strong><span style=\"color: #000000;\">Here you will learn about the functions in Python, Types of Functions in Python, How to create a function in Python, how function works in Python. These &#8220;Working with Function Notes&#8221; will surely helpful for the Computer Science, Informatics Practices\u00a0 students of class 12 CBSE.<\/span><\/p>\n<h2><span style=\"color: #000000;\">Functions in Python for Class 12<\/span><\/h2>\n<p><span style=\"color: #000000;\">In this section we will discuss about the function in Python. It will help the students having Computer Science \/ Informatics Practices in Class 11 and 12.<\/span><\/p>\n<p>&nbsp;<\/p>\n<h2><span style=\"color: #000000;\">What is Function?<\/span><\/h2>\n<p><span style=\"color: #000000;\">In Python, a function is a set of related statements that perform a specific task. Functions help to divide our program into smaller and more modular parts. As our programs become larger and larger, these features make them more organized and clear. It also avoids duplication and allows code to be reused.<\/span><\/p>\n<p>&nbsp;<\/p>\n<blockquote><p><span style=\"color: #000000;\">Functions are sub programs of a program. It means a function is a part of a program that carries out some well defined task.<\/span><\/p><\/blockquote>\n<h2 style=\"text-align: center;\"><span style=\"text-decoration: underline;\"><span style=\"color: #000000; text-decoration: underline;\">Functions in Python Class 12<\/span><\/span><\/h2>\n<h2><span style=\"color: #000000;\"><strong>Types of Functions in Python:<\/strong><\/span><\/h2>\n<h3><span style=\"color: #000000;\"><strong>1. Built in Function <\/strong><\/span><\/h3>\n<p><span style=\"color: #000000;\">These are the pre defined functions having some specific work to perform. <\/span><\/p>\n<p><span style=\"color: #000000;\">For Example: <\/span><\/p>\n<p><span style=\"color: #000000;\">print ( ): print the statement or result itself (We don\u2019t know how, we just use)<\/span><\/p>\n<p><span style=\"color: #000000;\">input ( ): Takes input from the keyboard (We don\u2019t know how, we just use)<\/span><\/p>\n<p><span style=\"color: #000000;\">len(), min(), max(), type() etc. are also some built in functions in Python<\/span><\/p>\n<p>&nbsp;<\/p>\n<h3><span style=\"color: #000000;\"><strong>2. Functions defined in Modules:<\/strong><\/span><\/h3>\n<p><span style=\"color: #000000;\">Library: A\u00a0<em><strong>library<\/strong><\/em>\u00a0is a bundle of code made to help you accomplish routine tasks more quickly. We just have to import the module in our program.<\/span><\/p>\n<p><span style=\"color: #000000;\">Example: math(), Random(), Pandas, numpy etc.<\/span><\/p>\n<p>&nbsp;<\/p>\n<h3><span style=\"color: #000000;\"><strong>3. User defined Functions<\/strong><\/span><\/h3>\n<p><span style=\"color: #000000;\">These are the functions which are defined\/written by the user to perform some specific task.<\/span><\/p>\n<h3><\/h3>\n<h3><span style=\"color: #000000;\">Syntax of user defined Function<\/span><\/h3>\n<pre><code>def function_name(parameters):\r\n\t\"\"\"docstring\"\"\"\r\n\tstatement(s)<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000;\">Keyword def, used to mark the beginning of the function header.<\/span><\/p>\n<p><span style=\"color: #000000;\">Function name, used to uniquely identify the function. Function names follow the same rules for writing identifiers in Python.<\/span><\/p>\n<p><span style=\"color: #000000;\">We use it to pass values to the parameters (arguments) of the function.<\/span><\/p>\n<p><span style=\"color: #000000;\">The\u00a0 colon (:) is used to mark the end of the function header;<\/span><\/p>\n<p><span style=\"color: #000000;\">The optional return statement is used to return a value from a function.<\/span><\/p>\n<p>&nbsp;<\/p>\n<h2>Creating a function in Python<\/h2>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">def my_function():\r\n    print(\"Hi this is python function\")<\/pre>\n<p>&nbsp;<\/p>\n<h2>Calling a function in Python<\/h2>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">def my_function():\r\n    print(\"Hi This is function\")\r\nmy_function()\r\n<\/pre>\n<p>Output:<\/p>\n<pre>Hi This is function<\/pre>\n<h3><\/h3>\n<blockquote><p><span style=\"color: #000000;\"><strong>&gt; Argument:<\/strong> a value that is supplied to the function by user<\/span><\/p>\n<p><span style=\"color: #000000;\"><strong>&gt; Return:<\/strong> A value that a function returns to its calling place. (will not display the result until you print the result)<\/span><\/p>\n<p>&nbsp;<\/p><\/blockquote>\n<h2><strong><span style=\"color: #000000;\">There are many ways to create a user defined function<\/span><\/strong><\/h2>\n<p><span style=\"color: #000000;\">1. No Argument No return<\/span><\/p>\n<p><span style=\"color: #000000;\">2. with Argument No return<\/span><\/p>\n<p><span style=\"color: #000000;\">3. No Argument with return<\/span><\/p>\n<p><span style=\"color: #000000;\">4. with Argument with return<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000;\">Now we will discuss one by one with the help of a python program to add two numbers.<\/span><\/p>\n<h3>i) <span style=\"color: #000000;\">No Argument No return<\/span><\/h3>\n<pre><span style=\"color: #000000;\">def add():<\/span>\r\n<span style=\"color: #000000;\"> \u00a0\u00a0\u00a0\u00a0a= int (input(\u201cEnter First Number\u201d))<\/span>\r\n<span style=\"color: #000000;\"> \u00a0\u00a0\u00a0\u00a0b= int (input(\u201cEnter Second Number\u201d))<\/span>\r\n<span style=\"color: #000000;\"> \u00a0\u00a0\u00a0\u00a0c=a+b<\/span>\r\n<span style=\"color: #000000;\"> \u00a0\u00a0\u00a0\u00a0print (\u201cThe Sum of inputted Numbers is:\u201d, c)<\/span>\r\n<span style=\"color: #000000;\">add()<\/span>\r\n<span style=\"color: #000000;\">print(\u201c Executed\u201d)<\/span><\/pre>\n<p>&nbsp;<\/p>\n<h3>ii) <span style=\"color: #000000;\">with Argument No return<\/span><\/h3>\n<pre><span style=\"color: #000000;\">def add(a,b):<\/span>\r\n<span style=\"color: #000000;\"> \u00a0\u00a0\u00a0\u00a0c=a+b<\/span>\r\n<span style=\"color: #000000;\"> \u00a0\u00a0\u00a0\u00a0print(\u201cThe Sum of inputted Numbers is:\u201d, c)<\/span>\r\n<span style=\"color: #000000;\">num1=int (input(\u201cEnter First Number\u201d)) <\/span>\r\n<span style=\"color: #000000;\">num2= int (input(\u201cEnter Second Number\u201d))<\/span>\r\n<span style=\"color: #000000;\">add(num1,num2) <\/span><\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000;\">Remember: The variable in main program are differ from the variable in function definition i.e. a and b should be x and y. In this scenario the variable passed (x,y)will be called argument, and when it replace with the variable defined in the function will be called as parameter.<\/span><\/p>\n<h3><\/h3>\n<h3>iii) <span style=\"color: #000000;\">No Argument with return<\/span><\/h3>\n<pre><span style=\"color: #000000;\">def add():<\/span>\r\n<span style=\"color: #000000;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0a=int (input(\u201cEnter First Number\u201d))<\/span>\r\n<span style=\"color: #000000;\"> \u00a0 \u00a0 \u00a0 b= int (input(\u201cEnter Second Number\u201d))<\/span>\r\n<span style=\"color: #000000;\">   \u00a0 \u00a0 c=a+b<\/span>\r\n<span style=\"color: #000000;\">  \u00a0 \u00a0 \u00a0return c<\/span>\r\n<span style=\"color: #000000;\">x= add()<\/span>\r\n<span style=\"color: #000000;\">print (\u201cThe Sum of inputted Numbers is:\u201d, x)<\/span>\r\n\r\n<\/pre>\n<p><strong>Note: { As return does not show the result we have to store the returned value on another variable x}<\/strong><\/p>\n<h3><\/h3>\n<h3>iv) <span style=\"color: #000000;\">with Argument with return<\/span><\/h3>\n<pre><span style=\"color: #000000;\">def\u00a0 add (a,b):<\/span>\r\n<span style=\"color: #000000;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 c=a+b<\/span>\r\n<span style=\"color: #000000;\"> \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return c<\/span>\r\n<span style=\"color: #000000;\">a=int (input(\u201cEnter First Number\u201d))<\/span>\r\n<span style=\"color: #000000;\">b= int (input(\u201cEnter Second Number\u201d))<\/span>\r\n<span style=\"color: #000000;\">x= add(a,b)<\/span>\r\n<span style=\"color: #000000;\">print (\u201cThe Sum of inputted Numbers is:\u201d, x)<\/span><\/pre>\n<h3><\/h3>\n<h1><span style=\"color: #000000;\">Difference between Arguments and parameters<\/span><\/h1>\n<p><span style=\"color: #000000;\">These two terms are very interchangeable, so it is not so important to know the difference. The terms they refer to are almost identical. However, in order to sound more professional, correct terminology is important. Variables that are in brackets when defining the function. When a method is called, the arguments are the data passed to the method&#8217;s parameters.<\/span><\/p>\n<p><span style=\"color: #000000;\"><strong>Function arguments :<\/strong> Arguments are used to pass information from the rest of the program to the function. This information return a result. There is no limit to the number of arguments that can be written. Depending on the type of function you&#8217;re performing, there might even be no argument.<\/span><\/p>\n<p><span style=\"color: #000000;\">Use commas to separate the arguments in a function. Take into account the number of arguments you put in a function call. The number of arguments must be exactly the same as the number of parameters.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000;\"><strong>In the example below<\/strong>, the variable name is the input parameter, where as the value, \u201cArnav\u201d, passed in the function call is the argument.<\/span><\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">def welcome(name):\r\n    print(\"Hello! \" + name + \" Good Morning!!\")\r\nwelcome(\"Arnav\")\r\n<\/pre>\n<p>Output:<\/p>\n<pre>Hello!\u00a0 Arnav Good Morning!!<\/pre>\n<p>&nbsp;<\/p>\n<h2><span style=\"color: #000000;\">Returning a Function<\/span><\/h2>\n<p><span style=\"color: #000000;\">If you wish to return some values from the function to the rest of the program, you can use the return statement. As the name suggests, it returns a value without printing it. Executing this statement will cause the Python function to end immediately and store the value being returned into a variable.<\/span><\/p>\n<p>&nbsp;<\/p>\n<h2><span style=\"color: #000000;\">How function\u00a0 works<\/span><\/h2>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"alignnone wp-image-820\" src=\"https:\/\/cbsepython.in\/wp-content\/uploads\/2021\/05\/functions-in-python-300x201.jpg\" alt=\"working with functions in Python\" width=\"384\" height=\"257\" title=\"\" srcset=\"https:\/\/cbsepython.in\/wp-content\/uploads\/2021\/05\/functions-in-python-300x201.jpg 300w, https:\/\/cbsepython.in\/wp-content\/uploads\/2021\/05\/functions-in-python.jpg 559w\" sizes=\"(max-width: 384px) 100vw, 384px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h3>Scope and Lifetime of variables<\/h3>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000;\">When dealing with Python functions, you should also consider the scope of the variables. The code in a function is in its own little world, separate from the rest of the program. Any variable declared in the function is ignored by the rest of the function. This means that two variables with the same name can exist, one inside the function and the other outside the function. However, this is not a good practice. All variable names must be unique regardless of their scope.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000;\"><strong>Local variable:<\/strong> A variable that is defined within a function and can only be used within that particular function. With respect to the local variable, the area in a function is called &#8220;local area&#8221;.<\/span><\/p>\n<p><span style=\"color: #000000;\"><strong>Global variable:<\/strong> A variable that is defined in the main part of the programming and, unlike local variables, can be accessed via local and global areas.<\/span><\/p>\n<p>&nbsp;<\/p>\n<h3><span style=\"color: #000000;\">In this article you learned functions in Python.\u00a0For More on working with functions in python Check Here: <a href=\"https:\/\/cbsepython.in\/introduction-to-python-module-class-11-computer-science\/\">Introduction to Python Module<\/a><\/span><\/h3>\n<h3><span style=\"color: #000000;\">Also Check: <a href=\"https:\/\/cbsepython.in\/random-module-in-python-class-11-12-notes\/\">What is Random Module in Python? How to use random Module?<\/a><\/span><\/h3>\n","protected":false},"excerpt":{"rendered":"<p>Working with Functions in Python Class 12 Notes Working with Functions in Python Class 12 Notes:\u00a0 Here you will learn about the functions in Python, Types of Functions in Python, How to create a function in Python, how function works in Python. These &#8220;Working with Function Notes&#8221; will surely helpful for the Computer Science, Informatics [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15,16,20],"tags":[],"class_list":["post-794","post","type-post","status-publish","format-standard","hentry","category-cbse-sample-papers-class-11","category-cbse-sample-papers-class-12","category-cbse-computer-science-with-python-class-12"],"_links":{"self":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/posts\/794","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/comments?post=794"}],"version-history":[{"count":4,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/posts\/794\/revisions"}],"predecessor-version":[{"id":5569,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/posts\/794\/revisions\/5569"}],"wp:attachment":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/media?parent=794"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/categories?post=794"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/tags?post=794"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}