C Programming Functions
Function Definition: Function is a group of statements which performs a specific task. It is also known as Sub-routine 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 in-built with the C language.
Examples of Library Functions:
/*C program to show use of sqrt() Library Functions.
This program demonstrate use of two library function:
ans = sqrt(100);
printf("The square root of 100 is %f.", ans);
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:
/*C program to show use of User Defined Functions.
This program demonstrate use of FindAverage() function.
void FindAverage(); // function prototype declaration.
FindAverage(); //Call function.
//function logic is written here..
float num1, num2;
printf("Enter number1: ");
printf("Enter number2: ");
printf("Average of two numbers: %f.", (num1+num2)/2);
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 built-in 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.