- System Defined/Library Functions
- User Defined Functions
Library Functions: are the functions which are declared in the C header files such as scanf(), printf(), gets(), puts(), ceil(), floor() etc.The system defined functions are also called as Library Functions or Standard Functions or Pre-Defined Functions.
Whenever we use system defined functions in the program, we must include the respective header file using #include statement. For example, if we use a system defined function sqrt() in the program, we must include the header file called math.h because the function sqrt() is defined in math.h.
User-defined functions: are the functions which are created by the C programmer, so that he/she can use it many times. It reduces the complexity of a big program and optimizes the code.
Example without return value
void hello()
{
printf("hello c");
}
Example with return value
int get()
{
return 10;
}
In C every user defined function must be declared and implemented.
Whenever we make function call the function definition gets executed.
For example, consider the following program in which we create a
funcion called addition with two parameters and a return value.
Example
#include<stdio.h>
#include<conio.h>
void main(){
int num1, num2, result ;
int addition(int,int) ; // function declaration
clrscr() ;
printf("Enter any two integer numbers : ") ;
scanf("%d%d", &num1, &num2);
result = addition(num1, num2) ; // function call
printf("SUM = %d", result);
getch() ;
}
int addition(int a, int b) // function definition
{
return a+b ;
}
In the above example program, the function declaration statement "int addition(int,int)" tells the compiler that there is a function with name addition which takes two integer values as parameters and returns an integer value. The function call statement takes the execution control to the additon() definition along with values of num1 and num2. Then function definition executes the code written inside it and comes back to the function call along with return value.
- function without arguments and without return value
- function without arguments and with return value
- function with arguments and without return value
- function with arguments and with return value
Example
#include<stdio.h>
#include<conio.h>
void addition() ; // function declaration
void main(){
addition() ; // function call
getch() ;
}
void addition() // function definition
{
int num1, num2 ;
printf("Enter any two integer numbers : ") ;
scanf("%d%d", &num1, &num2);
printf("Sum = %d", num1+num2 ) ;
}
Example
#include<stdio.h>
#include<conio.h>
int addition() ; // function declaration
void main(){
int result ;
result = addition() ; // function call
printf("Sum = %d", result) ;
getch() ;
}
int addition() // function definition
{
int num1, num2 ;
printf("Enter any two integer numbers : ") ;
scanf("%d%d", &num1, &num2);
return (num1+num2) ;
}
Example
#include<stdio.h>
#include<conio.h>
void addition(int, int) ; // function declaration
void main(){
int num1, num2 ;
printf("Enter any two integer numbers : ") ;
scanf("%d%d", &num1, &num2);
addition(num1, num2) ; // function call
getch() ;
}
void addition(int a, int b) // function definition
{
printf("Sum = %d", a+b ) ;
}
Function with arguments and with return value
Example
#include<stdio.h>
#include<conio.h>
int addition(int, int) ; // function declaration
void main(){
int num1, num2, result ;
printf("Enter any two integer numbers : ") ;
scanf("%d%d", &num1, &num2);
result = addition(num1, num2) ; // function call
printf("Sum = %d", result) ;
getch() ;
}
int addition(int a, int b) // function definition
{
return (a+b) ;
}
Notes
- The parameters specified in calling function are said to be Actual Parameters.
- The parameters declared in called function are said to be Formal Parameters.
- The value of actual parameters is always copied into formal parameters.
No comments:
Post a Comment