



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
This comprehensive document explores the fundamental concepts of functions and recursion in the C programming language. It begins with a clear explanation of function syntax, declaration, and usage, followed by an in-depth look at recursion—its structure, advantages, and limitations. The document includes a variety of sample problems with step-by-step solutions to reinforce learning and provide practical coding experience. Ideal for students and beginners aiming to strengthen their understanding of modular programming and recursive problem-solving techniques in C.
Typology: Study notes
1 / 6
This page cannot be seen from the preview
Don't miss anything!
void add() { int a,b,c; scanf("%d",&a); scanf("%d",&b); c=a+b; return c; } int main() { printf("%d",add()); return 0; }
A function is a block of code that performs a specific task. It allows us to divide a large program into smaller, manageable parts.
return_type function_name(parameter_list) { // Function body return value; // (if return type is not void) }
#include <stdio.h> // Function declaration int add(int, int); int main() { int a = 5 , b = 10 ; int result = add(a, b); // Function calling printf("Sum = %d\n", result); return 0 ; } // Function definition int add(int x, int y) { return x + y;
Type Arguments Return Type Example Function with return & arguments Yes Yes int add(int, int); Function with return, no arguments No Yes int getValue(); Function with arguments, no return Yes No void printValue(int); Function with no return & no arguments No No void display();
Recursion is a technique where a function calls itself directly or indirectly to solve a problem.
#include <stdio.h> // Function to calculate factorial int factorial(int n) { if (n == 0 ) // Base Condition return 1 ; return n * factorial(n - 1 ); // Recursive Call } int main() { int num = 5 ; printf("Factorial of %d = %d\n", num, factorial(num)); return 0 ; }
// Validating input if (r > num || num < 0 || r < 0) { printf("Invalid input! Ensure that n >= r >= 0.\n"); } else { printf("P(%d, %d) = %lld\n", num, r, permutations(num, r)); } return 0; }
#include <stdio.h> #include <math.h> int is_prime(int n) { if (n < 2) { return 0; // Numbers < 2 are not prime } for (int i = 2; i <= sqrt(n); i++) { // Corrected loop if (n % i == 0) { return 0; // Not prime } } return 1; // Prime } int main() { int num, temp, l, f, rot; printf("Enter number: "); scanf("%d", &num); int d = log10(num) + 1; // Get number of digits temp = num; // Preserve original number for (int i = 0; i < d; i++) { // Rotate based on digit count f = temp / 10; // Remove last digit l = temp % 10; // Extract last digit rot = (l * pow(10, d - 1)) + f; // Shift last digit to front if (!is_prime(rot)) { // Check if rotation is prime printf("not circular\n"); return 0; } temp = rot; // Update temp to new rotation
printf("circular\n"); return 0; }
#include <stdio.h> // Recursive function to sum odd numbers in the array int sumOdd(int arr[], int n) { if (n == 0) return 0; int lastElement = arr[n - 1]; // Get the last element return (lastElement % 2 != 0? lastElement : 0) + sumOdd(arr, n - 1); } int main() { int n; // Get the size of the array printf("Enter the number of elements: "); scanf("%d", &n); int arr[n]; // Input elements printf("Enter %d elements: ", n); for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); } // Call the recursive function int sum = sumOdd(arr, n); // Output result printf("Sum of odd numbers: %d\n", sum); return 0;