Skip to content
Lesson 4

Functions As Recipes

A function is a saved recipe. You write it once. You give it a name. From that point on, anywhere in the program, you call the name and the recipe runs. Without functions, every interesting program would be five thousan…

There is more than one way to understand this. If you have only been taught one, you have been taught less than you deserve.

There is more than one way to understand this. If you have only been taught one, you have been taught less than you deserve.

A function is a saved recipe. You write it once. You give it a name. From that point on, anywhere in the program, you call the name and the recipe runs. Without functions, every interesting program would be five thousand lines of repetition. With them, a program of five thousand lines becomes maybe forty named recipes calling each other in different combinations. The art of programming, after the first month, is mostly the art of choosing what to put inside a function and what to call it.

The cleanest example in any Pakistani kitchen is the paratha recipe. Watch your dadi make one. The steps are the same every time. Take a ball of soft dough. Roll it flat with the belan. Spread ghee on top. Fold the corners in. Roll again. Place on the hot tawa. Flip when bubbles appear. Cook till brown spots show. The recipe has a name, paratha banana, and an output, one cooked paratha. If you want ten parathas, dadi runs the recipe ten times. She does not invent it again on the seventh paratha. In Python this is exactly how a function looks.

Open Replit. Type, def banana_paratha(): then on the next line indented, print("Aata gola lo"), and several more print lines for each step. After the function definition, on a fresh line, write banana_paratha(). Run. The eight steps print. Now write banana_paratha() three more times. Run. Twenty four steps print, four parathas worth. The function ran four times. You did not rewrite it. This is the first taste of why functions are powerful. The recipe lives once. Its uses are unlimited.

A second example, closer to entrepreneurship. Salman, a Class 9 student in Karachi, runs a lemonade stand outside his house every Saturday. He charges sixty rupees a glass. He uses three lemons, two spoons of sugar, and ice. He needs to know, for any given Saturday, how much he made and how much profit was left after costs. The sentence is the function. He calls it kamaai_aur_munafa. It takes glasses_sold and cost_per_glass as inputs. It returns revenue and profit. He runs it every Saturday evening. Same function, different inputs, different outputs each week. By the end of summer, he has thirteen rows of data, all generated by one tiny function he wrote in fifteen minutes.

The most important rule of writing functions is, do one thing well. A function called bake_paratha should bake a paratha. It should not also order the dough, set the table, and call the children for breakfast. If your function does five things, split it into five smaller ones. The smaller pieces are easier to test, easier to debug, and easier to share. This rule, single responsibility, is the same rule a good restaurant uses when it gives the chef one job, the waiter another, and the dishwasher a third. Roles are clean. Mistakes are found quickly. Imagine a kitchen where one person does everything badly and you understand the kind of code that breaks at midnight before a deadline.

Closing thought. The reason functions matter is the reason recipes matter. They let knowledge be passed on. Your dadi's paratha recipe leaves your house when she teaches it to the bahu, the maid, the granddaughter, and the family friend who wanted to learn. A function lets your code reach more people in the same way. Once written and well named, it can be copied to a friend's project, a college assignment, a startup at I9 Markaz Islamabad. The labour you put in once is repaid every time someone calls your name. This is how the world really gets built.

Estimated time: 13 min