A function is a block of code that performs a specific task.The function contains the set of programming statements enclosed by {}. A function can be called multiple times to provide reusability and modularity to the C program. In other words, we can say that the collection of functions creates a program. The function is also known as procedureor subroutinein other programming languages.
Advantage of functions in C
There are the following advantages of C functions.
a) To improve the readability of code.
b) Improves the reusability of the code, same function can be used in any program rather than writing the same code from scratch.
c) Debugging of the code would be easier if you use functions, as errors are easy to be traced.
d) Reduces the size of the code, duplicate set of statements are replaced by function calls.
Function Parts
There are three parts of a C function.
- Function declaration:- A function must be declared globally in a c program to tell the compiler about the function name, function parameters, and return type.
- Function call:- Function can be called from anywhere in the program. The parameter list must not differ in function calling and function declaration. We must pass the same number of functions as it is declared in the function declaration.
- Function definition :- It contains the actual statements which are to be executed. It is the most important aspect to which the control comes when the function is called. Here, we must notice that only one value can be returned from the function.
Syntax:-
return_type function_name (argument list)
{
Set of statements – Block of code
}
1. Types of function
There are two types of function in C programming:
Standard library function
User-defined function
1). Standard library function:-
Standard library functions are also known as built-in functions. Functions such as
For example,
puts(), gets(), printf(), scanf() etc are standard library functions. These functions are already defined in header files (files with .h extensions are called header files such as stdio.h), so we just call them whenever there is a need to use them.For example,
printf() function is defined in <stdio.h> header file so in order to use the printf() function, we need to include the <stdio.h> header file in our program using #include <stdio.h>.2) User Defined functions
The functions that we create in a program are known as user defined functions or in other words you can say that a function created by user is known as user defined function.
Syntax:-
return_type function_name (argument list)
{
Set of statements – Block of code
}
Example:-
#include <stdio.h>
int max(int num1, int num2); /* function declaration */
int main () {
/* local variable definition */
int a = 100;
int b = 200;
int ret;
ret = max(a, b); /* calling a function to get max value */
printf( "Max value is : %d\n", ret );
return 0;
}
/* function returning the max between two numbers */
int max(int num1, int num2) {
int result; /* local variable declaration */
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
Function Arguments
If a function is to use arguments, it must declare variables that accept the values of the arguments. These variables are called the formal parameters of the function.
1. Call by value:- This method copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument.
2. Call by reference:- This method copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. This means that changes made to the parameter affect the argument.
0 Comments