Function
Function
1. Definition of Function
A function is a self-contained block of code that performs a specific task. Functions are used to
divide a large program into smaller, manageable, and reusable sections.
2. Types of Functions in C
3. Advantages of Functions
• Code reusability
• Declaration: Function prototype, specifying the function name, return type, and parameters.
return a + b;
• Call by Value: Copies the value of actual parameters into formal parameters.
o Example:
int temp = a;
a = b;
b = temp;
• Call by Reference: Passes the address of actual parameters, allowing changes to be reflected
outside the function.
o Example:
*a = *b;
*b = temp;
• Actual Parameters: Values or variables passed to the function during the function call.
8. Recursive Functions
• Example:
int factorial(int n) {
if (n == 0) return 1;
9. Example Programs
1. Area of a Sphere
#include <stdio.h>
#define PI 3.14159
int main() {
float r;
return 0;
#include <stdio.h>
if (num % i == 0) return 0;
return 1;
int main() {
int n;
scanf("%d", &n);
if (isPrime(n))
else
return 0;
3. Area of a Rectangle
#include <stdio.h>
int main() {
int l, w;
return 0;