<jupyter_start><jupyter_text>Fruitful functionsFruitful functions - functions that returns value. In previous functions that we wrote we used void, they had no return value; more precisely, their return value is None. First Example of fruitful functions; which return the area of circle<jupyter_code>import math
def area(radius):
    rad = math.pi * radius**2
    return rad

area(5)<jupyter_output><empty_output><jupyter_text>Other Example:<jupyter_code>def absolute_val(x):
    if x < 0:
        return -x
    else:
        return x
    
absolute_val(-5)
def distance(x1,x2,y1,y2):
    dx = x2-x1
    dy = y2-y1
    print('dx is ',dx)
    print('dy is ',dy)
    return 0.0

distance(1, 2, 6, 8)
def distance(x1,x2,y1,y2):
    dx = x2-x1
    dy = y2-y1
    dxy_quared = dx**2+dy**2
    result = math.sqrt(dxy_quared)
    return result
distance(1, 2, 6, 8)<jupyter_output><empty_output><jupyter_text>you can also return booleans:<jupyter_code>def is_divisible(x,y):
    if x%y == 0:
        return True
    else:
        return False
    
is_divisible(5,2)
is_divisible(6,2)<jupyter_output><empty_output><jupyter_text>Because reult of == is a boolean, we can return value directly<jupyter_code>def is_divisible(x,y):
    return x%y == 0
is_divisible(6,2)<jupyter_output><empty_output><jupyter_text>Boolean functions are often used in conditional statements:<jupyter_code>x=2
y=2

if is_divisible(x,y):
    print('x is divisible by y')
x=2
y=4

if is_divisible(x,y):
    print('x is divisible by y')
else:
    print('x is NOT divisible by y')
def factorial(n):
    if n == 0:
        return 1
    else:
        recurse = factorial(n-1)
        result = n * recurse
        return result
    
factorial(5)<jupyter_output><empty_output><jupyter_text>Lets try other recursively defined function; fibonacci numbers<jupyter_code>def fibonacci(n):
    if n == 0:
        return 0
    elif n==1:
        return 1
    else:
        n = fibonacci(n-1)+fibonacci(n-2)
        print(n)
        return n
    
fibonacci(4)<jupyter_output>1
2
1
3
{'fraction_non_alphanumeric': 0.10832102412604629, 'fraction_numerical': 0.029049729197439686, 'mean_word_length': 3.4270152505446623, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 24, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '272851', 'license': 'MIT', 'n_tokens_mistral': 787, 'n_tokens_neox': 762, 'n_words': 197, 'path': 'python/fruitful_functions.ipynb', 'repo_name': 'piszewc/python-deep-learning-data-science', 'size': '2031'}