Part 5: Function & Recursion
Introduction
to Functions
A
function is a block of code that performs a specific task. It helps to divide a
large program into smaller parts, making the program easier to write,
understand, test and reuse.
Syntax
of a user-defined function in C:
return_type
function_name(parameter_list)
{
// Statements
return value;
}
1.
Write a program to calculate the sum of two numbers using function.
Blue: Function
name
Green: Parameters / arguments
Orange: Return statement
Purple: Result variable
Red: Local
variable
#include
<stdio.h>
Function
definition
int
sum(int
x, int y)
{
int s;
s = x + y;
return s;
}
Main function
int
main()
{
int x, y,
result;
printf("Enter two numbers: ");
scanf("%d %d", &x, &y);
result =
sum(x,
y);
printf("Sum = %d", result);
return 0;
}
2.
Write a program to calculate the area of rectangle using function.
#include
<stdio.h>
float
area(float l, float b)
{
float a;
a = l * b;
return a;
}
int
main()
{
float l, b, result;
printf("Enter length and breadth:
");
scanf("%f %f", &l, &b);
result = area(l, b);
printf("Area of rectangle =
%.2f", result);
return 0;
}
3.
Write a program to calculate the area of circle using function.
#include
<stdio.h>
float
area(float r)
{
float a;
a =
3.14 * r * r;
return a;
}
int
main()
{
float r, result;
printf("Enter radius: ");
scanf("%f", &r);
result = area(r);
printf("Area of circle = %f",
result);
return 0;
}
4.
Write a program to calculate the simple interest using function.
#include
<stdio.h>
float
interest(float p, float t, float r)
{
float si;
si = (p * t * r) / 100;
return si;
}
int
main()
{
float p, t, r, result;
printf("Enter principal, time and
rate: ");
scanf("%f %f %f", &p, &t,
&r);
result = interest(p, t, r);
printf("Simple Interest = %f", result);
return 0;
}
5.
Write a program to calculate the square of a given number using function.
#include
<stdio.h>
int
square(int n)
{
int s;
s = n * n;
return s;
}
int
main()
{
int n, result;
printf("Enter a number: ");
scanf("%d", &n);
result = square(n);
printf("Square = %d", result);
return 0;
}
6. Write a program to check whether a given number is odd or even using function.
#include
<stdio.h>
int
check(int n)
{
if (n % 2 == 0)
printf("Even number");
else
printf("Odd number");
}
int
main()
{
int n;
printf("Enter a number: ");
scanf("%d", &n);
check(n);
return 0;
}
7.
Write a program to check whether a given number is positive, negative or zero
using function.
#include
<stdio.h>
int
check(int n)
{
if (n > 0)
printf("Positive number");
else if (n < 0)
printf("Negative number");
else
printf("Zero");
}
int
main()
{
int n;
printf("Enter a number: ");
scanf("%d", &n);
check(n);
return 0;
}
8.
Write a program to find the greatest among two numbers using function.
#include
<stdio.h>
int
greatest(int a, int b)
{
if (a > b)
return a;
else
return b;
}
int
main()
{
int a, b, result;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
result = greatest(a, b);
printf("Greatest = %d", result);
return 0;
}
9. Write a program to find the smallest among three numbers using function.
#include
<stdio.h>
int
smallest(int a, int b, int c)
{
if (a <= b && a <= c)
return a;
else if (b <= a && b <= c)
return b;
else
return c;
}
int
main()
{
int a, b, c, result;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b,
&c);
result = smallest(a, b, c);
printf("Smallest = %d", result);
return 0;
}
10.
Write a program to find the factorial of a given number using function.
#include <stdio.h>
int factorial(int n)
{
int f = 1;
int i;
for (i = 1; i <=
n; i++)
f = f * i;
return f;
}
int main()
{
int n;
int result;
printf("Enter
a number: ");
scanf("%d",
&n);
result = factorial(n);
printf("Factorial
= %ld", result);
return 0;
}
11. Write a program to find the factors of a given number using function.
#include <stdio.h>
int factors(int n)
{
int i;
for (i = 1; i <=
n; i++)
{
if (n % i == 0)
printf("%d
", i);
}
}
int main()
{
int n;
printf("Enter
a number: ");
scanf("%d",
&n);
printf("Factors
of %d are: ", n);
factors(n);
return 0;
}
2079
GIE Set B Q.No. 16 (31)
Program
to generate factorial of a given number using recursive function in C.
#include
<stdio.h>
int
factorial(int n)
{
if (n == 0)
return 1;
else
return n * factorial(n - 1);
}
int
main()
{
int n, result;
printf("Enter a number: ");
scanf("%d", &n);
result = factorial(n);
printf("Factorial = %d", result);
return 0;
}
2076
GIE Set A Q.No. 3 (39)
How
do you define a function in C program? Write a program to find whether a given
number is negative or positive using function.
A
function in C is a block of code that performs a specific task. It can be
called whenever required in a program.
#include
<stdio.h>
int
check(int n)
{
if (n > 0)
printf("Positive number");
else if (n < 0)
printf("Negative number");
else
printf("Zero");
}
int
main()
{
int n;
printf("Enter a number: ");
scanf("%d", &n);
check(n);
return 0;
}
2076
GIE Set B Q.No. 5 (41)
Explain
the structure in C. Write a program to enter 5 student names, ages and sex
using structure and display them.
A
structure in C is a user-defined data type that groups different types of
variables under a single name.
#include
<stdio.h>
struct
student
{
char name[30];
int age;
char gender;
};
int
main()
{
struct student s[5];
int i;
for (i = 0; i < 5; i++)
{
printf("Enter name, age and gender:
");
scanf("%s %d %c", s[i].name,
&s[i].age, &s[i]. gender);
}
printf("\nStudent Details:\n");
for (i = 0; i < 5; i++)
{
printf("\nName = %s",
s[i].name);
printf("\nAge = %d",
s[i].age);
printf("\n gender = %c\n",
s[i]. gender);
}
return 0;
}
2076
Q.No. 1 (43)
What
is a user-defined function? Write a program to input length and breadth of a
pond and find its area using a user-defined function.
A
user-defined function is a function created by the programmer to perform a
specific task.
#include
<stdio.h>
int
area(int l, int b)
{
int a;
a = l * b;
return a;
}
int
main()
{
int l, b, result;
printf("Enter length and breadth:
");
scanf("%d %d", &l, &b);
result = area(l, b);
printf("Area of pond = %d",
result);
return 0;
}
2075
Set A Q.No. 3 (47)
What
is a function? Write a program to read principal, time and rate. Find simple
interest using function.
A
function is a block of code that performs a specific task and can be used
whenever required.
#include
<stdio.h>
float
interest(float p, float t, float r)
{
float si;
si = (p * t * r) / 100;
return si;
}
int
main()
{
float p, t, r, result;
printf("Enter principal, time and
rate: ");
scanf("%f %f %f", &p, &t,
&r);
result = interest(p, t, r);
printf("Simple Interest = %f", result);
return 0;
}
2074
Supplementary Q.No. 3 (49)
What
are the importance of function in C program? Write a program to find whether a
given number is odd or even using function.
Importance
of functions:
Code
reusability: A function can be used many times.
Easy
debugging: Errors can be found and corrected easily.
Modularity:
A large program can be divided into smaller parts.
Easy
maintenance: Programs become easier to modify and understand.
Program:
#include
<stdio.h>
int
check(int n)
{
if (n % 2 == 0)
printf("Even number");
else
printf("Odd number");
}
int
main()
{
int n;
printf("Enter a number: ");
scanf("%d", &n);
check(n);
return 0;
}
2074
Set A Q.No. 4 / 2073 Set C Q.No. 2 (51)
Describe
function in C. Write a program to find the sum of two integer numbers using
function.
A
function in C is a block of code that performs a specific task. It helps to
divide a large program into smaller and reusable parts.
Program:
#include
<stdio.h>
int
sum(int a, int b)
{
int s;
s = a + b;
return s;
}
int
main()
{
int a, b, result;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
result = sum(a, b);
printf("Sum = %d", result);
return 0;
}
2074
Set B Q.No. 3
What
are the methods of function declaration and call in C? Write a program to find
whether a given number is odd or even using function.
There
are four types of user-defined functions in C based on arguments and return
values:
· Function with no
arguments and no return value.
· Function with
arguments and no return value.
· Function with no
arguments but with return value.
· Function with
arguments and return value.
Program:
#include
<stdio.h>
int
check(int n)
{
if (n % 2 == 0)
printf("Even number");
else
printf("Odd number");
}
int
main()
{
int n;
printf("Enter a number: ");
scanf("%d", &n);
check(n);
return 0;
}