Embed presentation
Download to read offline



![Defining a FunctionHere are simple rules to define a function in Python:• Function blocks begin with the keyword def followed by thefunction name (shouldnot be keyword) and parentheses ( ) .• Any input parameters or arguments should be placed withinthese parentheses. You can also define parameters orarguments inside these parentheses.• The first statement of a function can be an optional statement- the documentation string of the function or docstring.• The code block within every function starts with a colon (:) andis indented.• The statement return [expression] exits a function, optionallypassing back an expression to the caller. A return statementwith no arguments is the same as return None.](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonunit3-250119131330-f203f0f8%2f75%2fJNTUK-python-programming-python-unit-3-pptx-4-2048.jpg&f=jpg&w=240)
![Syntax:def functionname( parameters ):"function_docstring“function_suite[expression]returnExampleDef add(a,b):sum=a+breturn suma=int(input(“enter first number”))b=int(input(“enter second number”))res=add(a,b)print(“result = “res)](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonunit3-250119131330-f203f0f8%2f75%2fJNTUK-python-programming-python-unit-3-pptx-5-2048.jpg&f=jpg&w=240)




![Variable-length arguments:You may need to process a function for more arguments than you specifiedwhile defining the function. These arguments are called variable-lengtharguments and are not named in the function definition, unlike required anddefault arguments.The general syntax for a function with non-keyword variable arguments is this:def functionname([formal_args,] *var_args_tuple ):"function_docstring"function_suitereturn [expression]](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonunit3-250119131330-f203f0f8%2f75%2fJNTUK-python-programming-python-unit-3-pptx-10-2048.jpg&f=jpg&w=240)





















![There is one more example where argument is being passed by reference but inside thefunction, but the reference is being over-written.def changeme( mylist ): "This changes a passed list"mylist = [1,2,3,4];print("Values inside the function: ", mylist)returnmylist = [10,20,30];changeme( mylist );Print("Values outside the function: ", mylist)The parameter mylist is local to the function changeme. Changing mylist within the functiondoes not affect mylist. The function accomplishes nothing and finally this would producefollowing result:Values inside the function: [1, 2, 3, 4]Values outside the function: [10, 20, 30]](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonunit3-250119131330-f203f0f8%2f75%2fJNTUK-python-programming-python-unit-3-pptx-32-2048.jpg&f=jpg&w=240)
![Pass by reference vs valueAll parameters (arguments) in the Python language are passed by reference. It means ifyou change what a parameter refers to within a function, the change also reflects backin the calling function. For example:def changeme( mylist ): "This changes a passed list“mylist.append([1,2,3,4]);print ("Values inside the function: ", mylist)returnmylist = [10,20,30];changeme( mylist );print ("Values outside the function: ", mylist)So this would produce following result:Values inside the function: [10, 20, 30, [1, 2, 3, 4]]Values outside the function: [10, 20, 30, [1, 2, 3, 4]]](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonunit3-250119131330-f203f0f8%2f75%2fJNTUK-python-programming-python-unit-3-pptx-33-2048.jpg&f=jpg&w=240)


The document provides a comprehensive overview of Python functions, highlighting their purpose in code reusability and debugging. It covers various types of arguments like required, keyword, default, and variable-length arguments, along with the use of lambda functions and the concept of local versus global variables. The document concludes with examples illustrating the scope of variables and the differences between pass-by-reference and pass-by-value.



![Defining a FunctionHere are simple rules to define a function in Python:• Function blocks begin with the keyword def followed by thefunction name (shouldnot be keyword) and parentheses ( ) .• Any input parameters or arguments should be placed withinthese parentheses. You can also define parameters orarguments inside these parentheses.• The first statement of a function can be an optional statement- the documentation string of the function or docstring.• The code block within every function starts with a colon (:) andis indented.• The statement return [expression] exits a function, optionallypassing back an expression to the caller. A return statementwith no arguments is the same as return None.](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonunit3-250119131330-f203f0f8%2f75%2fJNTUK-python-programming-python-unit-3-pptx-4-2048.jpg&f=jpg&w=240)
![Syntax:def functionname( parameters ):"function_docstring“function_suite[expression]returnExampleDef add(a,b):sum=a+breturn suma=int(input(“enter first number”))b=int(input(“enter second number”))res=add(a,b)print(“result = “res)](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonunit3-250119131330-f203f0f8%2f75%2fJNTUK-python-programming-python-unit-3-pptx-5-2048.jpg&f=jpg&w=240)




![Variable-length arguments:You may need to process a function for more arguments than you specifiedwhile defining the function. These arguments are called variable-lengtharguments and are not named in the function definition, unlike required anddefault arguments.The general syntax for a function with non-keyword variable arguments is this:def functionname([formal_args,] *var_args_tuple ):"function_docstring"function_suitereturn [expression]](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonunit3-250119131330-f203f0f8%2f75%2fJNTUK-python-programming-python-unit-3-pptx-10-2048.jpg&f=jpg&w=240)





















![There is one more example where argument is being passed by reference but inside thefunction, but the reference is being over-written.def changeme( mylist ): "This changes a passed list"mylist = [1,2,3,4];print("Values inside the function: ", mylist)returnmylist = [10,20,30];changeme( mylist );Print("Values outside the function: ", mylist)The parameter mylist is local to the function changeme. Changing mylist within the functiondoes not affect mylist. The function accomplishes nothing and finally this would producefollowing result:Values inside the function: [1, 2, 3, 4]Values outside the function: [10, 20, 30]](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonunit3-250119131330-f203f0f8%2f75%2fJNTUK-python-programming-python-unit-3-pptx-32-2048.jpg&f=jpg&w=240)
![Pass by reference vs valueAll parameters (arguments) in the Python language are passed by reference. It means ifyou change what a parameter refers to within a function, the change also reflects backin the calling function. For example:def changeme( mylist ): "This changes a passed list“mylist.append([1,2,3,4]);print ("Values inside the function: ", mylist)returnmylist = [10,20,30];changeme( mylist );print ("Values outside the function: ", mylist)So this would produce following result:Values inside the function: [10, 20, 30, [1, 2, 3, 4]]Values outside the function: [10, 20, 30, [1, 2, 3, 4]]](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpythonunit3-250119131330-f203f0f8%2f75%2fJNTUK-python-programming-python-unit-3-pptx-33-2048.jpg&f=jpg&w=240)

