User Defined Functions
User Defined Functions
A function can be called multiple times to provide reusability and modularity to the
C program. In other words, we can say that the collection of functions creates a
program. The function is also known as procedure or subroutine in other
programming languages.
When you divide a large program into various functions, it becomes easy to
manage each function individually.
Whenever an error occurs in the program, you can easily investigate faulty
functions and correct only those errors. You can easily call and use functions
whenever they are required which automatically leads to saving time and space.
Advantages of Function
Types of function
There are two types of function in C programming:
The prototype and data definitions of these functions are present in their
respective header files. To use these functions we need to include the header
file in our program. For example,
If you want to use the printf() function, the header file <stdio.h> should be
included.
2.User-defined function
You can also create functions as per your need. Such functions created by the user
are known as user-defined functions.
The function declarations (called prototype) are usually done above the main ()
function and take the general form:
The return_data_type: is the data type of the value function returned back to the
calling statement.
The function_name: is followed by parentheses
Arguments names with their data type declarations optionally are placed inside the
parentheses.
Function Definition
Function definition means just writing the body of a function. A body of a function
consists of statements which are going to perform a specific task. A function body
consists of a single or a block of statements.
Function call
A function call means calling a function whenever it is required in a program.
Whenever we call a function, it performs an operation for which it was designed.
Output:
Enter the length and breadth
4
5
Output:
Output:
Output:
Enter the length and breadth
5
9
The area of the rectangle is: 45
--------------------------------
Process exited after 2.751 seconds with return value 0
Press any key to continue . . .
Programs:
1.Wap to find the area of a triangle using user-defined functions
#include <stdio.h>
void area_of_triangle();
int main()
{
area_of_triangle();
return 0;
Output:
Enter base 5
Enter height 5
Area of the triangle= 12.500000 sq. units
--------------------------------
#include<stdio.h>
// function declaration
float circleArea(float r);
int main()
{
float radius, area;
return 0;
}
// function definition
float circleArea(float r)
{
float area;
area= 3.14 * r * r;
return area; // return statement
}
#include <stdio.h>
Output:
Enter number 5
5 is odd
--------------------------------
Output:
Enter some number for finding cube
3
#include<stdio.h>
// function prototype, also called function declaration
float square ( float x );
int main( )
{
float m, n ;
printf ( "\nEnter some number for finding square \n");
scanf ( "%f", &m ) ;
n = square ( m ) ; //function call
printf ( "\nSquare of the given number %f is %f",m,n );
}
Output:
#include <stdio.h>
/* function declaration */
int max(int num1, int num2);
Output:
Enter the two numbers
4
6
Max value is : 6
#include <stdio.h>
Output:
Enter the 3 numbers4
5
6
The greatest number is 6
#include<stdio.h>
void sum();
int main()
{
sum();
return 0;
}
void sum()
{
int n, result,sum=0;
printf("Upto which number you want to find sum: ");
scanf("%d", &n);
for(int i=1; i<=n; i++)
{
sum += i;
}
printf("The sum of the given n natural numbers is %d",sum);
}
Output:
Upto which number you want to find sum: 10
The sum of the given n natural numbers is 55
--------------------------------
#include<stdio.h>
void sum();
int main()
{
sum();
return 0;
Output:
Upto which number you want to find sum: 10
The sum of the given n natural even numbers is 30
--------------------------------
#include<stdio.h>
void sum();
int main()
{
sum();
return 0;
}
void sum()
{
int n, result,sum=0;
printf("Upto which number you want to find sum: ");
scanf("%d", &n);
for(int i=1; i<=n; i+=2)
{
sum += i;
Output:
Upto which number you want to find sum: 10
The sum of the given n natural odd numbers is 25
--------------------------------
#include <stdio.h>
void fact();
int main()
{
fact();
return 0;
}
void fact()
{
int i,n,f=1;
printf("Enter a number to calculate its factorial\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
f=f*i;
}
printf("Factorial of the num %d = %d\n",n,f);
}
Output:
Enter a number to calculate its factorial
5
Factorial of the num 5 = 120
#include<stdio.h>
void factors();
int main()
{
factors();
return 0;
}
void factors()
{
int i,n;
printf("Enter the number:\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(n%i==0)
printf("%d is a factor of %d\n",i,n);
}
}
Output:
1 is a factor of 15
3 is a factor of 15
5 is a factor of 15
15 is a factor of 15
Output:
enter a number
123
the reverse of 123 is 321
--------------------------------
Process exited after 1.7 seconds with return value 0
Press any key to continue . . .
#include <stdio.h>
void check_palindrome();
int main()
Output:
Output:
enter the number of digits4
enter a number1634
1634 is armstrong
--------------------------------
#include<stdio.h>
void pos_neg();
int main()
{
pos_neg();
return 0;
}
void pos_neg()
{
int n;
printf("enter the number:");
scanf("%d",&n);
if(n>0)
{
printf("the number %d is positive",n);
}
else if(n<0)
{
printf("the number %d is negative",n);
}
else
{
printf("the number %d is zero",n);
}
Output:
#include<stdio.h>
void check_primeorcomposite();
int main()
{
check_primeorcomposite();
return 0;
}
void check_primeorcomposite()
{
int i,count=0;
int num;
printf("Enter a number to check whether it is prime or not\n");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
if(num%i==0)
{
count++;
}
}
if(count==1)
{
printf("The given number is neither prime nor composite");
}
else if(count==2)
#include<stdio.h>
void fibonacciSeries();
int main()
{
fibonacciSeries();
return 0;
}
void fibonacciSeries()
{
int a=0, b=1, temp,range;
printf("Enter range upto which you want to display: ");
scanf("%d", &range);
printf("\nThe Fibonacci series is: \n");
while (a<=range)
{
printf("%d\t", a);
temp = a+b;
void fibonacciSeries()
{
int a=0, b=1, c,i;
int term;
printf("Enter the term: ");
scanf("%d", &term);
for(i=0; i<term; i++)
{
printf("%d\t", a);
c = a+b;
a = b;
b = c;
}
}
#include<stdio.h>
void fibonacciTerm();
int main()
{
fibonacciTerm();
return 0;
}
void fibonacciTerm()
{
int a=0, b=1, c,i;
int term;
printf("Enter term to find: ");
scanf("%d", &term);
for(i=1; i<term; i++)
{
c = a+b;
a = b;
b = c;
}
printf("%d",a);
}
Output:
Enter term to find: 12
89
--------------------------------
Output:
Enter a positive number
5
--------------------------------