Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Functions and Recursion in C: Concepts, Examples, and Solutions, Study notes of Computer Science

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

2024/2025

Available from 06/09/2025

madumitha-g
madumitha-g 🇮🇳

4

(1)

4 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Functions and Recursion in C
//Basic Syntax
#include<stdio.h>
void add()
{
int a,b,c;
scanf("%d",&a);
scanf("%d",&b);
c=a+b;
return c;
}
int main() {
printf("%d",add());
return 0;
}
//Functions
Afunction is a block of code that performs a specific task. It allows us to divide a
large program into smaller, manageable parts.
//Syntax of a Function
return_type function_name(parameter_list) {
// Function body
return value; // (if return type is not void)
}
//Example of a Function
#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;
}
pf3
pf4
pf5

Partial preview of the text

Download Functions and Recursion in C: Concepts, Examples, and Solutions and more Study notes Computer Science in PDF only on Docsity!

Functions and Recursion in C

//Basic Syntax

#include<stdio.h>

void add() { int a,b,c; scanf("%d",&a); scanf("%d",&b); c=a+b; return c; } int main() { printf("%d",add()); return 0; }

//Functions

A function is a block of code that performs a specific task. It allows us to divide a large program into smaller, manageable parts.

//Syntax of a Function

return_type function_name(parameter_list) { // Function body return value; // (if return type is not void) }

//Example of a Function

#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;

//Types of Functions Based on Arguments and Return Type

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

Recursion is a technique where a function calls itself directly or indirectly to solve a problem.

//Properties of Recursion

  1. Base Condition – A condition to stop recursion.
  2. Recursive Case – A part where the function calls itself.

//Example: Factorial Using Recursion

#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 ; }

//Types of Recursion

  1. Direct Recursion – A function directly calls itself.
  2. Indirect Recursion – Function A calls Function B, which in turn calls Function A.

// 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; }

circular prime

#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; }

Sum of odd numbers in an array using recursion

Way 1

#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;