<jupyter_start><jupyter_text>Writing functions1. Give it a def name( ):2. Write a docstring - allows other people reading your code to know what the purpose of the function is!3. Decide parameters4. Decide return5. Implement6. Test**Let's generate a bar plot!**  Convention: - function names are lower_case_with_underscores- place imports at the top of the file- Function paramaters:    1. Obligatory    2. Parameters with default values (always goes after obligatory)    3. *args - list of extra parameters (any number)    4. **kwargs - dictionary of extra named parameters (any number)<jupyter_code>from matplotlib import pyplot as plt
%matplotlib inline
def barplot(): #That's the first step completed!
    '''
    Draws a super nice barplot using my favourite colours 
    Displays the plot on the screen
    '''
    ... #is a valid python command that is just like passing 
        #it results in no errors
    
    print("Hello World") # This can be used to check function is working
    
barplot() # call the function to check it's working on python shell
def barplot():
    
    '''
    Draws a super nice barplot using my favourite colours 
    Displays the plot on the screen
    '''
    x = range(1,5)
    
    y = [20,50,10,30]

    plt.bar(x, y, color = ['red', 'orange', 'blue', 'green'])
    plt.show()
    
barplot()
def barplot(y):
    '''
    Draws a super nice barplot using my favourite colours 
    Displays the plot on the screen
    '''
    x = range(1,5)

    plt.bar(x, y, color = ['red', 'orange', 'blue', 'green'])
    plt.show()
    
data = [20,50,10,30]
    
barplot(data)<jupyter_output><empty_output><jupyter_text>Now let's introduce an optional argument!- If you don't specify the argument, it will revert to the default defined in the function!<jupyter_code>def barplot(y, title='Some Barplot'): 
    '''
    Draws a super nice barplot using my favourite colours 
    Displays the plot on the screen
    '''
    x = range(1,5)

    plt.bar(x, y, color = ['red', 'orange', 'blue', 'green'])
    plt.title(title)
    plt.show()
    
data = [20,50,10,30]
    
barplot(data) # This one runs default title
barplot(data, title = 'My First Barplot')
def barplot(y, title='Some Barplot', *args): 
    '''
    Draws a super nice barplot using my favourite colours 
    Displays the plot on the screen
    '''
    print(args)
    
    x = range(1,5)

    plt.bar(x, y, color = ['red', 'orange', 'blue', 'green'])
    plt.title(title)
    plt.show()
    
data = [20,50,10,30]

#Need to remove 'title=' 
barplot(data,'My First Barplot', 1, 2, 3)<jupyter_output>(1, 2, 3)<jupyter_text>Now we can actually use *args to specify colour of bar plot outside the function!<jupyter_code>def barplot(y, title='Some Barplot', *args, **kwargs): 
    '''
    Draws a super nice barplot using my favourite colours 
    Displays the plot on the screen
    '''
    print(kwargs)
    
    x = range(1,5)

    plt.bar(x, y, color=args)
    plt.title(title)
    plt.show()
    
data = [20,50,10,30]

barplot(data,'My First Barplot', 'blue', 'purple', 'turquoise', 'indigo',
        filename='bar.png')<jupyter_output>{'filename': 'bar.png'}
{'fraction_non_alphanumeric': 0.11994909322303532, 'fraction_numerical': 0.020999045497931912, 'mean_word_length': 3.265943012211669, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '542011', 'license': 'MIT', 'n_tokens_mistral': 1061, 'n_tokens_neox': 999, 'n_words': 367, 'path': '07_WritingFunctions_Barplots.ipynb', 'repo_name': 'maximcondon/Appendix', 'size': '3143'}