Functions in C++ Notes | Basic Computer Engineering | RGPV BTech First Year
Functions in C++ Notes | Basic Computer Engineering | RGPV BTech First Year
Functions in C++
Programming рдореЗрдВ рдЬрдм рдХрд┐рд╕реА рд╡рд┐рд╢реЗрд╖ рдХрд╛рд░реНрдп (Task) рдХреЛ рдмрд╛рд░-рдмрд╛рд░ рдХрд░рдирд╛ рдкрдбрд╝рддрд╛ рд╣реИ, рддрдм рдЙрд╕реА Code рдХреЛ рдмрд╛рд░-рдмрд╛рд░ рд▓рд┐рдЦрдирд╛ рдЙрдЪрд┐рдд рдирд╣реАрдВ рд╣реЛрддрд╛ред рдЗрд╕рд╕реЗ Program рд▓рдВрдмрд╛, рдЬрдЯрд┐рд▓ рддрдерд╛ Maintain рдХрд░рдирд╛ рдХрдард┐рди рд╣реЛ рдЬрд╛рддрд╛ рд╣реИред рдЗрд╕ рд╕рдорд╕реНрдпрд╛ рдХрд╛ рд╕рдорд╛рдзрд╛рди Function рджреНрд╡рд╛рд░рд╛ рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред Function Program рдХрд╛ рдПрдХ рд╕реНрд╡рддрдВрддреНрд░ (Independent) Block рд╣реЛрддрд╛ рд╣реИ рдЬреЛ рдХрд┐рд╕реА рд╡рд┐рд╢реЗрд╖ рдХрд╛рд░реНрдп рдХреЛ рдкреВрд░рд╛ рдХрд░рддрд╛ рд╣реИ рдФрд░ рдЖрд╡рд╢реНрдпрдХрддрд╛ рдкрдбрд╝рдиреЗ рдкрд░ рдмрд╛рд░-рдмрд╛рд░ Call рдХрд┐рдпрд╛ рдЬрд╛ рд╕рдХрддрд╛ рд╣реИред
C++ Programming рдореЗрдВ Functions рдХрд╛ рдЙрдкрдпреЛрдЧ Code Reusability, Modularity рддрдерд╛ Program Organization рдХреЛ рдмреЗрд╣рддрд░ рдмрдирд╛рдиреЗ рдХреЗ рд▓рд┐рдП рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред рдмрдбрд╝реЗ Software Projects рдореЗрдВ рд╣рдЬрд╛рд░реЛрдВ Functions рдХрд╛ рдЙрдкрдпреЛрдЧ рдХрд░рдХреЗ рд╣реА рд╕рдореНрдкреВрд░реНрдг Application рддреИрдпрд╛рд░ рдХреА рдЬрд╛рддреА рд╣реИред
Introduction
Function C++ Programming рдХрд╛ рд╕рдмрд╕реЗ рдорд╣рддреНрд╡рдкреВрд░реНрдг Concept рд╣реИред рдпрд╣ рдмрдбрд╝реЗ Program рдХреЛ рдЫреЛрдЯреЗ-рдЫреЛрдЯреЗ Logical Parts рдореЗрдВ рд╡рд┐рднрд╛рдЬрд┐рдд рдХрд░рддрд╛ рд╣реИ рдЬрд┐рд╕рд╕реЗ Program рдХреЛ рд╕рдордЭрдирд╛, Debug рдХрд░рдирд╛ рддрдерд╛ Maintain рдХрд░рдирд╛ рдЖрд╕рд╛рди рд╣реЛ рдЬрд╛рддрд╛ рд╣реИред
рдкреНрд░рддреНрдпреЗрдХ C++ Program рдХрд╛ Execution main() Function рд╕реЗ рдкреНрд░рд╛рд░рдореНрдн рд╣реЛрддрд╛ рд╣реИред рдЗрд╕рдХреЗ рдЕрддрд┐рд░рд┐рдХреНрдд Programmer рдЕрдкрдиреА рдЖрд╡рд╢реНрдпрдХрддрд╛ рдХреЗ рдЕрдиреБрд╕рд╛рд░ рдЕрдиреЗрдХ User Defined Functions рднреА рдмрдирд╛ рд╕рдХрддрд╛ рд╣реИред
Definition of Function
Function Program рдХрд╛ рдРрд╕рд╛ Block рд╣реЛрддрд╛ рд╣реИ рдЬреЛ рдХрд┐рд╕реА рд╡рд┐рд╢реЗрд╖ рдХрд╛рд░реНрдп рдХреЛ рдкреВрд░рд╛ рдХрд░рддрд╛ рд╣реИ рддрдерд╛ рдЖрд╡рд╢реНрдпрдХрддрд╛ рдкрдбрд╝рдиреЗ рдкрд░ Function Call рджреНрд╡рд╛рд░рд╛ Execute рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред Function рдЖрд╡рд╢реНрдпрдХрддрд╛ рдЕрдиреБрд╕рд╛рд░ Value Return рднреА рдХрд░ рд╕рдХрддрд╛ рд╣реИред
Need of Functions
- Code Reusability рдмрдврд╝рд╛рдиреЗ рдХреЗ рд▓рд┐рдПред
- Large Programs рдХреЛ рдЫреЛрдЯреЗ Modules рдореЗрдВ рд╡рд┐рднрд╛рдЬрд┐рдд рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдПред
- Debugging рдЖрд╕рд╛рди рдмрдирд╛рдиреЗ рдХреЗ рд▓рд┐рдПред
- Program Readability Improve рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдПред
- Development Time рдХрдо рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдПред
- Maintenance рдЖрд╕рд╛рди рдмрдирд╛рдиреЗ рдХреЗ рд▓рд┐рдПред
Characteristics of Functions
- Reusable Code Block.
- Performs Specific Task.
- Can Accept Parameters.
- Can Return Value.
- Improves Modularity.
- Supports Structured Programming.
Advantages of Functions
- Reduces Code Duplication.
- Improves Readability.
- Easy Testing and Debugging.
- Enhances Code Reusability.
- Simplifies Maintenance.
- Supports Team Development.
Limitations of Functions
- Function Call Overhead.
- Too Many Small Functions May Reduce Readability.
- Incorrect Parameter Passing Causes Errors.
- Recursive Functions May Consume More Memory.
Types of Functions
- Library Functions
- User Defined Functions
Library Functions
Library Functions рд╡реЗ Functions рд╣реЛрддреЗ рд╣реИрдВ рдЬреЛ рдкрд╣рд▓реЗ рд╕реЗ C++ Standard Library рдореЗрдВ рдЙрдкрд▓рдмреНрдз рд░рд╣рддреЗ рд╣реИрдВред Programmer рдХреЗрд╡рд▓ рдЙрдиреНрд╣реЗрдВ Call рдХрд░рддрд╛ рд╣реИред
pow()
strlen()
toupper()
рдЗрди Functions рдХреЛ рдЙрдкрдпреЛрдЧ рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП рд╕рдВрдмрдВрдзрд┐рдд Header File Include рдХрд░рдиреА рд╣реЛрддреА рд╣реИред
User Defined Functions
User Defined Functions рд╡реЗ Functions рд╣реЛрддреЗ рд╣реИрдВ рдЬрд┐рдиреНрд╣реЗрдВ Programmer рд╕реНрд╡рдпрдВ рдЕрдкрдиреА рдЖрд╡рд╢реНрдпрдХрддрд╛ рдХреЗ рдЕрдиреБрд╕рд╛рд░ рдмрдирд╛рддрд╛ рд╣реИред
Function Declaration (Prototype)
Function Declaration рдЕрдерд╡рд╛ Function Prototype Compiler рдХреЛ Function рдХрд╛ Name, Return Type рддрдерд╛ Parameters рдХреА рдЬрд╛рдирдХрд╛рд░реА рдкрд╣рд▓реЗ рд╕реЗ рдкреНрд░рджрд╛рди рдХрд░рддрд╛ рд╣реИред
Syntax
Function Definition
Function Definition рдореЗрдВ Function рдХрд╛ рд╡рд╛рд╕реНрддрд╡рд┐рдХ Code рд▓рд┐рдЦрд╛ рдЬрд╛рддрд╛ рд╣реИред рдЗрд╕реА рднрд╛рдЧ рдореЗрдВ Function рдХрд╛ рдХрд╛рд░реНрдп рдирд┐рд░реНрдзрд╛рд░рд┐рдд рд╣реЛрддрд╛ рд╣реИред
Function Calling
Program рдореЗрдВ Function Execute рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП рдЙрд╕реЗ Function Call рджреНрд╡рд╛рд░рд╛ рдмреБрд▓рд╛рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред
Function with No Arguments and No Return Value
рдЗрд╕ рдкреНрд░рдХрд╛рд░ рдХрд╛ Function рдХреЛрдИ Parameter рд╕реНрд╡реАрдХрд╛рд░ рдирд╣реАрдВ рдХрд░рддрд╛ рддрдерд╛ рдХреЛрдИ Value Return рднреА рдирд╣реАрдВ рдХрд░рддрд╛ред
Function with Arguments
рдЗрд╕ рдкреНрд░рдХрд╛рд░ рдХреЗ Function рдореЗрдВ Function Call рдХреЗ рд╕рдордп Values рднреЗрдЬреА рдЬрд╛рддреА рд╣реИрдВред рдЗрди Values рдХреЛ Arguments рддрдерд╛ Function Definition рдореЗрдВ рдкреНрд░рд╛рдкреНрдд Variables рдХреЛ Parameters рдХрд╣рд╛ рдЬрд╛рддрд╛ рд╣реИред
Function with Return Value
рдЗрд╕ рдкреНрд░рдХрд╛рд░ рдХрд╛ Function рдХрд┐рд╕реА Calculation рдЕрдерд╡рд╛ Processing рдХреЗ рдмрд╛рдж рдПрдХ Value Return рдХрд░рддрд╛ рд╣реИред Return рдХреА рдЧрдИ Value рдХреЛ Program рдХреЗ рдЕрдиреНрдп рднрд╛рдЧреЛрдВ рдореЗрдВ рдЙрдкрдпреЛрдЧ рдХрд┐рдпрд╛ рдЬрд╛ рд╕рдХрддрд╛ рд╣реИред
Function with Arguments and Return Value
рдпрд╣ C++ рдореЗрдВ рд╕рдмрд╕реЗ рдЕрдзрд┐рдХ рдЙрдкрдпреЛрдЧ рдХрд┐рдпрд╛ рдЬрд╛рдиреЗ рд╡рд╛рд▓рд╛ Function Type рд╣реИред рдЗрд╕рдореЗрдВ Function Parameters рд╕реНрд╡реАрдХрд╛рд░ рдХрд░рддрд╛ рд╣реИ рддрдерд╛ Processing рдХреЗ рдмрд╛рдж Result Return рдХрд░рддрд╛ рд╣реИред
Parameter Passing
Function Call рдХреЗ рд╕рдордп Arguments Function рддрдХ рдкрд╣реБрдБрдЪрд╛рдиреЗ рдХреА рдкреНрд░рдХреНрд░рд┐рдпрд╛ рдХреЛ Parameter Passing рдХрд╣рд╛ рдЬрд╛рддрд╛ рд╣реИред C++ рдореЗрдВ рдореБрдЦреНрдп рд░реВрдк рд╕реЗ Call by Value рддрдерд╛ Call by Reference рдХрд╛ рдЙрдкрдпреЛрдЧ рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред
Call by Value
Call by Value рдореЗрдВ Original Variable рдХреА Copy Function рдХреЛ рднреЗрдЬреА рдЬрд╛рддреА рд╣реИред Function рдХреЗ рдЕрдВрджрд░ рдХрд┐рдП рдЧрдП рдкрд░рд┐рд╡рд░реНрддрди Original Variable рдХреЛ рдкреНрд░рднрд╛рд╡рд┐рдд рдирд╣реАрдВ рдХрд░рддреЗред
рдпрд╣ Method рдЕрдзрд┐рдХ рд╕реБрд░рдХреНрд╖рд┐рдд рдорд╛рдирд╛ рдЬрд╛рддрд╛ рд╣реИ рдХреНрдпреЛрдВрдХрд┐ Original Data рд╕реБрд░рдХреНрд╖рд┐рдд рд░рд╣рддрд╛ рд╣реИред
Call by Reference (Introduction)
Call by Reference рдореЗрдВ Variable рдХрд╛ Reference рдЕрдерд╡рд╛ Address Function рдХреЛ рднреЗрдЬрд╛ рдЬрд╛рддрд╛ рд╣реИред Function рдХреЗ рдЕрдВрджрд░ рдХрд┐рдП рдЧрдП рдкрд░рд┐рд╡рд░реНрддрди Original Variable рдкрд░ рднреА рд▓рд╛рдЧреВ рд╣реЛ рдЬрд╛рддреЗ рд╣реИрдВред
рдпрд╣ Method Memory Efficient рд╣реЛрддрд╛ рд╣реИ рддрдерд╛ Large Objects рдХреЗ рд▓рд┐рдП рдЕрдзрд┐рдХ рдЙрдкрдпреЛрдЧреА рдорд╛рдирд╛ рдЬрд╛рддрд╛ рд╣реИред
Recursive Function (Introduction)
рдЬрдм рдХреЛрдИ Function рд╕реНрд╡рдпрдВ рдХреЛ рд╣реА Call рдХрд░рддрд╛ рд╣реИ, рддрдм рдЙрд╕реЗ Recursive Function рдХрд╣рд╛ рдЬрд╛рддрд╛ рд╣реИред рдкреНрд░рддреНрдпреЗрдХ Recursive Function рдореЗрдВ рдПрдХ Base Condition рд╣реЛрдирд╛ рдЖрд╡рд╢реНрдпрдХ рд╣реИ, рдЕрдиреНрдпрдерд╛ Program Infinite Recursion рдореЗрдВ рдЪрд▓рд╛ рдЬрд╛рдПрдЧрд╛ред
Scope of Variables
Variable рдХрд╛ Scope рдпрд╣ рдирд┐рд░реНрдзрд╛рд░рд┐рдд рдХрд░рддрд╛ рд╣реИ рдХрд┐ Program рдХреЗ рдХрд┐рд╕ рднрд╛рдЧ рд╕реЗ рдЙрд╕реЗ Access рдХрд┐рдпрд╛ рдЬрд╛ рд╕рдХрддрд╛ рд╣реИред
Local and Global Variables
| Local Variable | Global Variable |
|---|---|
| Declared Inside Function | Declared Outside Functions |
| Accessible Only Within Function | Accessible Throughout Program |
| Temporary Lifetime | Program Lifetime |
Common Errors
- Missing Function Prototype.
- Incorrect Number of Arguments.
- Wrong Return Type.
- Missing return Statement.
- Variable Scope Errors.
- Infinite Recursion.
Best Practices
- Create Small and Reusable Functions.
- Use Meaningful Function Names.
- Avoid Unnecessary Global Variables.
- Prefer Call by Reference for Large Objects.
- Write Proper Function Documentation.
- Keep Functions Focused on One Task.
Applications
- Banking Software.
- Student Management Systems.
- Operating Systems.
- Game Development.
- Scientific Computing.
- Artificial Intelligence.
- Database Applications.
- Embedded Systems.
- Enterprise Software Development.
Industrial Importance
Modern Software Development рдкреВрд░реА рддрд░рд╣ Functions рдкрд░ рдЖрдзрд╛рд░рд┐рдд рд╣реИред Large Enterprise Applications, Web Applications, Mobile Apps, Operating Systems рддрдерд╛ Artificial Intelligence Systems рдореЗрдВ рд╣рдЬрд╛рд░реЛрдВ Functions рдорд┐рд▓рдХрд░ Application рдХрд╛ рдирд┐рд░реНрдорд╛рдг рдХрд░рддреЗ рд╣реИрдВред Functions рдХреЗ рдХрд╛рд░рдг Code Modular, Reusable рддрдерд╛ Maintainable рдмрдирддрд╛ рд╣реИред
Viva Questions
- What is a Function?
- Why are Functions used?
- Differentiate Library Function and User Defined Function.
- What is Function Prototype?
- Explain Function Calling.
- What is Call by Value?
- What is Call by Reference?
- What is a Recursive Function?
- Differentiate Local and Global Variables.
- State the advantages of Functions.
Exam Oriented Important Questions
- Define Function with suitable examples.
- Explain the types of Functions in C++.
- Describe Function Declaration, Definition and Calling.
- Explain Call by Value and Call by Reference.
- Write a note on Recursive Functions.
- Differentiate Local and Global Variables.
- Discuss the applications of Functions.
- Explain the Industrial Importance of Functions.
Conclusion
Functions C++ Programming рдХрд╛ рд╕рдмрд╕реЗ рдорд╣рддреНрд╡рдкреВрд░реНрдг Concept рд╣реИрдВред рдпреЗ Program рдХреЛ рдЫреЛрдЯреЗ-рдЫреЛрдЯреЗ Modules рдореЗрдВ рд╡рд┐рднрд╛рдЬрд┐рдд рдХрд░рдХреЗ рдЙрд╕реЗ рдЕрдзрд┐рдХ Readable, Reusable рддрдерд╛ Maintainable рдмрдирд╛рддреЗ рд╣реИрдВред Library Functions, User Defined Functions, Parameter Passing, Call by Value, Call by Reference рддрдерд╛ Recursive Functions рдХреА рдЕрдЪреНрдЫреА рд╕рдордЭ рдкреНрд░рддреНрдпреЗрдХ Computer Engineering Student рдХреЗ рд▓рд┐рдП рдЕрддреНрдпрдВрдд рдЖрд╡рд╢реНрдпрдХ рд╣реИ рдХреНрдпреЛрдВрдХрд┐ рдпрд╣реА Concepts рдЖрдЧреЗ Object Oriented Programming рддрдерд╛ Advanced Software Development рдХреА рдиреАрдВрд╡ рд░рдЦрддреЗ рд╣реИрдВред
Related Articles
Manipulators in C++ Notes | endl, setw, setprecision, setfill | Basic Computer Engineering | RGPV BTech First Year
0...
Read More тЖТType Conversion in C++ Notes | Basic Computer Engineering | RGPV BTech First Year
Type Co...
Read More тЖТAssignment Operator in C++ Notes | Basic Computer Engineering | RGPV BTech First Year
Assignm...
Read More тЖТCopy Constructor in C++ Notes | Basic Computer Engineering | RGPV BTech First Year
Copy Co...
Read More тЖТthis Pointer in C++ Notes | Basic Computer Engineering | RGPV BTech First Year
this Po...
Read More тЖТ