Functions in C Programming Notes | Basic Computer Engineering | RGPV BTech First Year

Functions in C Programming Notes | Basic Computer Engineering | RGPV BTech First Year


Functions in C Programming

Function C Programming Language рдХрд╛ рдПрдХ рдорд╣рддреНрд╡рдкреВрд░реНрдг Concept рд╣реИ рдЬрд┐рд╕рдХреЗ рдорд╛рдзреНрдпрдо рд╕реЗ рдХрд┐рд╕реА рдмрдбрд╝реЗ Program рдХреЛ рдЫреЛрдЯреЗ-рдЫреЛрдЯреЗ Modules рдореЗрдВ рд╡рд┐рднрд╛рдЬрд┐рдд рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред рдкреНрд░рддреНрдпреЗрдХ Function рдХрд┐рд╕реА рд╡рд┐рд╢реЗрд╖ рдХрд╛рд░реНрдп (Specific Task) рдХреЛ рдкреВрд░рд╛ рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП рдмрдирд╛рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред Functions рдХрд╛ рдЙрдкрдпреЛрдЧ рдХрд░рдиреЗ рд╕реЗ Program рдЕрдзрд┐рдХ рд╡реНрдпрд╡рд╕реНрдерд┐рдд (Organized), Reusable рддрдерд╛ Maintainable рдмрдирддрд╛ рд╣реИред

Software Development, Operating Systems, Embedded Systems, Artificial Intelligence, Banking Software рддрдерд╛ Enterprise Applications рдореЗрдВ Functions рдХрд╛ рд╡реНрдпрд╛рдкрдХ рдЙрдкрдпреЛрдЧ рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред Modular Programming рдХреА рдЕрд╡рдзрд╛рд░рдгрд╛ Functions рдкрд░ рд╣реА рдЖрдзрд╛рд░рд┐рдд рд╣реЛрддреА рд╣реИред


Introduction to Functions

Function Statements рдХрд╛ рдРрд╕рд╛ рд╕рдореВрд╣ (Block of Statements) рд╣реЛрддрд╛ рд╣реИ рдЬреЛ рдХрд┐рд╕реА рд╡рд┐рд╢реЗрд╖ рдХрд╛рд░реНрдп рдХреЛ рд╕рдореНрдкрдиреНрди рдХрд░рддрд╛ рд╣реИред рдХрд┐рд╕реА Function рдХреЛ Program рдореЗрдВ рдЖрд╡рд╢реНрдпрдХрддрд╛ рдЕрдиреБрд╕рд╛рд░ рдХрдИ рдмрд╛рд░ Call рдХрд┐рдпрд╛ рдЬрд╛ рд╕рдХрддрд╛ рд╣реИ рдЬрд┐рд╕рд╕реЗ Code Duplication рдХрдо рд╣реЛрддреА рд╣реИ рддрдерд╛ Program рдЕрдзрд┐рдХ Efficient рдмрдирддрд╛ рд╣реИред

C Program рдореЗрдВ Execution рд╣рдореЗрд╢рд╛ main() Function рд╕реЗ рдкреНрд░рд╛рд░рдореНрдн рд╣реЛрддрд╛ рд╣реИред рдЕрдиреНрдп Functions рдХреЛ рдЖрд╡рд╢реНрдпрдХрддрд╛ рдЕрдиреБрд╕рд╛рд░ main() рдЕрдерд╡рд╛ рдХрд┐рд╕реА рдЕрдиреНрдп Function рджреНрд╡рд╛рд░рд╛ Call рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред


Definition of Function

Function C Program рдХрд╛ рдРрд╕рд╛ рд╕реНрд╡рддрдВрддреНрд░ Block рд╣реЛрддрд╛ рд╣реИ рдЬреЛ рдХрд┐рд╕реА рд╡рд┐рд╢реЗрд╖ рдХрд╛рд░реНрдп рдХреЛ рдкреВрд░рд╛ рдХрд░рддрд╛ рд╣реИ рддрдерд╛ рдЖрд╡рд╢реНрдпрдХрддрд╛ рдЕрдиреБрд╕рд╛рд░ Value Return рднреА рдХрд░ рд╕рдХрддрд╛ рд╣реИред

