What are function in c programming language with suitable examples


Function in c programming language

C Function is a set of statements. There is a function in c programming language every single program. For example - main () Function is written even when asked in the program. Where Function is required, Function is called.

Benefits of Function:

  • The code written in the Function does not have to be written repeatedly.
  • Function protects programmer's time and program space.
  • Large programs can be divided into small functions.
  • If there is an error in the program, then it can be easily removed.
  • The function can be called repeatedly where needed.
The function has a specific name. At the beginning of the Function, its name is followed by two curly braces of two parentheses () and function statements.

There are two types of function.

  • In-built / Predefined Function
  • User-defined Function


1. In-built / Predefined Function

  • In-built functions are also called predefined or Library Functions.
  • Different header file or preprocessor has been created for each function in In-built Functions.
  • Function declaration occurs in definition header files.
  • There are so many header files in C that they are grouped by different functions.
  • If a programmer wants to create their own header files then he can do that.

for Example

  • printf() : This Function comes under stdio.h this header file. If the programmer #include <stdio.h> does not include this preprocessor program then cannot use printf.
  • strlen() : This function belongs to string.h under this header file. If programmer #include <string.h> does not include this preprocessor program, then strlen cannot be used.

2. User-defined Function

User-defined Function has three sections.
  1. Function Declaration
  2. Function Calling
  3. Function Definition


1. Function Declaration

While creating a function, the compiler has to tell you how to create a Function, this process is called Function Declaration.
Syntax for Function Declaration
return_type function_name(parameter(s));
Function Declaration has four divisions.
  1. return_type
  2. function_name
  3. parameter(s)/argument(s)
  4. semicolon

1. return_type

Every Function returns any value, whether it is null or numeric (int, float, double) or character (char). The return_type of the Function void is the default function of the return_type program.

2. function_name

The name of the function should be according to its code. If not, the compiler runs, but this is not called Good Programming. Function name C does not have any keyword. The functions of C are also inside the parenthesis (). The name of the function is case-sensitive. for eg hello () and Hello () are both different functions.

3. parameter(s)/argument(s)

Function's arguments are data_types or with their data type, their names are variable. What type of user wants to get through the value calling function, it is declared in the argument of the function.

4. semicolon

After each Declaration of Function, a semicolon is given. This is also part of the function declaration.
Example for Function Declaration with two parameters
int add(int a,int b);    // function declaration
Function Declaration without parameter(s)
int add();
Function Declaration with one parameter
int add(int a);


2. Function Calling

Syntax for Function Calling
function_name(Parameter1 ,Parameter2 ,.Parameter n);

Function Calling is just the function name, function's arguments and semicolon. The return type of the given example function is 'integer'. The name of the function is 'add' and the function has two parameters, a and b passed unless the function is called, the function declaration and definition is of no importance.
Example for Function calling with two parameters
add(a, b);  // funtion callling
Function calling without parameter(s)
add();
Function calling with one parameter
add( a );



3. Function Definition

Syntax for Function Definition
return_type function_name(Parameter(s)){

   function_body;

}



Read more:- 


Function Definition has four parts.

1. return_type

Every Function returns any value, whether it is null or numeric (int, float, double) or character (char). The return_type of the Function void is the default function of the return_type program.

2. function_name

The name of the function should be according to its code. If not, the compiler runs, but this is not called Good Programming. Function name C does not have any keyword. The functions of C are also inside the parenthesis (). The name of the function is case-sensitive. for eg hello () and Hello () are both different functions.

3. parameter(s)/argument(s)

Function's arguments are data_types or with their data type, their names are variable. What type of user wants to get through the value calling function, it is declared in the argument of the function.

4. function_body

There are variables in the Function body, but due to being inside the function, their scope is Local. Inside the body there are some statements (s) and the value returns.

Example for Function Definition

int add(int x,int y){   // function definition

int z;
z = x + y ;

return z;
}


Full Example for Function

#include <stdio.h>

int add(int a,int b);  // function declaration with argument

int main(){

int a,b,c;
printf("Enter value of a and b : ");
scanf("%d %d", &a, &b);

c = add(a,b);   // funtion calling

printf("Addition of a and b : %d", c);

return 0;
}

int add(int x,int y){   // function definition

int z;
z = x + y ;

return z;
}


Call By Value and Call By Reference Before Understanding Parameters
There are two types of Parameters in the Function.
  1. Formal Parameter
  2. Actual Parameter

Formal Parameter

Formal Parameter, which is written in the declaration of parameter function and in the definition.
for eg.
void swap(int x, int y); // Formal Parameters
int main(){
int a=2; b=10
swap (a, b)
}
void swap (int x, int y){    //Formal Parameters
------------
------------
}

The parameter that is written in the function call is called Actual Parameter.
for eg.
void swap(int x, int y);
int main(){
int a=2; b=10
swap (a, b)    //Actual Parameter
}
void swap (int x, int y){
------------
------------
}


There are two types of Function Calling.

  1. Call By Value
  2. Call By Reference


1. Call By Value

  • In Call By Value, the value of the variable is passed to the function as a parameter.
  • The value of Actual Parameter in Call By Value is copied to the Formal Parameter.

Here, the variable 'a' and 'b' function 'add' are passed in the program.

Here the values of 'a' and 'b' are copied to 'x' and 'y' variable.
for eg.
#include <stdio.h>

void swap(int x, int y);

int main(){
 
int a = 2, b = 10;
 printf("Before Swapping  a = %d and b = %d\n", a, b);
 swap(a, b);
}
void swap(int x, int y)
{
int temp;
 temp = x;
 x = y;
 y = temp;
 printf("After Swapping a = %d and b = %d", x, y);
}


2. Call By Reference

  • In the Call By Reference, the address of the variable is passed to the function as a parameter.
  • The value of Actual Parameter in Call By Value is not copied to the Formal Parameter.

Here the variable 'a' and 'b' address in the program are passed to the function 'add'.

Here the values of 'a' and 'b' do not copulate on 'x' and 'y' variable.

It just holds the address of the variables.
for eg.
#include <stdio.h>

void swap(int *x, int *y);

int main(){
 
int a = 2, b = 10;
 printf("Before Swapping  a = %d and b = %d\n", a, b);
 swap(&a, &b);
}
void swap(int *x, int *y)
{
int temp;
 temp = *x;
 *x = *y;
 *y = temp;
 printf("After Swapping a = %d and b = %d", *x, *y);
}

Read more:- 

Comments