Functions in C

Functions in C Programming Language: A function is a group of statements that perform a particular task. And  C functions are the basic building blocks of a program. These functions are enclosed in “{}”. A C function deceleration tells the compiler about a function name, return type and parameters. And also function definition provides the actual body of the compiler.

Syntax
return_type function_name( parameter list ) {
body of the function }

C Functions

Uses of  C Functions

  • C Functions provide modularity
  • They provide re-usability of code
  • The program can be modularized into smaller parts
  • C programs avoid writing the same logic again and again in a program.
  • We can call functions any no.of times from anywhere in the program.

Types of Functions in C

There are two types of C functions. They are as follows:



1. Built-in library functions
2. User-defined functions

1. C  Built-in Functions

The system provides some default functions which are already stored in functions. They are also known as C library functions.

Example

scanf( ),printf( ),strcpy,strcmp,strlen etc.;

2. C  User defined functions

These functions are defined by the user while writing a program for their use.

Parts of a Function

A function can be defined by 3 aspects. They are as follows:

1) Function Declaration (or) prototype
2) Function definition
3) Function Call

i) C Function Declaration

A function declaration tells the compiler about the function name and how to call a function.

Syntax: return_type function_name( parameter list ); 

Here, parameter names are not important in a function declaration, only their type is required.

ii) C Function definition

A function definition in C contains all the statements that are to be executed.

Syntax

Return_type function_name (arguments list)  {                  
Body of function;} 

Function Definition in c

iii) C Function Call

It calls the actual function. While the program calls a function the program control is transferred to the called function.

Syntax: function_name (arguments list); 

Example

#include<stdio.h> //function prototype, also called function declaration 
float square(float x );//main 
function
int main( )   { 
float m, n ; 
printf("\nEnter some number for finding square \n"); 
scanf("%f",&m); //function call 
n = square(m);                       
printf "\nSquare of the given number %f is %f",m,n); 
} 
float square (float x) //function definition 
{ 
float p ;
p = x * x ; 
return ( p ); 
}

Output
Enter some number for finding square 2
Square of the given number 2.000000 is 4.000000

C Function Arguments

C Function arguments are nothing but, while calling a function the arguments can be passed to a function in two ways. They are call by value and call by reference.

i) C Call by Value

In C call by value method, the value of the variable is passed to the function as a parameter.

  • The value of the actual parameter cannot be modified by the formal parameter.
  • Different memory is allocated for both actual and formal parameters. Because the value of the actual parameter is copied to the formal parameters.

Note

  • Actual parameter: an argument which is used in the function call.
  • Formal parameter: the argument which is used in a function definition.

Example: a program for a call by value

#include<stdio.h>//function prototype, also called function declaration 
void swap(int a, int b);            
int main()  { 
int m = 22, n = 44;//calling swap function by value 
printf(" values before swap  m = %d \n and n = %d",m,n); 
swap(m, n);   }  
void swap(int a, int b) {    
int tmp;   
tmp = a;    
a = b;   
b = tmp; 
printf("\nvalues after swap m = %d\n and n = %d",a, b);

Output
values before swap m = 22  and n = 44
values after swap m = 44  and n = 22

ii) C Call by Reference

In C call by reference method, the address of the variable is passed to the function as parameters.

  • While the value of the actual parameter can be modified by formal parameters.
  • And the same memory is used for both actual and formal parameters since an only address is used both parameters.

Example

#include<stdio.h>//function prototype, also called function declaration       void swap(int *a, int *b);                  
int main()  {                   
int m = 22, n = 44;//calling swap function by reference                 
printf("values before swap m = %d \n and n = %d",m,n);                   
swap(&m, &n);   }            
void swap(int *a, int *b)  {           
int tmp;           
tmp = *a;           
*a = *b;          
*b = tmp;         
printf("\n values after swap a = %d \n and b = %d", *a, *b);      
 }

C arguments and return values

All the functions in C can be called either with arguments in a C program. These functions may or may not return values to the calling function. And there are different types of return values in C. They are as follows:

  1. C with arguments and return value.
  2. with arguments and without return values.
  3. without arguments and without return values.
  4. without arguments and with return values.

i) C With arguments and return value

Syntax
function declaration:
int function (int);
function call: function (a);
function definition:
int function( int a )   {
statements;
return a;
}

ii) C with arguments and without return values

Syntax
function declaration:
void function (int);
function call: function( a );
function definition:
void function( int a )    {
statements;
}

iii) C without arguments and without return values

Syntax
function declaration:void function();
function call: function();
function definition:void function() {
statements;  }

iv) C without arguments and with return values

Syntax
function declaration:int function ( );
function call: function ( );
function definition:int function( ){
statements;
return a; }

Note: 

  • If the return data type of a function is “void”, then it can’t return any values to the calling function.
  • If the return data type of the function is other than void such as “int, float, double” etc.; then it can return values to the calling function.

Example

#include<stdio.h>             
#include<string.h>             
int function(int, int[], char[]);              
int main()  {                 
int i, a = 20;                 
int arr[5] = {10,20,30,40,50};                 
char str[30] = "\"freshersnow\"";             
printf("***values before modification***\n");                 
printf("value of a is %d\n",a);               
for(i=0;i<5;i++) { 
printf("value of arr[%d] is %d\n",i,arr[i]);//Accessing each variable       
} printf("value of str is %s\n",str);        
printf("\n***values after modification***\n");        
a= function(a, &arr[0], &str[0]);       
printf("value of a is %d\n",a);         
for (i=0;i<5;i++) {  
printf("value of arr[%d] is %d\n",i,arr[i]);         
}  
printf("value of str is %s\n",str);        
return 0; }        
int function(int a, int *arr, char *str) {           
int i;           
a = a+20;           
arr[0] = arr[0]+50;           
arr[1] = arr[1]+50;           
arr[2] = arr[2]+50;           
arr[3] = arr[3]+50;           
arr[4] = arr[4]+50;         
strcpy(str,"\"modified string\"");          
return a; }

Output
***values before modification***
value of a is 20
value of arr[0] is 10
value of arr[1] is 20
value of arr[2] is 30
value of arr[3] is 40
value of arr[4] is 50

value of str is “freshersnow”***values after modification***

value of a is 40
value of arr[0] is 60
value of arr[1] is 70
value of arr[2] is 80
value of arr[3] is 90
value of arr[4] is 100
value of str is “modified string”