Function = Reusable Block of Code that Performs a Specific Task

Need of Functions

  • Large Programs рдХреЛ рдЫреЛрдЯреЗ Modules рдореЗрдВ рд╡рд┐рднрд╛рдЬрд┐рдд рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдПред
  • Code Reusability рдмрдврд╝рд╛рдиреЗ рдХреЗ рд▓рд┐рдПред
  • Program Readability Improve рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдПред
  • Easy Testing рддрдерд╛ Debugging рдХреЗ рд▓рд┐рдПред
  • Maintenance рд╕рд░рд▓ рдмрдирд╛рдиреЗ рдХреЗ рд▓рд┐рдПред
  • Structured Programming рдХреЗ рд▓рд┐рдПред

Characteristics of Functions

  • Reusable Code.
  • Modular Programming.
  • Independent Block.
  • Supports Recursion.
  • Easy Maintenance.
  • Improves Readability.

Advantages of Functions

  • Reduces Code Duplication.
  • Improves Program Structure.
  • Easy Debugging.
  • Easy Maintenance.
  • Reusable Modules.
  • Supports Team Development.

Limitations of Functions

  • Improper Design Reduces Performance.
  • Excessive Function Calls Increase Overhead.
  • Recursive Functions Consume More Memory.
  • Poor Naming Reduces Readability.

Types of Functions

C Programming рдореЗрдВ рдореБрдЦреНрдп рд░реВрдк рд╕реЗ рджреЛ рдкреНрд░рдХрд╛рд░ рдХреЗ Functions рд╣реЛрддреЗ рд╣реИрдВред

  1. Library Functions
  2. User Defined Functions

Library Functions

Library Functions рд╡реЗ Functions рд╣реЛрддреЗ рд╣реИрдВ рдЬреЛ рдкрд╣рд▓реЗ рд╕реЗ C Standard Library рдореЗрдВ рдЙрдкрд▓рдмреНрдз рд╣реЛрддреЗ рд╣реИрдВред Programmer рдЗрдиреНрд╣реЗрдВ рд╕реАрдзреЗ Program рдореЗрдВ рдЙрдкрдпреЛрдЧ рдХрд░ рд╕рдХрддрд╛ рд╣реИред рдЙрджрд╛рд╣рд░рдг рдХреЗ рд▓рд┐рдП printf(), scanf(), sqrt(), strlen(), pow() рдЖрджрд┐ред

Function Header File Purpose
printf() stdio.h Display Output
scanf() stdio.h Read Input
sqrt() math.h Square Root
strlen() string.h String Length

User Defined Functions

User Defined Functions рд╡реЗ Functions рд╣реЛрддреЗ рд╣реИрдВ рдЬрд┐рдиреНрд╣реЗрдВ Programmer рдЕрдкрдиреА рдЖрд╡рд╢реНрдпрдХрддрд╛ рдХреЗ рдЕрдиреБрд╕рд╛рд░ рд╕реНрд╡рдпрдВ рд╡рд┐рдХрд╕рд┐рдд рдХрд░рддрд╛ рд╣реИред рдЗрдирдХрд╛ рдЙрдкрдпреЛрдЧ рдХрд┐рд╕реА рд╡рд┐рд╢реЗрд╖ Task рдХреЛ рдмрд╛рд░-рдмрд╛рд░ Execute рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред

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

Function Declaration (Prototype)

