Programming Languages

What is a function and why should I use one?

A function is a named block of code that does one job. You can run it whenever you need it by calling its name, and you can give it inputs (called parameters or arguments) and get a result back.

Why functions help: 1) They avoid repetition. Write the code once and reuse it. 2) They make code easier to read, because a good name like calculate_total explains what happens. 3) They make bugs easier to find, because you can test one small piece at a time. 4) They make changes easier. If the rule changes, you fix it in one place.

A practical test: if you copy and paste the same lines more than twice, consider making a function. Aim for each function to do one thing.

What was the first function you wrote?

What is the difference between a variable and a constant?

A variable is a named place in memory that holds a value, and the value can change while the program runs. A constant also has a name and a value, but the value is not supposed to change after it is set.

Examples: 1) A variable: a score in a game. It starts at 0 and goes up as the player earns points. 2) A constant: a value like the number of days in a week, or a tax rate your program uses everywhere. 3) Using a constant gives the value a meaningful name and means you only need to change it in one place if it ever changes. 4) Some languages enforce constants with a keyword such as const or final. Others, such as Python, rely on a naming convention, usually capital letters like TAX_RATE.

A good habit is to use names that say what the value is, such as total_price, not x.

Can you think of a value in your own project that should be a constant?

Explain the key differences between procedural programmin...

This question aims to assess the understanding of two fundamental programming paradigms: procedural programming and object-oriented programming (OOP).

Category