<jupyter_start><jupyter_text>![title](img/cover4.png) Copyright! This material is protected, please do not copy or distribute.    by:<NAME> ***Udemy course : Python Bootcamp for Data Science 2021 Numpy Pandas & Seaborn ***  5.1 Creating and Calling Functions We can create a simple function that adds to number like this:<jupyter_code>def sum_function(a, b):
    sum = a + b
    return sum<jupyter_output><empty_output><jupyter_text>We can call a function like this:<jupyter_code>sum_function(4,5)<jupyter_output><empty_output><jupyter_text>We can add an optional argument, if we specify its value, for example (c=1) in this example:<jupyter_code>def sum_function(a, b, c= 1):
    sum1 = a + b
    weighted_sum = sum1/c 
    return weighted_sum<jupyter_output><empty_output><jupyter_text>We can call the function without the optional argument<jupyter_code>sum_function(4,5)<jupyter_output><empty_output><jupyter_text>Also we can call the function with the optional argument, the new value is used for the optional argument.<jupyter_code>sum_function(4,5,2)<jupyter_output><empty_output><jupyter_text>For calrity, we can use the name of the arguments as well:<jupyter_code>sum_function(a = 4, b = 5, c= 2)<jupyter_output><empty_output><jupyter_text>The variable insdie the function cannot be accesses from ouside, because it is a local variable. if we try to access a local variable we get an **error**:<jupyter_code>sum1<jupyter_output><empty_output><jupyter_text>We can declare a local variable to be a global variable like this:<jupyter_code>sum1 = None
def sum_function(a, b, c= 1):
    global sum1
    sum1 = a + b
    weighted_sum = sum1/c 
    return weighted_sum<jupyter_output><empty_output><jupyter_text>The function can be called normally:<jupyter_code>sum_function(4,5)<jupyter_output><empty_output><jupyter_text>And we can now also access the variable inside the function without any problem:<jupyter_code>sum1<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08862704918032786, 'fraction_numerical': 0.015368852459016393, 'mean_word_length': 6.54054054054054, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '454977', 'license': 'MIT', 'n_tokens_mistral': 678, 'n_tokens_neox': 654, 'n_words': 196, 'path': '5.1 Creating and Calling Functions.ipynb', 'repo_name': 'SiamakMushakhian/Numpy-Pandas-Seaborn', 'size': '1957'}