Function Prototype Compiler рдХреЛ Function рдХреЗ Name, Return Type рддрдерд╛ Parameters рдХреА рдЬрд╛рдирдХрд╛рд░реА рдкрд╣рд▓реЗ рд╕реЗ рджреЗрддрд╛ рд╣реИред Function Definition рд╕реЗ рдкрд╣рд▓реЗ Prototype рд▓рд┐рдЦрдирд╛ рдЕрдЪреНрдЫреА Programming Practice рдорд╛рдирд╛ рдЬрд╛рддрд╛ рд╣реИред

int add(int,int);

Function Definition

Function Definition рд╡рд╣ рднрд╛рдЧ рд╣реЛрддрд╛ рд╣реИ рдЬрд┐рд╕рдореЗрдВ Function рдХреА рд╡рд╛рд╕реНрддрд╡рд┐рдХ Logic рд▓рд┐рдЦреА рдЬрд╛рддреА рд╣реИред рдЗрд╕реА рднрд╛рдЧ рдореЗрдВ рд╕рднреА Statements Execute рд╣реЛрддреА рд╣реИрдВред

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

Function Call

рдЬрдм рдХрд┐рд╕реА Function рдХреЛ Execute рдХрд░рдирд╛ рд╣реЛрддрд╛ рд╣реИ рддрдм рдЙрд╕реЗ рдЙрд╕рдХреЗ Name рдХреЗ рдорд╛рдзреНрдпрдо рд╕реЗ Call рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред Function Call рдХреЗ рд╕рдордп рдЖрд╡рд╢реНрдпрдХ Arguments Function рдХреЛ рднреЗрдЬреЗ рдЬрд╛рддреЗ рд╣реИрдВред

int result = add(10,20);

Function Arguments and Parameters

Arguments рд╡реЗ Values рд╣реЛрддреА рд╣реИрдВ рдЬреЛ Function Call рдХреЗ рд╕рдордп Function рдХреЛ рднреЗрдЬреА рдЬрд╛рддреА рд╣реИрдВ, рдЬрдмрдХрд┐ Parameters рд╡реЗ Variables рд╣реЛрддреЗ рд╣реИрдВ рдЬреЛ Function Definition рдореЗрдВ рдЗрди Values рдХреЛ Receive рдХрд░рддреЗ рд╣реИрдВред

int sum(int a, int b) { return a+b; } sum(10,20);

Call by Value

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

void change(int x) { x = x + 10; }

Call by Reference (Introduction)

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

void swap(int *a,int *b) { int temp=*a; *a=*b; *b=temp; }

Return Statement

return Statement Function рдХрд╛ Execution рд╕рдорд╛рдкреНрдд рдХрд░рддрд╛ рд╣реИ рддрдерд╛ рдЖрд╡рд╢реНрдпрдХрддрд╛ рд╣реЛрдиреЗ рдкрд░ рдХрд┐рд╕реА Value рдХреЛ Calling Function рдХреЗ рдкрд╛рд╕ рд╡рд╛рдкрд╕ рднреЗрдЬрддрд╛ рд╣реИред рдпрджрд┐ Function рдХрд╛ Return Type void рд╣реЛ рддреЛ рдХреЛрдИ Value Return рдирд╣реАрдВ рдХреА рдЬрд╛рддреАред

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

Recursive Functions (Introduction)

Recursive Function рдРрд╕рд╛ Function рд╣реЛрддрд╛ рд╣реИ рдЬреЛ рд╕реНрд╡рдпрдВ (Itself) рдХреЛ Call рдХрд░рддрд╛ рд╣реИред Recursion рдХрд╛ рдЙрдкрдпреЛрдЧ Factorial, Fibonacci Series, Tree Traversal рддрдерд╛ Divide and Conquer Algorithms рдореЗрдВ рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред

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

Difference between Library Function and User Defined Function

Library Function User Defined Function
Predefined Function. Created by Programmer.
Available in Standard Library. Defined in User Program.
Ready to Use. Needs Definition.
Example: printf(), scanf(). Example: add(), factorial().

