Thursday, April 25, 2019

return and void functions

Void functions
Implemenation on couting 10 random even and odd numbers

import random

def main():
  sub(10)

def sub(n):
  counteven=0
  countodd=0
  for i in range(n):
    a=random.randint(10,100)
    print(a) 
    if a%2==0:
      counteven+=1
    else:
      countodd+=1
  print("\n",a,counteven,countodd)

main()






Return functions

import random

def main():
    #we have to assign variables to function to return values
    #it's function like mapping
    #the first variables evenc is mapped to couteven of sub
    evenc,oddc=sub(10)
    print("evencout is",evenc)
    print("odd count is",oddc)
 


def sub(n):
  counteven=0
  countodd=0
  for i in range(n):
    a=random.randint(10,100)
    print(a)
    if a%2==0:
      counteven+=1
    else:
      countodd+=1
   #we can return more than one variables 
  return(counteven,countodd)

main()



easiest example




#this is return function
def ret():
a=33
return(a)


def mohan():
#return function directly output return values
if(ret()==33):
print("echo")
print(ret())
mohan()

print(ret())


result
33 echo 33





No comments:

Post a Comment