Function & Recursion – Let us C https://c.praveshagrawal.com Example C Programs Sat, 02 Dec 2023 11:10:47 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.2 Function Pointers https://c.praveshagrawal.com/2023/12/02/function-pointers/ https://c.praveshagrawal.com/2023/12/02/function-pointers/#respond Sat, 02 Dec 2023 11:10:47 +0000 https://c.praveshagrawal.com/?p=84 #include <stdio.h> // Function to add two numbers int add(int a, int b) { return a + b; } // Function to subtract two numbers int subtract(int a, int b) { return a - b; } int main() { // Function pointers example int (*operation)(int, int); operation = add; printf("Addition: %d\n", operation(5, 3)); operation = subtract; printf("Subtraction: %d\n", operation(5, 3)); return 0; } ]]> https://c.praveshagrawal.com/2023/12/02/function-pointers/feed/ 0 Function with Default Parameter Value https://c.praveshagrawal.com/2023/12/02/function-with-default-parameter-value/ https://c.praveshagrawal.com/2023/12/02/function-with-default-parameter-value/#respond Sat, 02 Dec 2023 11:10:24 +0000 https://c.praveshagrawal.com/?p=82 #include <stdio.h> // Function with default parameter value int power(int base, int exponent) { int result = 1; for (int i = 0; i < exponent; i++) { result *= base; } return result; } int main() { // Function with default parameter value int base = 2; int exponent = 3; int result = power(base, exponent); printf("%d to the power of %d: %d\n", base, exponent, result); return 0; } ]]> https://c.praveshagrawal.com/2023/12/02/function-with-default-parameter-value/feed/ 0 Recursive Factorial Function https://c.praveshagrawal.com/2023/12/02/recursive-factorial-function/ https://c.praveshagrawal.com/2023/12/02/recursive-factorial-function/#respond Sat, 02 Dec 2023 11:10:06 +0000 https://c.praveshagrawal.com/?p=80 #include <stdio.h> // Recursive factorial function int factorial(int n) { if (n == 0 || n == 1) { return 1; } else { return n * factorial(n - 1); } } int main() { // Recursive factorial function example int num = 5; int result = factorial(num); printf("Factorial of %d: %d\n", num, result); return 0; } ]]> https://c.praveshagrawal.com/2023/12/02/recursive-factorial-function/feed/ 0 Function with Parameters and Return Value https://c.praveshagrawal.com/2023/12/02/function-with-parameters-and-return-value/ https://c.praveshagrawal.com/2023/12/02/function-with-parameters-and-return-value/#respond Sat, 02 Dec 2023 11:09:49 +0000 https://c.praveshagrawal.com/?p=78 #include <stdio.h> // Function to calculate the square of a number int square(int num) { return num * num; } int main() { // Function with parameters and return value int number = 4; int result = square(number); printf("Square of %d: %d\n", number, result); return 0; } ]]> https://c.praveshagrawal.com/2023/12/02/function-with-parameters-and-return-value/feed/ 0 Simple Function https://c.praveshagrawal.com/2023/12/02/simple-function/ https://c.praveshagrawal.com/2023/12/02/simple-function/#respond Sat, 02 Dec 2023 11:09:28 +0000 https://c.praveshagrawal.com/?p=76 #include <stdio.h> // Function to add two numbers int add(int a, int b) { return a + b; } int main() { // Simple function example int result = add(5, 7); printf("Sum: %d\n", result); return 0; } ]]> https://c.praveshagrawal.com/2023/12/02/simple-function/feed/ 0