#simple_function.py
#libraries
import random
import sys
import os
import time

#functions are defined   as   def function_name ( arguments ):
#   examples are
#   def  my_func():        # note: no argumens
#   def  func_two (val1, 16 )     # two arguments
#   a_list = [1,2,3,"bank"]
#   def  func_three( val_2, a_list,"Harry")   #Note: one argument is a list
#
#    The arguments of a function may then be used in the function
#
#   functions may "return" values.   See examples below

#################################
def  multiply( val1, val2):
    product = (val1 * val2)
    return(product)

mult_1 = 14
mult_2 = 8
ans = multiply(mult_1, mult_2)
print("\nanswer to ",mult_1," times ",mult_2," is ",ans)

#################################
def make_list( start,end):
    list = []
    for num in range(start,end):
        list.append(num)
    return(list)

out_list = make_list( 5, 12)
print("\nout_list from make_list function is ",out_list)

#################################
def multi_val_return( val1, val2):
    out1 = val1 **.5
    out2 = val2 **.5
    return(out1,out2)

#################################
out = (multi_val_return( 17, 26))
(out_val1,out_val2) = out
print("\nValues from multi_val_return function are :")
print(out_val1)
print(out_val2)

print("\n\nThe Examples below relate to the \"Scope\" of variables.")
# Scope of variables 
#Note:  The examples below deal with the "scope" of variables.

# scope refers to how variables defined outside a function may, or may
#  not be used inside a function.

#These examples can be ignored if
#  (a) You do not use variable names inside a function that are the
#       same as those outside the function..unless you know
#       the rules affecting doing that
#  (b) you always pass values in as aguments and pass values back out
#       as return values,
# The examples above meet conditions (a) and (b)


# a function can reference a variable outside the function, but if
# if it does so, any attempt to change that variable will cause
# a run_time error.

outside_var = 20

print("\n#####################  ref_var_function")
print("\nbefore running ref_var()  outside_var is ",outside_var)

def ref_var():
    print("while inside ref_var function the outside_var is ",outside_var)
    in_var = outside_var *5
    return(in_var)
returned_var = ref_var()

print("value returned  from rev_var function is ",returned_var)

#if function sets or changes the value of variable named outsdie the
# function,  it has no relation to the value of the variable with the
# same name outside the variable

print("\n#####################  var_local_to_function    function")
print("\nbefore running var_local_to_function()  outside_var is ",outside_var)
def var_local_to_function():
    outside_var = 200
    print("Inside function, outside_var is ", outside_var)
    #end of function

var_local_to_function()
print("after running var_local_to_function outside_var is still ",  outside_var)

#if an outside variable is defined as "global" in the function, then
# it can be used changed in the function and the change persist
# after the funtion has been run.

normal_val_outside_function = 100

val_to_be_made_global = 200

out_side_val = 300
print("\n#####################  change_vals   function ")
print("before running change_vals function :")
print("    normal_val_outside_function is ",normal_val_outside_function)
print("    val_to_be_made_global is ",val_to_be_made_global)
print("    out_side_val is ",out_side_val)

def change_vals():
    global val_to_be_made_global
    print("while in change_vals() function: ")
   
    print("    out_side_val is ",out_side_val," and cannot be changed")
    print("    val_to_be_made_global is initially",val_to_be_made_global)

    
    val_to_be_made_global = val_to_be_made_global * 3
    normal_val_outside_function = 35

  
    print("    normal_val_outside_function set to ",normal_val_outside_function)
    print("    val_to_be_made_global changed to ",val_to_be_made_global, "inside function")
    # end of function, Note, not return statement needed

change_vals()

print("\nafter change_vals function is run: ")
print("    normal_val_outside_function is unchanged : ",normal_val_outside_function)
print("    val_to_be_made_global was changed to ",val_to_be_made_global)
    
    
