Functions in real world

All programming functions contain a set of instructions used to create the output from its input. When we feel (Input) something, we consequently respond (Output) to that feeling.

A function should be created for commonly used routine. Let’s make a really, really good homemade Pizza every day!

Python
def make_pizza():

print(‘Preheat the oven’)
print(‘Make your own pizza dough’)
print(‘Add homemade salsa as a base for your pizza sauce’)
print(‘Add some homegrown produce’)
print(‘Add the pork’)
print(‘Load your pizza with cheddar cheese’)
print(‘Place the pizza in the preheated oven, and bake for 20 minutes’)
pizza = ‘delicious pizza’
return pizza

python

Code reuse

Duplication is a waste because it causes unnecessary difficulties, is unmaintainable, and creates opportunities for more bugs. Learn more

Eating pizza will get old. Let’s make pizza and pancakes with different ingredients.

Python
def make_pizza(*ingredients):

print(‘Preheat the oven’)
print(‘Make your own pizza dough’)
add_and_mix(*ingredients)
print(‘Place the pizza in the preheated oven, and bake for 20 minutes’)
pizza = ‘delicious pizza with {} ingredients’.format(len(ingredients))
return pizza

def make_pancake(*ingredients):

add_and_mix(*ingredients)
print(‘Pre-heat the pan or skillet’)
print(‘Add oil to pan’)
print(‘Pour the batter into the pan’)
print(‘Flip the pancakes when the bubbles burst’)
pancake = ‘delicious pancake with {} ingredients’.format(len(ingredients))
return pancake

def add_and_mix(*ingredients):

return print(‘Add {} ingredients and mix in a bowl’.format(len(ingredients)))

make_pizza(‘olive oil’,’salt’,’sugar’)
make_pancake(‘flour’,’baking powder’, ‘salt’, ‘sugar’,’milk’,’eggs’,’oil’)

python-2

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s