Python Functions Examples - Call, Indentation, Arguments & Return Values
Python Functions Examples - Call, Indentation, Arguments & Return Values
Example:
Let us define a function by using the command " def func1():" and call the function. The
output of the function will be "I am learning Python function".
/
(/images/Pythonnew/Python10.1.png)
The function print func1() calls our def func1(): and print the command " I am learning
Python function None."
Python follows a particular style of indentation to define the code, since Python functions
don't have any explicit begin or end like curly braces to indicate the start and stop for the
function, they have to rely on this indentation. Here we take a simple example with "print"
command. When we write "print" function right below the def func 1 (): It will show an
"indentation error: expected an indented block".
/
(/images/Pythonnew/Python10.2.png)
Now, when you add the indent (space) in front of "print" function, it should print as
expected.
(/images/Pythonnew/Python10.3.png)
At least, one indent is enough to make your code work successfully. But as a best practice it
is advisable to leave about 3-4 indent to call your function.
It is also necessary that while declaring indentation, you have to maintain the same indent
for the rest of your code. For example, in below screen shot when we call another
statement "still in func1" and when it is not declared right below the first print statement it
will show an indentation error "unindent does not match any other indentation level."
/
(/images/Pythonnew/Python10.4.png)
Now, when we apply same indentation for both the statements and align them in the same
line, it gives the expected output.
(/images/Pythonnew/Python10.5.png)
Step 1) Here - we see when function is not "return". For example, we want the square of 4,
and it should give answer "16" when the code is executed. Which it gives when we simply
use "print x*x" code, but when you call function "print square" it gives "None" as an
output. This is because when you call the function, recursion does not happen and fall off
the end of the function. Python returns "None" for failing off the end of the function.
/
(/images/Pythonnew/Python10.6.png)
Step 2) To make this clearer we replace the print command with assignment command.
Let's check the output.
(/images/Pythonnew/Python10.7.png)
When you run the command "print square (4)" it actually returns the value of the object
since we don't have any specific function to run over here it returns "None".
Step 3) Now, here we will see how to retrieve the output using "return" command. When
you use the "return" function and execute the code, it will give the output "16."
/
(/images/Pythonnew/Python10.8.png)
Step 4) Functions in Python are themselves an object, and an object has some value. We
will here see how Python treats an object. When you run the command "print square" it
returns the value of the object. Since we have not passed any argument, we don't have any
specific function to run over here it returns a default value (0x021B2D30) which is the
location of the object. In practical Python program, you probably won't ever need to do
this.
(/images/Pythonnew/Python10.9.png)
Arguments in Functions
The argument is a value that is passed to the function when it's called.
In other words on the calling side, it is an argument and on the function side it is a
parameter.
Step 1) Arguments are declared in the function definition. While calling the function, you
can pass the values for that args as shown below
/
(/images/Pythonnew/Python10.10.png)
(/images/Pythonnew/Python10.11.png)
Example: x has no default values. Default values of y=0. When we supply only one
argument while calling multiply function, Python assigns the supplied value to x while
keeping the value of y=0. Hence the multiply of x*y=0
(/images/Pythonnew/Python10.12.png)
Step 3) This time we will change the value to y=2 instead of the default value y=0, and it will
return the output as (4x2)=8.
/
(/images/Pythonnew/Python10.13.png)
Step 4) You can also change the order in which the arguments can be passed in Python.
Here we have reversed the order of the value x and y to x=4 and y=2.
(/images/Pythonnew/Python10.14.png)
Step 5) Multiple Arguments can also be passed as an array. Here in the example we call the
multiple args (1,2,3,4,5) by calling the (*args) function.
Example: We declared multiple args as number (1,2,3,4,5) when we call the (*args)
function; it prints out the output as (1,2,3,4,5)
/
(/images/Pythonnew/Python10.15.png)
Tips:
#define a function
def func1():
print ("I am learning Python function")
print ("still in func1")
func1()
def square(x):
return x*x
print(square(4))
def multiply(x,y=0):
print("value of x=",x)
print("value of y=",y)
return x*y
print(multiply(y=2,x=4))
/
Here is the complete Python 2 code
#define a function
def func1():
print " I am learning Python function"
print " still in func1"
func1()
def square(x):
return x*x
print square(4)
def multiply(x,y=0):
print"value of x=",x
print"value of y=",y
return x*y
print multiply(y=2,x=4)
Summary:
Function in Python is a piece of reusable code that is used to perform single, related action.
In this article, we will see
/
Next (/if-loop-python-conditional-structures.html)
Python Tutorials
6) Variables in Python (/variables-in-python.html)
(https://www.facebook.com/guru99com/)
(https://twitter.com/guru99com)
(https://www.linkedin.com/company/guru99/)
(https://www.youtube.com/channel/UC19i1XD6k88KqHlET8atqFQ)
(https://forms.aweber.com/form/46/724807646.htm)
About
About Us (/about-us.html)
Advertise with Us (/advertise-us.html)
Write For Us (/become-an-instructor.html)
Contact Us (/contact-us.html)
Career Suggestion
SAP Career Suggestion Tool (/best-sap-module.html)
Software Testing as a Career (/software-testing-career-
complete-guide.html)
Interesting
Books to Read! (/books.html)
Blog (/blog/)
Quiz (/tests.html)
eBook (/ebook-pdf.html)
Execute online
Execute Java Online (/try-java-editor.html)
Execute Javascript (/execute-javascript-online.html)
Execute HTML (/execute-html-online.html)
Execute Python (/execute-python-online.html)
/
© Copyright - Guru99 2020
Privacy Policy (/privacy-policy.html) | Affiliate
Disclaimer (/affiliate-earning-disclaimer.html) | ToS
(/terms-of-service.html) | Address: 4023 Kennett Pike
#50286 Wilmington, Delaware 19807, USA