Lab_5_Use_of_Functions
Lab_5_Use_of_Functions
Use of Functions
Introduction
Functions are an essential part of programming. They allow for modular, reusable, and
organized code. In this lab, we will explore different types of functions, including
functions with parameters, return values, recursion, and function pointers.
#include <stdio.h>
void greet() {
printf("Hello, World!\n");
}
int main() {
greet(); // Function call
return 0;
}
#include <stdio.h>
void add(int a, int b) {
printf("Sum: %d\n", a + b);
}
int main() {
add(5, 3);
return 0;
}
#include <stdio.h>
int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
int main() {
printf("Factorial of 5: %d\n", factorial(5));
return 0;
}
#include <stdio.h>
void greet() {
printf("Hello from function pointer!\n");
}
int main() {
void (*funcPtr)(); // Function pointer declaration
funcPtr = greet; // Assigning function to pointer
funcPtr(); // Calling function using pointer
return 0;
}
Tasks
1. Write a function that finds the maximum of three numbers.
2. Write a function that checks whether a given number is prime.
3. Implement a recursive function to calculate the nth Fibonacci number.
4. Write a program that uses a function pointer to switch between different mathematical
operations.