Loading...

Python Functions


in this tutorial we are going to learn about functions in python. what is a function, how to create a function, how to call a function, what are paremeters and arguments in python, what are return statements, what are pass statements in python


What is a function in python ?


a function is a group or collection of statements to perform a specific task in python programs.
it helps in re-usablity of the code. once define a funtion you can use it anywhere in the program.


How to create a function in python ?


in python we create a function by using the def keyword before the function name
the function name must followed by parentheses ( ) and colon :
for example:
        		     					    		
def greetfunction():
      print('welcome to the python world !')

        		     					    		
        		     					    	


How to call a function in python ?


in python it is so simple by just write the function name followed by parentheses ( )
for example:
        		     					    		
def greetfunction():
      print('welcome to the python world !')

greetfunction()

output:

welcome to the python world !

        		     					    		
        		     					    	


What are Parameters and Arguments in python ?


both parameters and arguments are used to pass data to a function on which function will perform actions.

Parameters
in python parameters acts as a variable placeholders which we pass in the parentheses of the function.
it is used when we define the function in the program.
for example:
        		     					    		
def greetfunction(name):
      print('hi '+ name + ' welcome to the python world !')


        		     					    		
        		     					    	
in the above example name is the parameter in the greetfunction.

Arguments
in python arguments refers to the actual data values which we pass in the parentheses of the function.
it is used when we actually call the function.
for example:
        		     					    		
def greetfunction(name):
      print('hi '+ name + ' welcome to the python world !')

greetfunction('robert')

output:

hi robert welcome to the python world !

        		     					    		
        		     					    	
in the above program robert is a argument in greetfunction

Default Parameters
in python default parameters are used to initialize the default values to a function definition.
it is used when we call a function and does not provide any argument to it.
for example:
        		     					    		
def greetfunction(name = 'robert'):
      print('hi '+ name + ' welcome to the python world !')

greetfunction()

output:

hi robert welcome to the python world !
        		     					    		
        		     					    	


Pass statement in python


in python the pass statement is used to define a empty function, empty class etc.
in python no one can define a empty function but to rescue from this situation we use pass in funtion definition to avoid run time error.
you can think of it as a place holder in the function definition. which will be filled by later in future.
for example:
        		     					    		
def taskfunction():
	pass
	


        		     					    		
        		     					    	



Return statements in python


in python return statements are used to stops the execution of the function call after getting the result from the return statements
for example:
        		     					    		
def addfunction(val1, val2):
	return val1 + val2

x = 10

y = 11

sum = addfunction(x, y)

print(f"the sum of {x} and {y} is : {sum}")

output:

the sum of 10 and 11 is : 21	


        		     					    		
        		     					    	
in the above example x and y are two variables having values of 10 and 11
when we assign these variables to addfunction(x, y) then function return the addition values to the sum variable.