Code Point Blog
Iam presenting here ,all type of knowledge if you want to interest knowing about any topic then click below thumbnail view button and knowing.
Functions in C are an integral part of the language and serve various purposes. There are several types of functions based on their return types and parameters:
1.Functions with a Return Value :These functions return a value of a specified data type after performing their task. The return type is explicitly mentioned in the function declaration.
For example:
#include <stdio.h>
int add(int a,int b);
return a+b;
}
2.Functions with No Return Value (void functions) :These functions do not return any value. They are typically used for tasks that do not need to produce a result. For example:
#include <stdio.h>
void greet(){
printf("hello world !");
}
3.Functions with Parameters :
These functions accept input values (parameters) that are used in their operations. Parameters are specified in the function declaration and definition.
#include <stdio.h>
int square(int num){
return num*num;
}
4.functions without Parameters : These functions do not take any parameters. They perform their tasks based on predefined logic within the function body.
For example:
#include <stdio.h>
int gererateRandomNumber(){
return rand();
}
5.Recursive FunctionsThese are functions that call themselves either directly or indirectly. Recursive functions are used to solve problems that can be divided into smaller, similar sub-problems.
For example:
#include <stdio.h>
int factorial(int n) {
if (n == 0 || n == 1)
return 1;
else
return n * factorial(n - 1);
}
In C language, there are two main types of function calls:
1.Regular (or Direct) Function Call :
#include <stdio.h>
// Function definition
void greet() {
printf("Hello, World!\n");
}
int main() {
// Regular function call
greet(); // Function is called directly
printf("After the function call.\n");
return 0;
}
Output:
Hello, World!
After the function call.
2.Recursive Function Call :
#include <stdio.h>
int factorial(int n) {
if (n == 0 || n == 1)
return 1;
else
return n * factorial(n - 1); // Recursive function call
}
int main() {
int num = 5;
int result = factorial(num); // Recursive function call
printf("Factorial of %d is %d\n", num, result);
return 0;
}
Output:
Factorial of 5 is 120
In the recursive function call example, the `factorial` function calls itself with a smaller
value (`n - 1`) until it reaches the base case (`n == 0` or `n == 1`), where the recursion
stops.