Function
A function in C is a set of statements that when called perform some specific task. It is the basic building block of a C program that provides modularity and code reusability. The programming statements of a function are enclosed within { } braces, having certain meanings and performing certain operations. They are also called subroutines or procedures in other languages.
Function are Sub-program i.e the part of a Program which perform a specific type of operation. Remember that it is a not complete program whereas it is a part of the program.
Function are broadly divided into two type:
1. Library function/Built-in function
2. User-defined function.
Benefits/ Advantages of function.
Code Reusability: Functions allow you to write a piece of code once and reuse it multiple times throughout your program. This avoids duplication and makes the code easier to maintain.
Modularity: Functions help break down complex problems into smaller, more manageable parts. Each function performs a specific task, making it easier to develop and test individual components of the program.
Improved Readability: By organizing code into functions, you make your program more readable and understandable. Each function has a specific name and purpose, which clarifies what the code does and how it fits into the overall program.
Easier Maintenance: Functions encapsulate code, which means changes to one function don’t necessarily affect other parts of the program. This modular approach simplifies updating or debugging specific parts of your code.
Code Organization: Functions help in organizing code logically. They separate different functionalities into distinct sections, making the program easier to navigate and manage.
1. Library function/Built-in function:-
In C programming, library functions are pre-written functions that are provided by the C Standard Library. They perform common tasks such as input/output operations, string manipulation, mathematical computations, and more. Using these functions can simplify your code and reduce the need to write these common operations from scratch.
Library functions / Built-in function are those which are already defined means whose functionality is already defined and we need to just Call then in Order to use them. they will perform the functionality Only which is called.
Categories of Library function:
1. Standard Input/Output Functions:
printf(): Prints formatted output to the console.
scanf(): Reads formatted input from the console.
putchar(): Writes a single character to the console.
getchar(): Reads a single character from the console.
2. String Handling Functions:
strlen(): Returns the length of a string.
strcpy(): Copies one string to another.
strcat(): Concatenates (joins) two strings.
strcmp(): Compares two strings.
3. Mathematical Functions:
sqrt(): Calculates the square root of a number.
pow(): Raises a number to a power.
fabs(): Returns the absolute value of a floating-point number.
4. Memory Management Functions:
malloc(): Allocates a block of memory.
free(): Deallocates a previously allocated block of memory.
calloc(): Allocates memory for an array and initializes it to zero.
realloc(): Changes the size of a previously allocated block of memory.
5. File I/O Functions:
fopen(): Opens a file.
fclose(): Closes an open file.
fread(): Reads data from a file.
fwrite(): Writes data to a file.
fgets(): Reads a string from a file.
fprintf(): Writes formatted output to a file.
2. User-defined Function:-
In C programming, a user-defined function is a function created by the programmer to perform a specific task. Unlike library functions that come with the C standard library, user-defined functions are written in your program to implement specific functionality or to modularize code.
How to define User-defined function
- Function Declaration
- Function Definition
- Function Call
ⅰ) Function Declaration (Prototype): This declares the function's return type, name, and parameters, allowing the compiler to understand how to handle calls to the function before its actual definition appears in the code.
ii) Function Definition: This provides the actual implementation of the function, including the body where the logic is written.
iii) Function Call: This is where you use the function in your code to execute its logic.
Benefits of User-Defined Functions:
Modularity: Breaking down a program into functions makes it easier to manage and understand.
Reusability: Functions can be reused in different parts of the program or even in different programs.
Maintainability: Changes to a function’s logic can be made in one place without affecting the rest of the code.
Abstraction: Functions allow you to encapsulate complex logic and provide a simple interface for using it.
A function may be written using for ways.
1. No argument no return
2.with argument no return
3.No argument with return
4. with argument with return
1. No argument no return:
In C programming, a function that does not take any arguments and does not return a value is called a no argument, no return value function. These functions are often used to perform tasks that do not require input from the calling function and do not need to send any data back.
2.with argument no return
3.No argument with return
In C programming, a function that does not take any arguments but returns a value is called a no argument with return value function. These functions are useful when you need to perform an operation that does not require input from the calling function but needs to send a result back.
4. with argument with return
In C programming, a function that takes arguments and returns a value is called a function with arguments and return value. These functions are useful when you need to pass data to the function and also get a result back.
Call by Value and Call by Reference:
In C programming, call by value and call by reference are two ways to pass data to a function:
- Call by Value
- Call by Reference
1. Call by Value:
Call by Value is a method of passing arguments to a function in which a copy of the actual parameter's value is made. When the function is called, this copy is used within the function, meaning that any changes made to the parameter do not affect the original variable outside the function.
2. Call by Reference:
Call by Reference is a method of passing arguments to a function in C where the address of the actual parameter is passed. This allows the function to access and modify the original variable directly.