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 рднреА рдХрд░ рд╕рдХрддрд╛ рд╣реИред
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 рд╣реЛрддреЗ рд╣реИрдВред
- Library Functions
- 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 рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред
Function Declaration (Prototype)
Function Prototype Compiler рдХреЛ Function рдХреЗ Name, Return Type рддрдерд╛ Parameters рдХреА рдЬрд╛рдирдХрд╛рд░реА рдкрд╣рд▓реЗ рд╕реЗ рджреЗрддрд╛ рд╣реИред Function Definition рд╕реЗ рдкрд╣рд▓реЗ Prototype рд▓рд┐рдЦрдирд╛ рдЕрдЪреНрдЫреА Programming Practice рдорд╛рдирд╛ рдЬрд╛рддрд╛ рд╣реИред
Function Definition
Function Definition рд╡рд╣ рднрд╛рдЧ рд╣реЛрддрд╛ рд╣реИ рдЬрд┐рд╕рдореЗрдВ Function рдХреА рд╡рд╛рд╕реНрддрд╡рд┐рдХ Logic рд▓рд┐рдЦреА рдЬрд╛рддреА рд╣реИред рдЗрд╕реА рднрд╛рдЧ рдореЗрдВ рд╕рднреА Statements Execute рд╣реЛрддреА рд╣реИрдВред
Function Call
рдЬрдм рдХрд┐рд╕реА Function рдХреЛ Execute рдХрд░рдирд╛ рд╣реЛрддрд╛ рд╣реИ рддрдм рдЙрд╕реЗ рдЙрд╕рдХреЗ Name рдХреЗ рдорд╛рдзреНрдпрдо рд╕реЗ Call рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред Function Call рдХреЗ рд╕рдордп рдЖрд╡рд╢реНрдпрдХ Arguments Function рдХреЛ рднреЗрдЬреЗ рдЬрд╛рддреЗ рд╣реИрдВред
Function Arguments and Parameters
Arguments рд╡реЗ Values рд╣реЛрддреА рд╣реИрдВ рдЬреЛ Function Call рдХреЗ рд╕рдордп Function рдХреЛ рднреЗрдЬреА рдЬрд╛рддреА рд╣реИрдВ, рдЬрдмрдХрд┐ Parameters рд╡реЗ Variables рд╣реЛрддреЗ рд╣реИрдВ рдЬреЛ Function Definition рдореЗрдВ рдЗрди Values рдХреЛ Receive рдХрд░рддреЗ рд╣реИрдВред
Call by Value
Call by Value рдореЗрдВ Function рдХреЛ Variable рдХреА Original Value рдХреА Copy рднреЗрдЬреА рдЬрд╛рддреА рд╣реИред Function рдХреЗ рдЕрдВрджрд░ рдХрд┐рдП рдЧрдП рдкрд░рд┐рд╡рд░реНрддрди Original Variable рдХреЛ рдкреНрд░рднрд╛рд╡рд┐рдд рдирд╣реАрдВ рдХрд░рддреЗред
Call by Reference (Introduction)
Call by Reference рдореЗрдВ Function рдХреЛ Variable рдХрд╛ Address рднреЗрдЬрд╛ рдЬрд╛рддрд╛ рд╣реИред рдЗрд╕рд▓рд┐рдП Function рдХреЗ рдЕрдВрджрд░ рдХрд┐рдП рдЧрдП рдкрд░рд┐рд╡рд░реНрддрди рд╕реАрдзреЗ Original Variable рдкрд░ рдкреНрд░рднрд╛рд╡ рдбрд╛рд▓рддреЗ рд╣реИрдВред C Language рдореЗрдВ рдпрд╣ Pointer Variables рдХреА рд╕рд╣рд╛рдпрддрд╛ рд╕реЗ рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред
Return Statement
return Statement Function рдХрд╛ Execution рд╕рдорд╛рдкреНрдд рдХрд░рддрд╛ рд╣реИ рддрдерд╛ рдЖрд╡рд╢реНрдпрдХрддрд╛ рд╣реЛрдиреЗ рдкрд░ рдХрд┐рд╕реА Value рдХреЛ Calling Function рдХреЗ рдкрд╛рд╕ рд╡рд╛рдкрд╕ рднреЗрдЬрддрд╛ рд╣реИред рдпрджрд┐ Function рдХрд╛ Return Type void рд╣реЛ рддреЛ рдХреЛрдИ Value Return рдирд╣реАрдВ рдХреА рдЬрд╛рддреАред
Recursive Functions (Introduction)
Recursive Function рдРрд╕рд╛ Function рд╣реЛрддрд╛ рд╣реИ рдЬреЛ рд╕реНрд╡рдпрдВ (Itself) рдХреЛ Call рдХрд░рддрд╛ рд╣реИред Recursion рдХрд╛ рдЙрдкрдпреЛрдЧ Factorial, Fibonacci Series, Tree Traversal рддрдерд╛ Divide and Conquer Algorithms рдореЗрдВ рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред
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
- What is a Function in C?
- Why are Functions required?
- Differentiate Library Function and User Defined Function.
- What is Function Prototype?
- Explain Function Definition.
- What is Function Call?
- Differentiate Arguments and Parameters.
- Explain Call by Value.
- What is Call by Reference?
- What is a Recursive Function?
Exam Oriented Important Questions
- Define Function with suitable examples.
- Explain the need and characteristics of Functions.
- Describe different types of Functions in C.
- Explain Library Functions and User Defined Functions.
- Discuss Function Prototype, Definition and Function Call.
- Explain Call by Value and Call by Reference.
- Describe Recursive Functions with suitable examples.
- 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 тЖТ