0% found this document useful (0 votes)
4 views13 pages

C Function Parameters and Examples

The document covers various aspects of functions in C programming, including the significance of function parameters, function prototyping, and the distinction between formal and actual parameters. It also discusses categories of functions, advantages and disadvantages of recursion, and provides examples of mathematical functions like abs(), fabs(), and sqrt(). Additionally, it includes sample programs demonstrating user-defined functions, recursion, and character manipulation using ctype.h.

Uploaded by

kashishgupta9892
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)
4 views13 pages

C Function Parameters and Examples

The document covers various aspects of functions in C programming, including the significance of function parameters, function prototyping, and the distinction between formal and actual parameters. It also discusses categories of functions, advantages and disadvantages of recursion, and provides examples of mathematical functions like abs(), fabs(), and sqrt(). Additionally, it includes sample programs demonstrating user-defined functions, recursion, and character manipulation using ctype.h.

Uploaded by

kashishgupta9892
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

Q64. (2 Marks) What is the significance of function parameters in C?

Answer:
Function parameters allow values to be passed to functions, enabling the function to
operate on different data and return results. They make functions more flexible and
reusable.

Q65. (2 Marks) Explain Function Prototyping with example.

Answer:
Function prototyping declares the function signature before its definition. It helps the
compiler ensure correct usage of the function.

Example:

#include <stdio.h>

// Function prototype

int add(int, int);

