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 рднреА рдХрд░ рд╕рдХрддрд╛ рд╣реИред

Function = Reusable Block of Code that Performs a Specific Task

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

  1. Library Functions
  2. User Defined Functions

Library Functions

Library Functions рд╡реЗ Functions рд╣реЛрддреЗ рд╣реИрдВ рдЬреЛ рдкрд╣рд▓реЗ рд╕реЗ C++ Standard Library рдореЗрдВ рдЙрдкрд▓рдмреНрдз рд░рд╣рддреЗ рд╣реИрдВред Programmer рдХреЗрд╡рд▓ рдЙрдиреНрд╣реЗрдВ Call рдХрд░рддрд╛ рд╣реИред

sqrt()
pow()
strlen()
toupper()

рдЗрди Functions рдХреЛ рдЙрдкрдпреЛрдЧ рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП рд╕рдВрдмрдВрдзрд┐рдд Header File Include рдХрд░рдиреА рд╣реЛрддреА рд╣реИред


User Defined Functions

User Defined Functions рд╡реЗ Functions рд╣реЛрддреЗ рд╣реИрдВ рдЬрд┐рдиреНрд╣реЗрдВ Programmer рд╕реНрд╡рдпрдВ рдЕрдкрдиреА рдЖрд╡рд╢реНрдпрдХрддрд╛ рдХреЗ рдЕрдиреБрд╕рд╛рд░ рдмрдирд╛рддрд╛ рд╣реИред

void display() {   cout << "Hello"; }

Function Declaration (Prototype)

Function Declaration рдЕрдерд╡рд╛ Function Prototype Compiler рдХреЛ Function рдХрд╛ Name, Return Type рддрдерд╛ Parameters рдХреА рдЬрд╛рдирдХрд╛рд░реА рдкрд╣рд▓реЗ рд╕реЗ рдкреНрд░рджрд╛рди рдХрд░рддрд╛ рд╣реИред

Syntax

return_type function_name(parameters);
int add(int, int);

Function Definition

Function Definition рдореЗрдВ Function рдХрд╛ рд╡рд╛рд╕реНрддрд╡рд┐рдХ Code рд▓рд┐рдЦрд╛ рдЬрд╛рддрд╛ рд╣реИред рдЗрд╕реА рднрд╛рдЧ рдореЗрдВ Function рдХрд╛ рдХрд╛рд░реНрдп рдирд┐рд░реНрдзрд╛рд░рд┐рдд рд╣реЛрддрд╛ рд╣реИред

int add(int a, int b) {   return a+b; }

Function Calling

Program рдореЗрдВ Function Execute рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП рдЙрд╕реЗ Function Call рджреНрд╡рд╛рд░рд╛ рдмреБрд▓рд╛рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред

int result = add(10,20);

Function with No Arguments and No Return Value

рдЗрд╕ рдкреНрд░рдХрд╛рд░ рдХрд╛ Function рдХреЛрдИ Parameter рд╕реНрд╡реАрдХрд╛рд░ рдирд╣реАрдВ рдХрд░рддрд╛ рддрдерд╛ рдХреЛрдИ Value Return рднреА рдирд╣реАрдВ рдХрд░рддрд╛ред

void display() {   cout << "Welcome"; }

Function with Arguments

рдЗрд╕ рдкреНрд░рдХрд╛рд░ рдХреЗ Function рдореЗрдВ Function Call рдХреЗ рд╕рдордп Values рднреЗрдЬреА рдЬрд╛рддреА рд╣реИрдВред рдЗрди Values рдХреЛ Arguments рддрдерд╛ Function Definition рдореЗрдВ рдкреНрд░рд╛рдкреНрдд Variables рдХреЛ Parameters рдХрд╣рд╛ рдЬрд╛рддрд╛ рд╣реИред

void display(string name) {   cout << name; } display("RGPV");

Function with Return Value

рдЗрд╕ рдкреНрд░рдХрд╛рд░ рдХрд╛ Function рдХрд┐рд╕реА Calculation рдЕрдерд╡рд╛ Processing рдХреЗ рдмрд╛рдж рдПрдХ Value Return рдХрд░рддрд╛ рд╣реИред Return рдХреА рдЧрдИ Value рдХреЛ Program рдХреЗ рдЕрдиреНрдп рднрд╛рдЧреЛрдВ рдореЗрдВ рдЙрдкрдпреЛрдЧ рдХрд┐рдпрд╛ рдЬрд╛ рд╕рдХрддрд╛ рд╣реИред

