Next>>
Functions
A Function in programming can be defined as a smaller program, which has it's own specific purpose and can be used to make large programs. A C program consists of one or more functions. Remember, Every C program must contain main() function.
Let's understand the function concept with the help of an example :
Suppose, We are writing a C program, which accepts three numbers from the user and prints the reverse of the numbers as output on the screen. Let's first write it without using function and then by using function.
#include<stdio.h>
void main() {
int num1, num2, num3, reverse = 0, rem ;
printf("Enter the three numbers : ");
scanf("%d%d%d",&num1,&num2, &num3) ;
/* Accepting the three numbers from the user */
/* Reverse Logic Starts Here */
/* Reverse of First Number */
while(num1>0){
rem = num1%10;
reverse = reverse*10 + rem;
num1 = num1/10;
}
printf("The reverse of First number is = %d\n",reverse);
/* Reverse of Second Number */
reverse = 0;
while(num2>0){
rem = num2%10;
reverse = reverse*10 + rem;
num2 = num2/10;
}
printf("The reverse of Second number is = %d\n",reverse);
/* Reverse of Third Number */
reverse = 0;
while(num3>0){
rem = num3%10;
reverse = reverse*10 + rem;
num3 = num3/10;
}
printf("The reverse of Third number is = %d\n",reverse);
}
The above program uses the same logic multiple times. This program can be made more simpler with the help of function. A Function is used to perform a repetitive task. Let's simplify the above code with the help of function.
#include<stdio.h>
void reverse(int n);
//Function declaration
void main() {
int num1, num2, num3;
printf("Enter the three numbers : ");
scanf("%d%d%d",&num1,&num2, &num3) ;
/* Accepting the three numbers from the user */
printf("The reverse of %d is = ",num1);
reverse(num1);
/* Function Call */
printf("The reverse of %d is = ",num2);
reverse(num2);
printf("The reverse of %d is = ",num3);
reverse(num3);
}
void reverse(int n) {
/* Function Definition */
int reverse = 0, rem;
while(n>0) {
rem = n%10;
reverse = reverse*10 + rem;
n = n/10;
}
printf("%d\n",reverse);
} /* Function Ends */
In the above program, we have used a function to output reverse of a number. Let's understand how a function is defined and used in programming.
C Language offers 2 Types of Functions :
1. Pre-Defined or Library Functions
2. User-Defined Functions
1. Pre-Defined or Library Functions
2. User-Defined Functions
1. Pre-Defined or Library Functions
Pre-Defined or Library functions are the functions, which are defined by the developers of C Language. These functions are present in the C Library and they are predefined. Examples of such functions include printf(), scanf(), sqrt(), strlen() etc. These functions are defined in particular files known as header files.
printf() and scanf() are input-output library functions, defined in header file "stdio.h" . sqrt() is a mathematical library function which is used for finding out the square root of any number, defined in "math.h". strlen() is a string function which is used to calculate the length of a string, defined in "string.h".
To use a library function we have to include the header file in which these functions are defined.
Note: A function is always followed by parentheses or brackets "( )".
Let's write a program using Pre-defined Function :
printf() and scanf() are input-output library functions, defined in header file "stdio.h" . sqrt() is a mathematical library function which is used for finding out the square root of any number, defined in "math.h". strlen() is a string function which is used to calculate the length of a string, defined in "string.h".
To use a library function we have to include the header file in which these functions are defined.
Note: A function is always followed by parentheses or brackets "( )".
Let's write a program using Pre-defined Function :
#include<stdio.h>
#include<math.h>
#include<math.h>
void main() {
double num, result;
printf("Enter a number : ");
scanf("%lf",&num);
result = sqrt(num);
printf("The Square root of %lf is %lf\n", num, result);
}
2. User-Defined Functions
User-Defined Functions are defined by the programmer or the user. It means user can create their own functions to perform specific tasks of any program. For Example - the reverse function which is given on the top of the page is a user defined function. Simply, Functions that you yourself create are called User-Defined Functions.
Let's learn to create a User-Defined Function :
Let's learn to create a User-Defined Function :
User-defined Functions can be used in 3 steps :-
(i) Function Declaration
(ii) Function Definition
(iii) Function Call
(i) Function Declaration
(ii) Function Definition
(iii) Function Call
(i) Function Declaration
Function Deceleration means you are telling the compiler, that I am creating a new Function. Function Deceleration involves providing the name to the, function giving it a return type and defining its parameters.Function Deceleration takes the following form :
return-type function-name(parameter-list) ;
return-type tells which type of value (void, int, float, double) is returned by a function after it's execution. "function-name" can be any valid identifier name in C. (parameter-list) defines the values supplied to a function and their types. Parameter-list contains variables which accepts the value given to the function. These variables which accept value are called Parameters.
For Example -
For Example -
void square (int number) ;
A function with name square is defined with void return-type and number of type int as parameter.
(ii) Function Definition
Function Definition defines what the function do. A Function can perform many tasks like - printing a statement, calculating a product, finding the square etc. A function definition consists of 2 parts - a function header and a function body. The general syntax of a function definition is -return-type function-name(parameter-list)
/* Function Header */
{ /* Function Starts here */
Function Statements
} /* Function ends here */
Let's define function, We declared earlier :
Our square function accepts a number of int type from the user and outputs its square value. As we know, the variables declared inside the parameter-list during the function declaration are called Parameters whereas the value provided to the parameters are called Arguments. So, Arguments are the values provided in the parenthesis during a function call. Our function has void return-type, which means our function will only execute statements inside it and does not return any value.
A Function can be defined anywhere in the program. But, generally all the definitions are placed after the main() function.
Let's see the Complete program that we have discussed above
void square (int number)
/* Function Header */
{ /* Function Starts here */
int result;
result = number*number;
printf("The Square of the given number is %d" ,result);
} /* Function ends here */
A Function can be defined anywhere in the program. But, generally all the definitions are placed after the main() function.
(iii) Function Call
Function Call or Calling a function means using the function in our program. A Function is called by simply writing its name followed by the argument inside the parentheses.
square(5);
"square" function is called and argument 5 is provided during the function call.
Let's see the Complete program that we have discussed above
#include<stdio.h>
void square(int number);
//Function declaration
void main() {
square(5);
square(8);
//Function Call
}
square(5);
square(8);
//Function Call
}
void square (int number)
/* Function Header */
{ /* Function Starts here */
int result;
result = number*number;
printf("The Square of the given number is %d\n" ,result);
} /* Function ends here */
Whenever a function is called in the main() function, then control is transferred to that particular function. Once the function ends, the control is again transferred to the next statement of the function call.
Let's make some more programs to understand the functions concept well.
<<Previous
Next>>
Post a Comment