Python function return multiple values - codebugfree

python function return multiple values

Return the multiple values in python function is easy in python programming language. In other language like c it is quit difficult but in python to easy. In this section we learn and see the example of how to python function return multiple values. 
First we know the basic python function. The python function is the block or group of code that only run when programmer calls. We can sent data in python function as know as parameter. In python there are mainly two types of function they are built in function and programmer make function using lamda and def. 

How to python function return values

def function_name(function_input_parameter):
    return

def is the starting of the function. Function name is the name of function inside the (). it is the function input parameter means what a function input like number for int. last semicolon : is the end of the any function second line return is means what actual function return when we call function. we can use return and print in same position of function but it is perform different task. when we use print not return than doesn't use print when call function. Same opposite in return use print when call the function.

Example with return

def codebugfree(names):
    return "hello codebugfree "+ names

print(codebugfree("Visitors"))

We use return  and function call with print.

INOUTPUT

hello codebugfree Visitors

Example with print

def multiple_values(Input_here):
    print(Input_here,"are you fine")
multiple_values("visitors")

We use print and function call without using print 

INOUTPUT

visitors are you fine

All python programmer confused difference between return and print. Remember this point when you use print and call the function with print than function doesn't have return value and function return None. 

def not_use_return(noreturn):
    print("i am not use return")
  print(not_use_return("call return value"))

INOUTPUT

i am not use return
    None

How to python function return multiple values

python function return a multiple values by separated  commas in return value. When separated by return comma its return the tupples value. this () sing is optional to use after the return value but after the print () it is not optional. 

def function_name():
    return "codebugfree", 10000
visitors = function_name()
print(visitors)
print(type(visitors))

INOUTPUT

('codebugfree', 10000)
class 'tuple'

You can return more than multiple values. Now return three values and every return values assign different variables.

def three_values():
    return "website","codebugfree",[1,34,5,6]

a,b,c = three_values()
print(a)
print(type(a))
print(b)
print(type(b))
print(c)
print(type(c))

Here return have more than multiple values and make object with  a,b and c it is different variable. 

INOUTPUT

website
class 'str'
codebugfree
class 'str'
[1, 34, 5, 6]
class 'list'

Now we make the python project as make own adding function  and greet people like good morning. If you know to write this program and send me in comment if you don't know then see the example

def numplus (num1 , num2):#this fucton is use to add sum of number
    return num1+num2
add = numplus(5, 5)
print(add)
def hello (name):#this is also called call fuction
    print("hello"+ name)
hello(" sir")

Explain this program  we make function name as numplus and input parameter 2 value num1 and num2 return the call function num1+num2 add is passing the 2 number they add. 

Another greet program input parameter as name use print passing the sir in function. Let see in output

INOUTPUT

10
hello sir

Post a Comment

0 Comments