int main() {

int result = add(5, 3);

printf("Result: %d", result);

return 0;

// Function definition

int add(int a, int b) {

return a + b;

}
Q66. (2 Marks) Illustrate Formal and Actual Parameters with example.

Answer:

• Formal Parameters: Variables defined in the function definition.

• Actual Parameters: Values passed to the function during the function call.

Example:

#include <stdio.h>

void sum(int a, int b); // Formal Parameters

int main() {

sum(3, 4); // Actual Parameters

return 0;

void sum(int a, int b) { // Formal Parameters

printf("Sum: %d", a + b);

Q67. (2 Marks) Discuss the Categories of Function.

Answer:
Functions in C are categorized as:

1. Library Functions: Predefined functions like printf(), scanf().

2. User-defined Functions: Functions created by the programmer to perform specific


tasks.

Q68. (2 Marks) Show advantages & disadvantages of recursion.

Answer:

• Advantages:
1. Simplifies problems like tree traversals and factorial computation.

2. Easier to write and understand in some cases.

• Disadvantages:

1. Recursion can lead to stack overflow if the base case is not defined properly.

2. May use more memory and take longer to execute due to function calls.

Q69. (2 Marks) What is the role of the abs() and fabs() function in math.h?

Answer:

• abs(): Returns the absolute value of an integer.

• fabs(): Returns the absolute value of a floating-point number.

Example:

#include <stdio.h>

#include <math.h>

int main() {

int x = -5;

float y = -5.5;

printf("abs: %d\n", abs(x));

printf("fabs: %.1f\n", fabs(y));

return 0;

Q70. (2 Marks) Explain sqrt() function with example.

Answer:
sqrt() returns the square root of a number.

Example:
#include <stdio.h>

#include <math.h>

int main() {

double num = 16.0;

printf("Square root: %.2f", sqrt(num));

return 0;

Q71. (5 Marks) Write a C program to find the maximum and minimum of some values
entered by user using a function.

Answer:

#include <stdio.h>

void find_max_min(int arr[], int n, int *max, int *min) {

*max = arr[0];

*min = arr[0];

for (int i = 1; i < n; i++) {

if (arr[i] > *max) *max = arr[i];

if (arr[i] < *min) *min = arr[i];

int main() {

int n;

printf("Enter number of elements: ");


scanf("%d", &n);

int arr[n];

printf("Enter elements:\n");

for (int i = 0; i < n; i++) {

scanf("%d", &arr[i]);

int max, min;

find_max_min(arr, n, &max, &min);

printf("Maximum: %d\nMinimum: %d\n", max, min);

return 0;

Q72. (5 Marks) Write a program to calculate the average of five numbers using a
function.

Answer:

#include <stdio.h>

float calculate_average(int a, int b, int c, int d, int e) {

return (a + b + c + d + e) / 5.0;

int main() {

int a, b, c, d, e;

printf("Enter five numbers: ");

scanf("%d %d %d %d %d", &a, &b, &c, &d, &e);


printf("Average: %.2f", calculate_average(a, b, c, d, e));

return 0;

Q73. (5 Marks) Write program to check a given number is prime or not using recursion.

Answer:

#include <stdio.h>

int is_prime(int num, int i) {

if (i == 1) return 1;

if (num % i == 0) return 0;

return is_prime(num, i - 1);

int main() {

int num;

printf("Enter a number: ");

scanf("%d", &num);

if (is_prime(num, num / 2))

printf("%d is a prime number.", num);

else

printf("%d is not a prime number.", num);

return 0;

}
Q74. (5 Marks) Explain the various functions provided by ctype.h for character
manipulation.

Answer:
The ctype.h library provides functions to check character types and convert characters:

1. isalnum(): Checks if the character is alphanumeric.

2. isalpha(): Checks if the character is alphabetic.

3. isdigit(): Checks if the character is a digit.

4. tolower(): Converts the character to lowercase.

5. toupper(): Converts the character to uppercase.

Example:

#include <stdio.h>

#include <ctype.h>

int main() {

char ch = 'a';

printf("%c is %s\n", ch, isalpha(ch) ? "Alphabet" : "Not Alphabet");

return 0;

Q75. (5 Marks) Write a C program to convert decimal to binary using Function.

Answer:

#include <stdio.h>

void decimal_to_binary(int n) {

if (n > 1)

decimal_to_binary(n / 2);
printf("%d", n % 2);

int main() {

int num;

printf("Enter a decimal number: ");

scanf("%d", &num);

printf("Binary representation: ");

decimal_to_binary(num);

return 0;

Q76. (5 Marks) Define user-defined functions. How do they differ from library
functions?

Answer:

• User-defined functions: Functions created by the programmer to perform specific


tasks.

• Library functions: Predefined functions provided by C libraries for common


operations.

Difference:

• User-defined functions are created by the programmer for specific needs, whereas
library functions are predefined and provided by the C library.

Q77. (5 Marks) List different functions provided by ctype.h and math.h libraries.
Explain one from each.

Answer:

• ctype.h:
1. isalnum()

2. isalpha()

3. isdigit()

• math.h:

1. sqrt(): Calculates the square root.

2. pow(): Calculates power of a number.

Q78. (10 Marks) Discuss Function and Explain with Suitable example: (a) Function
Prototyping (b) Function Call (c) Function Definition

Answer:

• Function Prototyping: Declares a function signature before its definition.

int add(int, int); // Function prototype

• Function Call: Invoking a function with arguments.

int result = add(5, 3); // Function call

• Function Definition: Defines the body of the function.

int add(int a, int b) {

return a + b;

Q79. (10 Marks) Explain Storage Classes in detail: (a) Static (b) Auto (c) Register (d)
Extern

Answer:

1. Static: Variables retain their value between function calls.

2. Auto: Default storage class for local variables, which is automatically initialized.

3. Register: Suggests the variable to be stored in a CPU register for faster access.

4. Extern: Used to declare a variable that is defined outside of the current file.
Q80. (10 Marks) Differentiate Call by Value and call by reference in function with
suitable example.

Answer:

• Call by Value: Passes a copy of the argument's value.

• Call by Reference: Passes the address of the argument, allowing direct


modification.

Example:

// Call by value

void add(int a) { a = a + 5; }

// Call by reference

void add(int *a) { *a = *a + 5; }

Q81. (10 Marks) Write code to find Fibonacci Series of numbers using a function.

Answer:

#include <stdio.h>

void fibonacci(int n) {

int a = 0, b = 1, next;

printf("%d %d ", a, b);

for (int i = 3; i <= n; i++) {

next = a + b;

printf("%d ", next);

a = b;

b = next;

}
int main() {

int n;

printf("Enter the number of terms: ");

scanf("%d", &n);

fibonacci(n);

return 0;

Q82. (10 Marks) State Advantages and disadvantages of Recursion. Develop a code to
perform Factorial using Recursive Function.

Answer:

• Advantages:

1. Simplifies problems like factorial and Fibonacci sequence.

2. Can replace loops in some cases.

• Disadvantages:

1. May lead to stack overflow if the base case is not reached.

2. Uses more memory due to repeated function calls.

Factorial Program:

#include <stdio.h>

int factorial(int n) {

if (n == 0) return 1;

return n * factorial(n - 1);

}
int main() {

int num;

printf("Enter a number: ");

scanf("%d", &num);

printf("Factorial of %d is %d", num, factorial(num));

return 0;

Q83. (10 Marks) Explain any Two Categories of Function with Suitable Example.

Answer:

1. Library Functions: These are predefined functions like printf() and scanf().

printf("Hello, World!");

2. User-Defined Functions: Functions created by the programmer for specific tasks.

int add(int a, int b) { return a + b; }

Q84. (10 Marks) Write a program using recursion to find sum of digits of a number.

Answer:

#include <stdio.h>

int sum_of_digits(int num) {

if (num == 0) return 0;

return num % 10 + sum_of_digits(num / 10);

int main() {

int num;
printf("Enter a number: ");

scanf("%d", &num);

printf("Sum of digits: %d", sum_of_digits(num));

return 0;

You might also like