int square(int n) {   return n*n; }

Function with Arguments and Return Value

рдпрд╣ C++ рдореЗрдВ рд╕рдмрд╕реЗ рдЕрдзрд┐рдХ рдЙрдкрдпреЛрдЧ рдХрд┐рдпрд╛ рдЬрд╛рдиреЗ рд╡рд╛рд▓рд╛ Function Type рд╣реИред рдЗрд╕рдореЗрдВ Function Parameters рд╕реНрд╡реАрдХрд╛рд░ рдХрд░рддрд╛ рд╣реИ рддрдерд╛ Processing рдХреЗ рдмрд╛рдж Result Return рдХрд░рддрд╛ рд╣реИред

int add(int a,int b) {   return a+b; } int result = add(15,25);

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 рдХреЛ рдкреНрд░рднрд╛рд╡рд┐рдд рдирд╣реАрдВ рдХрд░рддреЗред

void change(int x) {   x = 100; }

рдпрд╣ Method рдЕрдзрд┐рдХ рд╕реБрд░рдХреНрд╖рд┐рдд рдорд╛рдирд╛ рдЬрд╛рддрд╛ рд╣реИ рдХреНрдпреЛрдВрдХрд┐ Original Data рд╕реБрд░рдХреНрд╖рд┐рдд рд░рд╣рддрд╛ рд╣реИред


Call by Reference (Introduction)

Call by Reference рдореЗрдВ Variable рдХрд╛ Reference рдЕрдерд╡рд╛ Address Function рдХреЛ рднреЗрдЬрд╛ рдЬрд╛рддрд╛ рд╣реИред Function рдХреЗ рдЕрдВрджрд░ рдХрд┐рдП рдЧрдП рдкрд░рд┐рд╡рд░реНрддрди Original Variable рдкрд░ рднреА рд▓рд╛рдЧреВ рд╣реЛ рдЬрд╛рддреЗ рд╣реИрдВред

void change(int &x) {   x = 100; }

рдпрд╣ Method Memory Efficient рд╣реЛрддрд╛ рд╣реИ рддрдерд╛ Large Objects рдХреЗ рд▓рд┐рдП рдЕрдзрд┐рдХ рдЙрдкрдпреЛрдЧреА рдорд╛рдирд╛ рдЬрд╛рддрд╛ рд╣реИред


Recursive Function (Introduction)

рдЬрдм рдХреЛрдИ Function рд╕реНрд╡рдпрдВ рдХреЛ рд╣реА Call рдХрд░рддрд╛ рд╣реИ, рддрдм рдЙрд╕реЗ Recursive Function рдХрд╣рд╛ рдЬрд╛рддрд╛ рд╣реИред рдкреНрд░рддреНрдпреЗрдХ Recursive Function рдореЗрдВ рдПрдХ Base Condition рд╣реЛрдирд╛ рдЖрд╡рд╢реНрдпрдХ рд╣реИ, рдЕрдиреНрдпрдерд╛ Program Infinite Recursion рдореЗрдВ рдЪрд▓рд╛ рдЬрд╛рдПрдЧрд╛ред

int fact(int n) {   if(n==1)     return 1;   return n * fact(n-1); }

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

  1. What is a Function?
  2. Why are Functions used?
  3. Differentiate Library Function and User Defined Function.
  4. What is Function Prototype?
  5. Explain Function Calling.
  6. What is Call by Value?
  7. What is Call by Reference?
  8. What is a Recursive Function?
  9. Differentiate Local and Global Variables.
  10. State the advantages of Functions.

Exam Oriented Important Questions

  1. Define Function with suitable examples.
  2. Explain the types of Functions in C++.
  3. Describe Function Declaration, Definition and Calling.
  4. Explain Call by Value and Call by Reference.
  5. Write a note on Recursive Functions.
  6. Differentiate Local and Global Variables.
  7. Discuss the applications of Functions.
  8. 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 тЖТ