Kendi kendini çağıran fonksiyonlardır . Yinelenen bir fonksiyonun her kopyasında, yerel
değişkenler ve parametreler yığın ( stack) bellekte tutulur.
ÖRNEK ► Faktöriyel Hesaplama
#include <stdio.h>
// Function to calculate the factorial of a number
int faktoryel(int n) {
if (n == 1) {
return 1; // Base case: factorial of 1 is 1
} else {
return n * faktoryel(n - 1); // Recursive case: n * factorial of (n-1)
}
}
int main() {
int number;
// Input a number from the user
printf("Bir sayı girin: ");
scanf("%d", &number);
// Calculate and print the factorial
printf("%d! = %d\n", number, faktoryel(number));
return 0;
}
RubyÖRNEK ► EBOB Hesaplama
#include <stdio.h>
#include <stdlib.h>
// Function to calculate the greatest common divisor (EBOB)
int ebob(int a, int b, int n) {
if (a % n == 0 && b % n == 0) {
return n; // Base case: n is the greatest common divisor
} else {
return ebob(a, b, n - 1); // Recursive case: decrement n and try again
}
}
int main(int argc, char *argv[]) {
int x, y, z;
// Input two numbers
printf("a = ");
scanf("%d", &x);
printf("b = ");
scanf("%d", &y);
// Calculate EBOB using the smaller of the two numbers as the starting point
z = ebob(x, y, x < y ? x : y);
// Output the result
printf("EBOB(%d, %d) = %d\n", x, y, z);
return 0;
}
Ruby