0% found this document useful (0 votes)
23 views4 pages

C Programming Function Practice Guide

The document contains C programming practice exercises focusing on function-based programs and algorithms. It includes examples for summing numbers using different function patterns, swapping numbers with call by value, calculating the area of a circle using static functions, implementing recursive functions for GCD and Fibonacci, and using macros for computations. Each section provides algorithms and code snippets to demonstrate the concepts.

Uploaded by

das51sayan52
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views4 pages

C Programming Function Practice Guide

The document contains C programming practice exercises focusing on function-based programs and algorithms. It includes examples for summing numbers using different function patterns, swapping numbers with call by value, calculating the area of a circle using static functions, implementing recursive functions for GCD and Fibonacci, and using macros for computations. Each section provides algorithms and code snippets to demonstrate the concepts.

Uploaded by

das51sayan52
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

C Programming Practice - Function Based Programs with Algorithms

1. Sum using 4 different user-defined function patterns


Algorithm:
Algorithm:
1. Start
2. Define four different function patterns:
- No arguments, no return
- With arguments, no return
- No arguments, with return
- With arguments and return
3. In each, take two numbers and compute their sum accordingly
4. Call each function from main and display the result
5. End
#include <stdio.h>

void sum1() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
printf("Sum = %d\n", a + b);
}

void sum2(int a, int b) {


printf("Sum = %d\n", a + b);
}

int sum3() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
return a + b;
}

int sum4(int a, int b) {


return a + b;
}

int main() {
int a, b;
sum1();
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
sum2(a, b);
printf("Sum = %d\n", sum3());
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
printf("Sum = %d\n", sum4(a, b));
return 0;
}
C Programming Practice - Function Based Programs with Algorithms

2. Swap two numbers using call by value


Algorithm:
Algorithm:
1. Start
2. Define a function swap(a, b) that attempts to swap two numbers
3. Inside swap, use a temporary variable to exchange the values of a and b
4. In main, read two numbers and call the swap function
5. Display the values before and after calling swap
6. Show that values don't actually swap due to call by value
7. End
#include <stdio.h>

void swap(int a, int b) {


int temp = a;
a = b;
b = temp;
printf("Inside swap function: a = %d, b = %d\n", a, b);
}

int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
swap(a, b);
printf("In main function: a = %d, b = %d\n", a, b);
return 0;
}

3. Area of circle using static function inside another function


Algorithm:
Algorithm:
1. Start
2. Define a static function area(r) that calculates and returns PI * r * r
3. Define a function dis() that reads radius and calls area()
4. In main, call dis() three times in a loop
5. End
#include <stdio.h>
#define PI 3.1416

static float area(float r) {


return PI * r * r;
}

void dis() {
float r;
printf("Enter radius: ");
scanf("%f", &r);
printf("Area = %.2f\n", area(r));
C Programming Practice - Function Based Programs with Algorithms
}

int main() {
for (int i = 0; i < 3; i++) {
dis();
}
return 0;
}

4. Recursive functions for GCD and Fibonacci


Algorithm:
Algorithm for GCD:
1. Start
2. Define recursive gcd(a, b)
3. If b == 0, return a
4. Else, return gcd(b, a % b)

Algorithm for Fibonacci:


1. Start
2. Define recursive function fibonacci(n)
3. If n is 0 or 1, return n
4. Else, return fibonacci(n-1) + fibonacci(n-2)
5. Loop through 0 to n and print each term
6. End
#include <stdio.h>

int gcd(int a, int b) {


if (b == 0)
return a;
return gcd(b, a % b);
}

int fibonacci(int n) {
if (n == 0) return 0;
else if (n == 1) return 1;
else return fibonacci(n-1) + fibonacci(n-2);
}

int main() {
int a, b, n, i;
printf("Enter two numbers for GCD: ");
scanf("%d %d", &a, &b);
printf("GCD = %d\n", gcd(a, b));

printf("Enter number of terms in Fibonacci series: ");


scanf("%d", &n);
printf("Fibonacci Series: ");
for (i = 0; i < n; i++) {
printf("%d ", fibonacci(i));
}
C Programming Practice - Function Based Programs with Algorithms
printf("\n");
return 0;
}

5. Macro to compute S = A + 3*B


Algorithm:
Algorithm:
1. Start
2. Define a macro SUM(A, B) as A + 3*B
3. In main, read values for A and B
4. Use macro to compute result and display it
5. End
#include <stdio.h>
#define SUM(A, B) (A + 3 * B)

