Programming C Function
Programming C Function
In programming C, we can divide a large program into the basic building blocks known as function. The function
contains the set of sub program and its statements enclosed by { }. A function can be called multiple times by main()
function, to provide reusability and modularity to the C program. Function is a block of code that performs a specific
task. There are two types of function in C programming
1. LIBRARY FUNCTION
The library functions are built-in functions in C programming. These functions are defined in header files. For
example, The printf() is a standard library function to send output to the screen. This is done because function
printf() defined in the stdio.h header file. Library function supports the following functions, for Example:
Mathematical Library Function #include<math.h> The function which is use to do any mathematical work. Like
sqrt() It is use to calculates the square root of a number and before using them in our program we
must add #include<math.h> file in the beginning of program. (Hint: √36 = 6)
root = sqrt(num);
pow() It is use to calculates the to the power value of a any input number and before using them in our
3
program we must add #include<math.h> file in the beginning of program. (Hint: 4 = 64 )
#include<stdio.h>
Enter your base value 4
#include<conio.h>
Enter to the power value 3
#include<math.h>
void main() 4 to the power of 3 = 64
{
clrscr();
int b, p, result=0;
printf("Enter your base value: ");
scanf("%d",&b);
printf("Enter to the power value: ");
scanf("%d",&p);
result=pow(b,p);
printf("%d to the power of %d = %d",b,p,result);
getch();
}
STRING LIGRARY FUNCTION #include<string.h> The function which is used to work on String type value. Like
strlen() This function is use to calculate the length of any input string ‘ Charter’ and before using them in
our program we must add #include<string.h> file in the beginning of program.
#include <stdio.h> Please Enter your name Abhijeet
#include <string.h>
int main() The length of your input Name is 8
{
int count=0;
char name[9];
printf("Please Enter your name: ");
scanf("%s", &name);
count = strlen(name);
printf("The Length of your input string is %d", count);
return 0;
}
strcat() It library function use to add two input string and before using them in our program we must
add #include<string.h> file in the beginning of program.
Enter your first name Abhijeet
#include<stdio.h>
#include<conio.h> Enter your second name Mishra
#include<string.h>
void main() Hello Abhijeet Mishra
{
char n1[9], n2[9];
clrscr();
printf("Enter your first name: ");
scanf("%s",&n1);
printf("Enter your last name : ");
scanf("%s",&n2);
printf("Hello %s",strcat(n1,n2));
getch();
}
#include <stdio.h>
void functionName()
{
... .. ...
}
void main()
{
... .. ...
functionName();
... .. ...
}
#include <stdio.h>
void functionName()
{
... .. ...
}
void main()
{
... .. ...
functionName();
... .. ...
}
There can be four types of User defined Function:
• Function with no argument and no return value
• Function with argument and no return value
• Function with argument and with return value
• Function with no argument and with return value
Program for Function with no argument and no A program for Function with argument and with
return value: return value:
#include<stdio.h>
#include<conio.h> #include<stdio.h>
void sum() #include<conio.h>
{ int sum(int a,int b)
int n1,n2; {
printf("Enter any two Number: "); return a+b;
scanf("%d%d",&n1,&n2); }
int max(int a,int b)
printf("Total = %d", n1+n2); {
} if(a>b) return a; else if(b>a)
return b;
void main() }
{ void main()
clrscr(); {
sum(); clrscr();
getch(); int n1,n2,t=0;
} printf("Enter any two No:");
Program for Function with argument and no return scanf("%d%d",&n1,&n2);
value: t=sum(n1,n2);
#include<stdio.h> printf("Total = %d \n",t);
#include<conio.h> t=max(n1,n2);
void sum(int a,int b) printf("MAX NO Is = %d",t);
{ getch();
printf("Total = %d",a + b); }
}
A program for Function with No argument and with
void main() return value:
{
clrscr();
int n1,n2;
printf("Enter any two Number: ");
Do it your self ☺
scanf("%d%d",&n1,&n2);
sum(n1,n2);
getch();
}