In C programming, functions are categorized into two sections: library functions provided by the language and custom user-defined functions.
Library/ Built-in Function
A library function comprises preexisting functions whose operations are outlined in the C header files. Therefore, developers do not need to manually write the code for these specific functions; rather, they can simply invoke them within a program as needed. For instance, functions like printf, scanf, getch, among others, are part of the predefined functions within the C library, and their functionalities remain constant and cannot be altered.
Let's create a program to specify the library function in the C programming language.
Predefined.c
# include <stdio.h> // Contains the printf, scanf() built-in function
#include <conio.h>
void main()
{
printf( " Welcome to the Logic Practice.com ");
printf("\n It is the library or built-in function in C ");
getch(); // built-in function
}
Output:
Welcome to the Logic Practice.com
It is the library function or built-in function in C
User-defined Function
A user-defined function in the C programming language carries out specific tasks based on the programmer's needs. This type of function is categorized into three parts: function declaration, function definition, and function invocation.
Function Declaration
A function declaration establishes the function's name and return type within a program. To utilize the function, it must be declared outside of the main function within the program.
Syntax:
return_data_type function_name ( data_type arguments) ;
Example of function declaration:
int add ( int num1, int num2 );
In the given illustration, int serves as the return data type for the function named add function, which accepts two integer parameters, num1 and num2. Additionally, the function declaration can be composed as shown below:
int add (int, int);
Function Definition
It specifies the functional implementation within a program to carry out specific operations in the C language.
Syntax:
return_data_type function_name (argument list ) {
// Statement to be executed in a function;
}
In the above syntax, a function definition contains the three parts as follow:
- Return Data_Type: It defines the return data type of a value in the function. The return data type can be integer, float, character, etc.
- Function Name: It defines the actual name of a function that contains some parameters.
- Parameters/ Arguments: It is a parameter that passed inside the function name of a program. Parameters can be any type, order, and the number of parameters.
- Function Body: It is the collection of the statements to be executed for performing the specific tasks in a function.
Consider an illustration to showcase the definition of a function:
int min (int n1, int n2); // min() is the name of a function that contains n1 and n2 parameters
{
// declaration of the local variable.
int out;
if ( n1 > n2)
out = n1; // return n1 when n1 is greater than n2.
else
out = n2; // return n2 when n2 is greater than n1.
return out;
}
Function Calling:
A function invocation plays a crucial role in the C programming language. It is invoked within a program whenever there is a need to execute a function. It is specifically invoked by its name within the main function of a program. Arguments can be transmitted to a function invocation within the main function.
Syntax:
Add(a, b) // a and b are the parameters
Let's explore a scenario where a program invokes a function in the C programming language.
Add.c
#include <stdio.h>
int add(int a, int b);
void main()
{
int sum;
int a, b;
printf(" Enter the first and second number \n");
scanf("%d %d", &a, &b);
sum = add(a, b); // call add() function
printf( "The sum of the two number is %d", sum);
}
int add(int n1, int n2) // pass n1 and n2 parameter
{
int c;
c = n1 + n2;
return c;
}
Output:
Enter the first and second number
5
6
The sum of the two number is 11
Call by Value:
When a function copies single or multiple actual argument values into its formal parameter, this method is known as Call by Value. As a result, it does not modify the function's actual parameter through the formal parameter.
Consider a program showcasing the Call by Value concept in the C programming language.
Call_Value.c
#include <stdio.h>
int main()
{
int x = 10, y = 20;
printf (" x = %d, y = %d from main before calling the function", x, y);
CallValue(x, y);
printf( "\n x = %d, y = %d from main after calling the function", x, y);
}
int CallValue( int x, int y)
{
x = x + 5;
y = y + 5;
printf (" \nx = %d, y = %d from modular function", x, y);
}
Output:
x = 10, y = 20 from main before calling the function
x = 15, y = 25 from modular function
x = 10, y = 20 from main after calling the function
Call by Reference:
In this technique, the function call's formal parameter receives a copy of the actual argument's address; this approach is referred to as Call by Reference. Modifying the formal parameters directly impacts the value of the actual parameter.
Consider a program showcasing the concept of Call by Reference in the C programming language.
Call_Ref.c
#include <stdio.h>
int main()
{
int x = 10, y = 20;
printf (" x = %d, y = %d from main before calling the function", x, y);
CallValue (&x, &y);
printf( "\n x = %d, y = %d from main after calling the function", x, y);
}
int CallRef( int *a, int *b)
{
*a = *a + 5;
*b = *b + 5;
printf (" \nx = %d, y = %d from modular function", *a, *b);
}
Output:
x = 10, y = 20 from main before calling the function
x = 15, y = 25 from modular function
x = 15, y = 25 from main after calling the function