C Programming Functions
Function Definition: Function is a group of statements which performs a specific task. It is also known as Subroutine or Procedure or Method.
There are two types of C functions:
 Library Functions (also known as System defined function)
 User defined functions (written by programmers)
Library Functions:
These are functions provided by the developer of C language. They are inbuilt with
the C language.
Examples of Library Functions:
 printf()
 scanf()
 getchar()
 sqrt()
 pow()
/*C program
to show use of sqrt() Library Functions.
This program
demonstrate use of two library function:
>
sqrt()
>
printf()
*/
#include<stdio.h>
#include<math.h>
void main()
{
float ans;
ans = sqrt(100);
printf("The square root of 100 is
%f.", ans);
}
//Output
The square
root of 100 is 10.000000.

User Defined Functions: The functions developed by the programmers
or users are known as User Defined Functions. The names given to user defined
functions must be different from the names of Library functions. Note that
main( ) is a special type of user defined function in C language.
Examples of User Defined Functions:
 FindAverage()
 DisplayMenu()
 DrawLine()
 GenerateReport()
 PrintLine()
/*C program
to show use of User Defined Functions.
This program
demonstrate use of FindAverage() function.
*/
#include<stdio.h>
void
FindAverage(); // function prototype declaration.
void main()
{
FindAverage(); //Call function.
}
void
FindAverage()
{
//function logic is written here..
float num1, num2;
printf("Enter number1: ");
scanf("%f",&num1);
printf("Enter number2: ");
scanf("%f",&num2);
printf("Average of two numbers:
%f.", (num1+num2)/2);
}
//Output
Enter
number1: 10
Enter
number2: 20
Average of
two numbers: 15.000000.

Explore following C programs based on function concept:
 Factorial of given number using recursive function.
 Use of builtin C string library function.
 C function with no arguments and no return value.
 C function with an argument and no return value.
 Square of given number using function with an argument and a return value.
No comments:
Post a Comment