🧭 Topic: Function Basics

Quick Overview :

This topic introduces functions in C++, why we use them, how they are defined and called, how parameters and return values work, and how to write clean, professional function-based code.


📌 Covered in This Topic

Introduction to Functions

  • What functions are (self-contained blocks of code)
  • Key goals:
    • Modularity
    • Reusability (Write once, use everywhere)
    • Readability
    • Debugging & error isolation
  • Real-world analogy: a “machine” that takes input and produces output

Why We Need Functions

  • Eliminating repetition (D.R.Y. — Don’t Repeat Yourself)
  • Breaking problems into sub-problems
  • Reducing complexity
  • Abstraction (main doesn’t care how, only what)
  • Professional collaboration benefits

User-Defined Functions

  • Anatomy of a function:
    • Return type
    • Function name
    • Parameters
    • Body
  • Function lifecycle:
    • Declaration (prototype)
    • Definition
    • Calling
  • Declaration vs. Definition
  • Function prototypes and their purpose

Return Types

  • Allowed return types
  • Limitation: a function returns only one value
  • void functions:
    • Definition and use cases
    • Using return; as an “exit door”

Parameters & Arguments

  • Parameter vs. argument (placeholder vs. actual data)
  • Order and clarity of parameters
  • Parameter names optional in prototypes
  • Scope and lifetime of parameters

Scope & Lifetime

  • Local variables
  • Global variables:
    • What they are
    • Why to avoid them
  • Static local variables
  • Variable shadowing
  • Priority of scopes

Default Arguments

  • Syntax of default parameters
  • Rules:
    • Must be placed on the right
    • Evaluated at compile time
  • Correct vs. incorrect usage
  • Common ambiguity problems

Parameter Passing Methods

  • Pass by value
    • Copy is made
    • Original variable unchanged
    • Best for small data types
  • Pass by reference (&)
    • Works on original variable
    • Used for modification or performance
  • When and why to choose each method

Function Overloading

  • Definition of overloading
  • Valid overloading rules
  • Return type alone does NOT overload
  • Overload resolution basics
  • Numeric literal suffixes and their effect
  • Overloading vs. default arguments (ambiguity)

Clean Code & Best Practices

  • Syntax and formatting
  • Function placement & structure
  • Meaningful naming (verbs for functions)
  • SRP (Single Responsibility Principle)
  • Function length and complexity
  • Refactoring deeply nested logic

📑 Slides & Materials


🛠️ Workshop & Assignments


🌐 Additional Resources

  • C++ Reference (cplusplus.com)

⏩ Navigation


Tip :

If something doesn’t make sense — don’t stay stuck alone. Ask the TA to clarify it right away. Often, a quick question can save you a lot of time and frustration. Remember: if you’re confused, chances are others are too.