docs(function1-workshop): finalize markdown workshop content
This commit is contained in:
@@ -1,118 +1,100 @@
|
|||||||
# 🧭 Topic: Function Basics
|
# 🧭 Topic: Function Basics
|
||||||
|
|
||||||
|
> [!info] **Quick Overview :**
|
||||||
> [!info] 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
|
## 📌 Covered in This Topic
|
||||||
|
|
||||||
**Introduction to Functions**
|
### **Introduction to Functions**
|
||||||
- What functions are
|
- What functions are (self-contained blocks of code)
|
||||||
- Key goals: modularity, reuse, readability, debugging
|
- Key goals:
|
||||||
- Real-world analogy: “tasks” or “machines” that take input and produce output
|
- 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**
|
### **Why We Need Functions**
|
||||||
- Code reuse
|
- Eliminating repetition (D.R.Y. — Don’t Repeat Yourself)
|
||||||
- Reducing complexity
|
|
||||||
- Avoiding repetition
|
|
||||||
- Breaking problems into sub-problems
|
- Breaking problems into sub-problems
|
||||||
- Making programs testable
|
- Reducing complexity
|
||||||
- Collaboration benefits: multiple people working on different functions
|
- Abstraction (main doesn’t care *how*, only *what*)
|
||||||
|
- Professional collaboration benefits
|
||||||
|
|
||||||
**Predefined Library Functions**
|
### **User-Defined Functions**
|
||||||
- Overview of `<cmath>`, `<iomanip>`, helpers
|
- Anatomy of a function:
|
||||||
- Calling library functions (syntax, examples)
|
- Return type
|
||||||
|
- Function name
|
||||||
|
- Parameters
|
||||||
|
- Body
|
||||||
|
- Function lifecycle:
|
||||||
|
- Declaration (prototype)
|
||||||
|
- Definition
|
||||||
|
- Calling
|
||||||
|
- Declaration vs. Definition
|
||||||
|
- Function prototypes and their purpose
|
||||||
|
|
||||||
**User-Defined Functions**
|
### **Return Types**
|
||||||
- Components of a user-defined function
|
- Allowed return types
|
||||||
- **Declaration** vs. **Definition** vs. **Call**
|
- Limitation: a function returns only **one value**
|
||||||
- Variables inside functions (local variables & scope)
|
- `void` functions:
|
||||||
- Global variables — why to avoid them
|
- Definition and use cases
|
||||||
- Lifetime and visibility of variables
|
- Using `return;` as an “exit door”
|
||||||
|
|
||||||
**Parameters, Arguments & Terminology**
|
### **Parameters & Arguments**
|
||||||
- Parameter vs. Argument (definition, examples)
|
- Parameter vs. argument (placeholder vs. actual data)
|
||||||
- Types of parameters: input, output, in-out
|
- Order and clarity of parameters
|
||||||
- Naming conventions for parameters
|
- Parameter names optional in prototypes
|
||||||
- Why parameter order and clarity matter
|
- Scope and lifetime of parameters
|
||||||
- Common student mistakes (e.g., confusing scope)
|
|
||||||
|
|
||||||
**Return Types**
|
### **Scope & Lifetime**
|
||||||
- `void` vs. non-void
|
- Local variables
|
||||||
- Returning multiple values → reference parameters
|
- Global variables:
|
||||||
- Return type limitations (only one return value)
|
- What they are
|
||||||
- Allowed data types
|
- Why to avoid them
|
||||||
- When to use `return;` in void functions
|
- Static local variables
|
||||||
- Early returns and readability. (Guard Clause)
|
- Variable shadowing
|
||||||
|
- Priority of scopes
|
||||||
|
|
||||||
**Function Prototypes**
|
### **Default Arguments**
|
||||||
- What prototypes do
|
- Syntax of default parameters
|
||||||
- Why prototypes are needed in C++
|
- Rules:
|
||||||
- Where to place prototypes (before `main` or in headers)
|
- Must be placed on the **right**
|
||||||
- Typical student errors (e.g., mismatched signatures)
|
- Evaluated at **compile time**
|
||||||
|
- Correct vs. incorrect usage
|
||||||
|
- Common ambiguity problems
|
||||||
|
|
||||||
**Default Arguments**
|
### **Parameter Passing Methods**
|
||||||
- Syntax for default values
|
|
||||||
- Rules (right-to-left, only in declaration)
|
|
||||||
- Good vs. bad use cases
|
|
||||||
- Common pitfalls
|
|
||||||
|
|
||||||
**Parameter Passing Methods**
|
|
||||||
- **Pass by value**
|
- **Pass by value**
|
||||||
- Cheap for ints, bools, chars
|
- Copy is made
|
||||||
- Safe, makes a copy
|
- Original variable unchanged
|
||||||
|
- Best for small data types
|
||||||
- **Pass by reference (`&`)**
|
- **Pass by reference (`&`)**
|
||||||
- Used for modifying caller variables
|
- Works on original variable
|
||||||
- Used for performance
|
- Used for modification or performance
|
||||||
- **When to choose which method**
|
- When and why to choose each method
|
||||||
- Memory model overview (stack frame, copies)
|
|
||||||
|
|
||||||
|
### **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)
|
||||||
|
|
||||||
**Function Overloading**
|
### **Clean Code & Best Practices**
|
||||||
- What overloading is
|
- Syntax and formatting
|
||||||
- Rules for valid overloading
|
- Function placement & structure
|
||||||
- Overloading vs. default args and ambiguity
|
- Meaningful naming (verbs for functions)
|
||||||
- Examples: `abs`, `sort`, etc.
|
- SRP (Single Responsibility Principle)
|
||||||
- Common pitfalls (type conversion confusion)
|
- Function length and complexity
|
||||||
|
- Refactoring deeply nested logic
|
||||||
**Clean Code & Best Practices**
|
|
||||||
**Syntax**
|
|
||||||
- Consistent formatting
|
|
||||||
- Clear parameter naming
|
|
||||||
- Avoid deeply nested logic inside functions
|
|
||||||
|
|
||||||
**Naming**
|
|
||||||
- Verbs for functions (`calculateTotal`, `isValid`)
|
|
||||||
- Avoid generic names (`func1`, `doStuff`)
|
|
||||||
- Self-documenting naming
|
|
||||||
|
|
||||||
**SRP (Single Responsibility Principle)**
|
|
||||||
- One function should do one thing
|
|
||||||
- How complex is “too complex”?
|
|
||||||
- When to break a function into smaller pieces
|
|
||||||
|
|
||||||
**Function Length & Complexity**
|
|
||||||
- Cyclomatic complexity
|
|
||||||
- Indicators that a function needs refactoring
|
|
||||||
- How to make logic testable
|
|
||||||
|
|
||||||
**Scope, Lifetime & Static Functions**
|
|
||||||
- Local vs. global variables
|
|
||||||
- Shadowing
|
|
||||||
- `static` local variables
|
|
||||||
- When to use function-level statics
|
|
||||||
- Danger of relying on state
|
|
||||||
|
|
||||||
**Common Student Mistakes**
|
|
||||||
- Defining a function inside another function
|
|
||||||
- Forgetting to return a value
|
|
||||||
- Mismatched declaration and definition
|
|
||||||
- Depending on global variables instead of parameters
|
|
||||||
- (Optional) Passing large objects by value
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 📑 Slides & Materials
|
## 📑 Slides & Materials
|
||||||
- 👨🏫 [Professor Slides (PDF)](01-topics/04-function-1/4-ProfessorSlides.md)
|
- 👨🏫 [Professor Slides (PDF)](01-topics/04-function-1/4-ProfessorSlides.md)
|
||||||
- 🧑🏫 [TA Workshop Slides (PDF)](01-topics/04-function-1/5-TASlides.md)
|
- 🧑🏫 [TA Workshop Slides (PDF)](01-topics/04-function-1/5-TASlides.md)
|
||||||
@@ -126,8 +108,7 @@
|
|||||||
|
|
||||||
---
|
---
|
||||||
## 🌐 Additional Resources
|
## 🌐 Additional Resources
|
||||||
|
- C++ Reference (cplusplus.com)
|
||||||
- [C++ Reference](https://cplusplus.com/reference/)
|
|
||||||
|
|
||||||
---
|
---
|
||||||
## ⏩ Navigation
|
## ⏩ Navigation
|
||||||
@@ -136,6 +117,7 @@
|
|||||||
- ➡️ [Next Topic: Arrays](../05-arrays/0-Overview.md)
|
- ➡️ [Next Topic: Arrays](../05-arrays/0-Overview.md)
|
||||||
---
|
---
|
||||||
|
|
||||||
> [!tip] Tip
|
> [!tip] **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.
|
> 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.
|
||||||
|
|
||||||
|
|||||||
@@ -14,18 +14,17 @@
|
|||||||
|
|
||||||
## 📦 Download All Questions and Solutions
|
## 📦 Download All Questions and Solutions
|
||||||
|
|
||||||
- **All questions (zip):** [Download all questions]()
|
- **All questions (zip):** [Download all questions](https://drive.google.com/file/d/19EIICYkB7sUmsEVB6da5eZ5MQ7WTuP88/view?usp=drive_link)
|
||||||
- **All official solutions (zip):** [Download all official solutions]()
|
- **All official solutions (zip):** [Download all official solutions](https://drive.google.com/file/d/1j0gtsHGPOcpH8GOVAyvxFqecCze1SQH0/view?usp=drive_link)
|
||||||
|
|
||||||
---
|
---
|
||||||
## 🧾 Official Questions & Solutions
|
## 🧾 Official Questions & Solutions
|
||||||
|
|
||||||
| Question # | Question (Download) | Official Solution (Download) | Additional Notes |
|
| Question # | Question (Download) | Official Solution (Download) | Additional Notes |
|
||||||
| ---------- | ------------------- | ---------------------------- | ---------------- |
|
| ---------- | ------------------- | ---------------------------- | ---------------- |
|
||||||
| 01 | [Download PDF]() | [Download Solution (zip)]() | |
|
| 01 | [Download PDF](https://drive.google.com/file/d/1LZysw18yVMxIeSfI5myQ-PJEtHD5_Qgl/view?usp=drive_link) | [Download Solution (zip)](https://drive.google.com/file/d/1piw1JTjDYyrfhyDaFOlzbwsyp0exbMws/view?usp=drive_link) | |
|
||||||
| 02 | [Download PDF]() | [Download Solution (zip)]() | |
|
| 02 | [Download PDF](https://drive.google.com/file/d/1_Qvwn_c2AUgAEYWYLs2D8rwDgzklFr2-/view?usp=drive_link) | [Download Solution (zip)](https://drive.google.com/file/d/1njLCfQOu7Wg5JOgigoLM9D8ApACNRleT/view?usp=drive_link) | |
|
||||||
| 03 | [Download PDF]() | [Download Solution (zip)]() | |
|
| 03 | [Download PDF](https://drive.google.com/file/d/1bUqIBdveAywXfEIxdAMHQdvzrf2qS2H-/view?usp=drive_link) | [Download Solution (zip)](https://drive.google.com/file/d/1Ig4BJdoivI__8_1Aj2uZm9-KRWh0zT-1/view?usp=drive_link) | |
|
||||||
| 04 | [Download PDF]() | [Download Solution (zip)]() | |
|
|
||||||
|
|
||||||
> [!note] Not available yet?
|
> [!note] Not available yet?
|
||||||
> Some solutions will be uploaded after the workshop or assignment deadline. Check back later or ask your TA.
|
> Some solutions will be uploaded after the workshop or assignment deadline. Check back later or ask your TA.
|
||||||
|
|||||||
Reference in New Issue
Block a user