Calling 2 functions from within a function

A variable which is defined inside a function is local to that function. It is accessible from the point at which it is defined until the end of the function and exists for as long as the function is executing.

  1. you can pass the values as arguments to other functions

    def naming(in_name): # function to respond to name as per user input 
        if in_name == 'David':
            print 'That\'s a cool name!!'
        elif in_name == 'Jane':
            print 'That\'s a great name!!'
        else:
            print 'That\'s an OK name!!!'
    
    def age(in_age): # function to respond to age as per user input 
        if in_age > 60:
            print 'That\'s old!!'
        elif in_age < 15:
            print 'That\'s young!!'
        else:
            print 'That\'s neither young nor old!!'
    
    def loc(in_loc): # function to respond to location as per user input 
        if in_loc == 'London':
            print 'London is a big city!!'
        elif in_loc == 'Manchester':
            print 'Manchester is a wonderful place!!'
        else:
            print 'That sounds OK!!'
    
    
    def questions(): #function to own the whole process (name + age + loc)
        in_name = raw_input('What is your name? -->')
        naming(in_name)
        in_age = input('How old are you? -->')
        age(in_age)
        in_loc = raw_input('Where do you live? -->')
        loc(in_loc)
        print 'Your name is',in_name,', you are' ,in_age , 'years old and you live in' , in_loc,'.'
    
    questions()
    
  2. otherwise you can define the variables as global, so you don’t have to pass it as an argument.(This method is not recommended)

    def naming(): # function to respond to name as per user input 
        if in_name == 'David':
            print 'That\'s a cool name!!'
        elif in_name == 'Jane':
            print 'That\'s a great name!!'
        else:
            print 'That\'s an OK name!!!'
    
    def age(): # function to respond to age as per user input 
        if in_age > 60:
            print 'That\'s old!!'
        elif in_age < 15:
            print 'That\'s young!!'
        else:
            print 'That\'s neither young nor old!!'
    
    def loc(): # function to respond to location as per user input 
        if in_loc == 'London':
            print 'London is a big city!!'
        elif in_loc == 'Manchester':
            print 'Manchester is a wonderful place!!'
        else:
            print 'That sounds OK!!'
    
    
    def questions(): #function to own the whole process (name + age + loc)
        global in_name, in_age, in_loc
        in_name = raw_input('What is your name? -->')
        naming()
        in_age = input('How old are you? -->')
        age()
        in_loc = raw_input('Where do you live? -->')
        loc()
        print 'Your name is',in_name,', you are' ,in_age , 'years old and you live in' , in_loc,'.'
    
    questions()
    

Leave a Comment