Applications of Functions

  • Modular Programming.
  • Code Reusability.
  • Software Development.
  • Operating Systems.
  • Embedded Systems.
  • Artificial Intelligence.
  • Database Applications.
  • Scientific Computing.
  • Networking Software.
  • Enterprise Applications.

Common Errors

  • Missing Function Prototype.
  • Incorrect Return Type.
  • Wrong Number of Arguments.
  • Parameter Type Mismatch.
  • Missing Return Statement.
  • Infinite Recursion.

Best Practices

  • Use Meaningful Function Names.
  • Keep Functions Small and Focused.
  • Write Function Prototypes.
  • Reuse Existing Functions.
  • Avoid Unnecessary Global Variables.
  • Document Every Function Properly.

Industrial Importance

Functions рдЖрдзреБрдирд┐рдХ Software Engineering рдХрд╛ рдЖрдзрд╛рд░ рд╣реИрдВред Large Scale Software Projects, Operating Systems, Banking Software, Embedded Systems, Artificial Intelligence, Cloud Computing рддрдерд╛ Enterprise Applications рдореЗрдВ Modular Design Functions рдХреА рд╕рд╣рд╛рдпрддрд╛ рд╕реЗ рд╣реА рддреИрдпрд╛рд░ рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред Functions Code Reusability рдмрдврд╝рд╛рддреЗ рд╣реИрдВ, Maintenance рдЖрд╕рд╛рди рдмрдирд╛рддреЗ рд╣реИрдВ рддрдерд╛ Team Based Development рдХреЛ рдЕрдзрд┐рдХ рдкреНрд░рднрд╛рд╡реА рдмрдирд╛рддреЗ рд╣реИрдВред


Viva Questions

  1. What is a Function in C?
  2. Why are Functions required?
  3. Differentiate Library Function and User Defined Function.
  4. What is Function Prototype?
  5. Explain Function Definition.
  6. What is Function Call?
  7. Differentiate Arguments and Parameters.
  8. Explain Call by Value.
  9. What is Call by Reference?
  10. What is a Recursive Function?

Exam Oriented Important Questions

  1. Define Function with suitable examples.
  2. Explain the need and characteristics of Functions.
  3. Describe different types of Functions in C.
  4. Explain Library Functions and User Defined Functions.
  5. Discuss Function Prototype, Definition and Function Call.
  6. Explain Call by Value and Call by Reference.
  7. Describe Recursive Functions with suitable examples.
  8. Write short notes on Return Statement and Function Arguments.

Conclusion

Functions C Programming рдХреА рд╕рдмрд╕реЗ рдорд╣рддреНрд╡рдкреВрд░реНрдг рдЕрд╡рдзрд╛рд░рдгрд╛рдУрдВ рдореЗрдВ рд╕реЗ рдПрдХ рд╣реИрдВред рдЗрдирдХреЗ рдорд╛рдзреНрдпрдо рд╕реЗ рдмрдбрд╝реЗ Programs рдХреЛ рдЫреЛрдЯреЗ-рдЫреЛрдЯреЗ Modules рдореЗрдВ рд╡рд┐рднрд╛рдЬрд┐рдд рдХрд░рдХреЗ рдЕрдзрд┐рдХ рд╡реНрдпрд╡рд╕реНрдерд┐рдд, Reusable рддрдерд╛ Maintainable рдмрдирд╛рдпрд╛ рдЬрд╛ рд╕рдХрддрд╛ рд╣реИред Functions рдХреА рдЕрдЪреНрдЫреА рд╕рдордЭ рд╡рд┐рджреНрдпрд╛рд░реНрдерд┐рдпреЛрдВ рдХреЛ Structured Programming, Data Structures, Algorithms рддрдерд╛ Advanced Software Development Concepts рд╕реАрдЦрдиреЗ рдореЗрдВ рдордЬрдмреВрдд рдЖрдзрд╛рд░ рдкреНрд░рджрд╛рди рдХрд░рддреА рд╣реИред

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 тЖТ