0% found this document useful (0 votes)
3 views

Programming C Function

The document explains the concept of functions in C programming, highlighting their role in modularity and reusability. It describes two types of functions: library functions, which are built-in and defined in header files, and user-defined functions, which are created by the programmer. Examples of both types are provided, including mathematical and string library functions, as well as various user-defined function types.

Uploaded by

rahulkumarmgs224
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)
3 views

Programming C Function

The document explains the concept of functions in C programming, highlighting their role in modularity and reusability. It describes two types of functions: library functions, which are built-in and defined in header files, and user-defined functions, which are created by the programmer. Examples of both types are provided, including mathematical and string library functions, as well as various user-defined function types.

Uploaded by

rahulkumarmgs224
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
You are on page 1/ 3

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)

#include <stdio.h> Enter any number 36


#include <math.h>
int main() Square root of 36 is 6
{
float num, root;
printf("Enter a number: ");
scanf("%f", &num);

root = sqrt(num);

printf("Square root of %.2f is %.2f", num, root);


return 0;
}

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();
}

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. Syntax is:

#include <stdio.h>
void functionName()
{
... .. ...
}

void main()
{
... .. ...
functionName();
... .. ...
}

There can be four types of User defined Function: (Next Class)


3. USER DEFINED FUNCTION:
You can also create functions as per your need. Such functions created by the user are known as user-defined
functions. Syntax is:

#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();
}

You might also like