Functions in C
A function in C is a block of code that performs a specific task. Functions help in code reusability, modular programming, and easy debugging.
Types of Functions
In C, functions are categorized into two types:
- Library Functions: Predefined functions like
printf()
,scanf()
,sqrt()
, etc. - User-Defined Functions: Functions created by the programmer to perform specific tasks.
Function Declaration, Definition, and Calling
Function Declaration
A function must be declared before use. It specifies the function name, return type, and parameters.
int add(int, int); // Function declaration
Function Definition
A function definition contains the actual implementation.
#include <stdio.h>
// Function definition
int add(int a, int b) {
return a + b;
}
int main() {
int sum = add(5, 3); // Function call
printf("Sum: %d", sum);
return 0;
}
Function Calling
To execute a function, it must be called in main()
or another function.
Types of Function Arguments
1. Function Without Parameters and Without Return Value
A function that takes no arguments and returns nothing.
#include <stdio.h>
void greet() {
printf("Hello, Welcome to C Programming!");
}
int main() {
greet(); // Function call
return 0;
}
2. Function With Parameters and Without Return Value
A function that takes arguments but does not return any value.
#include <stdio.h>
void display(int num) {
printf("Number: %d", num);
}
int main() {
display(10); // Function call
return 0;
}
3. Function Without Parameters and With Return Value
A function that does not take arguments but returns a value.
#include <stdio.h>
int getNumber() {
return 25;
}
int main() {
int num = getNumber();
printf("Returned Value: %d", num);
return 0;
}
4. Function With Parameters and With Return Value
A function that takes arguments and returns a value.
#include <stdio.h>
int multiply(int a, int b) {
return a * b;
}
int main() {
int result = multiply(4, 5);
printf("Multiplication: %d", result);
return 0;
}
Call by Value vs Call by Reference
Call by Value
In Call by Value, a copy of the argument is passed to the function. Changes in the function do not affect the original variable.
#include <stdio.h>
void modify(int x) {
x = 20; // Changing value inside the function
}
int main() {
int num = 10;
modify(num);
printf("Value after function call: %d", num); // Output: 10
return 0;
}
Call by Reference
In Call by Reference, a pointer to the argument is passed, allowing modifications to affect the original variable.
#include <stdio.h>
void modify(int *x) {
*x = 20; // Modifying the actual value
}
int main() {
int num = 10;
modify(&num);
printf("Value after function call: %d", num); // Output: 20
return 0;
}
Recursion in C
Recursion is when a function calls itself to solve a problem.
Example: Factorial Using Recursion
#include <stdio.h>
int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
int main() {
int num = 5;
printf("Factorial of %d is %d", num, factorial(num));
return 0;
}
Functions are essential in C programming for structuring code efficiently. They improve code reusability, readability, and modularity.
If you have any questions, feel free to ask. Thank you for reading!
If you have any query or question, please contact through below form. Our team will be in touch with you soon.
Please provide your valueable feedback or suggession as well.