int main() {
int a, b;
printf("Enter values of A and B: ");
scanf("%d %d", &a, &b);
printf("S = %d\n", SUM(a, b));
return 0;
}

Common questions

Powered by AI

Choosing between recursive and iterative approaches must weigh factors including performance requirements, stack memory availability, ease of algorithm expression, and clarity versus complexity in understanding. Recursive solutions, while elegant and often cleaner for naturally recursive tasks (e.g., divisibility or hierarchical processes), can cause deep stack usage leading to inefficiencies or overflow. Iterative solutions circumvent these issues, generally resulting in reduced execution depth and overhead but may require complex loop management. The specific problem nature often dictates the optimal path .

The static function for computing the area of a circle exemplifies local data scope by limiting the function's visibility and modifiability to its file, providing encapsulation. Persistence is relevant to variables rather than functions directly; however, the function's static nature communicates its bounded usage context, aiding in isolating its operations from external influence. This ensures the calculation remains unaffected by other potential functions, maintaining reliable execution with each call .

Using a macro like 'S = A + 3*B' can introduce unexpected behavior due to lack of strict type enforcement and side-effect evaluation order that macros entail. Macros, unlike functions, cause direct text substitution without evaluating the safety of operations, such as parentheses misplacement leading to logical errors in complex expressions or unintended side effects when increment/decrement operations are used within macro parameters. This could lead to inaccurate results if not carefully structured, as it evaluates expressions literally .

Computational implications of using recursion for calculating Fibonacci numbers include simplicity and elegance in implementation. However, this method is limited by its inefficiency, as each call leads to multiple redundant calculations—large overlaps in subproblem solutions boost time complexity exponentially to O(2^n). Repeated calls for already computed values (e.g., in fibonacci(n-1) and fibonacci(n-2) needing their preceding sequences) heavily strain processing, especially as n grows, leading to significant performance bottlenecks .

The recursive implementation of the GCD function ensures logical simplicity by directly applying the Euclidean algorithm's core principle: GCD(a, b) is equal to GCD(b, a % b) until b is 0. This recursion creates not only a clear base case but also neatly breaks down the problem without managing loop variables explicitly, as in iterative versions. The correctness follows from precisely reiterating the consistently valid mathematical framework recursively, guaranteeing accurate results without complex logic structures typically required in loops .

Call by value in C means that the actual arguments' values are copied into the function's formal parameters, with no relation between the originals and copies. Therefore, when attempting to swap two numbers, changes inside the swap function do not affect the original variables in the calling function. The values inside swap do exchange temporarily but are not reflected back in the caller, as demonstrated by the unchanged variables after swap is called .

Input handling in the described function patterns varies, influencing user interaction flexibility and complexity. Functions like sum1 and sum3 directly request inputs, reducing the need for initial argument passing while maintaining similar interaction points internally. Alternatively, sum2 and sum4 require explicit input provision, facilitating greater initial control over inputs via the caller but necessitating prior user preparation. This diversity in handling reflects different interaction models, accommodating both centralized interaction in the function and decentralized in the calling environment .

Encapsulating area computation in a function enhances modularity by isolating the logic into a reusable, independent unit. This abstraction allows for separation of concerns, facilitating upgrades or changes to logic without affecting other components. It also promotes code reuse, as the area can be recalculated in diverse contexts without rewriting logic, leading to reduced redundancy and more straightforward maintenance—all key modular design principles .

The four user-defined function patterns illustrate different aspects of function usage by varying how arguments and returns are handled. The 'No arguments, no return' pattern (sum1) does not take input or provide output beyond printing directly. The 'With arguments, no return' pattern (sum2) takes inputs directly from its parameters, performing operations and printing results without returning a value. The 'No arguments, with return' pattern (sum3) requests inputs internally and returns the computed sum. Lastly, the 'With arguments and return' pattern (sum4) takes inputs via arguments and returns the result for external handling. This variety demonstrates how functions can be designed to suit requirements of input/output control and data encapsulation .

Macros, through direct text substitution, enhance runtime efficiency by eliminating function call overhead but at the expense of potential errors from lack of type checking and unforeseen side effects. Recursive functions provide clear, concise implementations (like GCD and Fibonacci) for problems expressed iteratively as repeated tasks but often degrade efficiency due to excessive function call overhead and lack of intermediate result caching, notably with Fibonacci series. Overall, these constructs demand careful consideration for trade-offs between human-readable code simplicity and runtime optimization, impacting maintainability negatively if misapplied .

You might also like