Functions improve readability, reduce code duplication, and make debugging easier. Every C program uses functions, including the main() function which acts as the starting point of execution.
Table of Contents
What is a Function in C?
A function in C is a block of code that performs a specific task. It is executed only when it is called from another part of the program.A function usually contains:
- Function declaration
- Function definition
- Function call
Syntax Explanation:return_type function_name(parameters)
{
// code statements
}
- return_type: Defines the type of value returned by the function
- function_name: Name used to call the function
- parameters: Input values passed to the function
- return: Sends value back to the calling function
// C program to implement functions
#include stdio.h
void greet()
{
printf("Welcome to C Programming");
}
int main()
{
greet();
return 0;
}
Output:
Explanation:Welcome to C Programming
In this program, greet() is a function. When it is called inside main(), the message is displayed on the screen.
Library Functions in C
Library functions are predefined functions already available in the C standard library. These functions are created by the developers of the C language and can be directly used in programs by including the required header file.Library functions save time because programmers do not need to write common operations manually.
Features of Library Functions
- Predefined Functions: Library functions are already written and tested by C developers.
- Easy to Use: These functions can be directly used by including the required header file.
- Reduce Programming Effort: Programmers do not need to write code for common tasks like input, output, or string handling.
- Reliable and Optimized: Library functions are optimized for better performance and accuracy.
- Improve Code Readability: Using standard functions makes programs easier to understand.
| Function | Definition | Header File |
|---|---|---|
| printf() | Displays output on the screen | stdio.h |
| scanf() | Takes input from the user | stdio.h |
| strlen() | Calculates the length of the string | string.h |
| strcpy() | Copies one string into another | string.h |
| strcat() | Joins two strings | string.h |
| sqrt() | Finds square root of a number | math.h |
| pow() | Calculates the power of a number | math.h |
Example:
// C program to implement
// library function
#include stdio.h
#include string.h
int main()
{
char name[] = "Kashvi";
printf("Length of String = %lu", strlen(name));
return 0;
}
Output:
Explanation:Length of String = 6
The strlen() library function calculates the number of characters present in the string.
User Defined Functions in C
User defined functions are functions created by programmers according to program requirements. These functions help perform customized tasks.Instead of writing the same code repeatedly, a user defined function can be called whenever needed.
Features of User Defined Functions
- Reusability: The same function can be used multiple times in a program.
- Easy Maintenance: Changes made inside the function automatically apply everywhere the function is called.
- Better Program Structure: Programs become more organized and modular.
- Simplifies Debugging: Errors can be identified and corrected easily.
- Reduces Code Duplication: Repeated statements are avoided using functions.
Steps to Use User Defined Functions
- Function Declaration: Declares the function name, return type, and parameters before use.
- Function Definition: Contains the actual code that performs the task.
- Function Call: Executes the function whenever it is needed in the program.
// C program to implement
// user-defined function
#include stdio.h
int add(int a, int b)
{
return a + b;
}
int main()
{
int result;
result = add(10, 20);
printf("Sum = %d", result);
return 0;
}
Output:
Explanation:Sum = 30
The add() function accepts two numbers as arguments and returns their sum.
Types of User Defined Functions in C
User defined functions are classified into four types based on arguments and return values.1. Function Without Arguments and Without Return Value
This function does not receive arguments and does not return any value.
// C program to implement
// user-defined function
#include stdio.h
void display()
{
printf("Hello User");
}
int main()
{
display();
return 0;
}
Output:
2. Function With Arguments and Without Return ValueHello User
This function receives input values but does not return a result.
// C program to implement
// user-defined function
#include stdio.h
void multiply(int x, int y)
{
printf("Product = %d", x * y);
}
int main()
{
multiply(5, 4);
return 0;
}
Output:
3. Function Without Arguments and With Return ValueProduct = 20
This function does not receive parameters but returns a value.
// C program to implement
// user-defined function
#include stdio.h
int getNumber()
{
return 100;
}
int main()
{
int value;
value = getNumber();
printf("%d", value);
return 0;
}
Output:
4. Function With Arguments and With Return Value100
This function receives arguments and returns a value.
// C program to implement
// user-defined function
#include stdio.h
int square(int num)
{
return num * num;
}
int main()
{
int result;
result = square(6);
printf("Square = %d", result);
return 0;
}
Output:
Square = 36
Advantages of Functions
- Code Reusability: Functions allow programmers to reuse code multiple times.
- Better Readability: Programs become easier to understand and manage.
- Easy Testing: Each function can be tested separately.
- Faster Development: Functions reduce overall coding effort.
- Easier Debugging: Errors can be found quickly because programs are divided into smaller parts.
Conclusion
Functions are an essential part of C programming because they help create organized and reusable programs. C provides both library functions and user defined functions for performing different operations.Understanding functions is important for writing efficient programs and learning advanced topics like recursion, pointers, structures, and file handling.
Frequently Asked Questions
1. What is a function in C?2. What are library functions in C?A function is a block of code used to perform a specific task.
3. What are user defined functions?Library functions are predefined functions provided by the C standard library.
4. Why are functions important in C?User defined functions are created by programmers according to program needs.
5. Can a function return multiple values in C?Functions improve readability, reduce repetition, and simplify debugging.
A function directly returns only one value, but multiple values can be returned using pointers or arrays.
0 Comments