C - SPLessons

Chapter 17

Functions in C

Functions in C

Functions in C can be defined as a partitioned blocks of code in a program, which can be called any number of times during single execution of a program. The information is passed to the functions by using arguments.

  • Functions reduces the memory and complexity of the program.
  • Functions in C mainly helps in re-usability.
  • Functions reduces the bugs and saves the time of programmer.
  • Information hiding is possible by functions.
  • Once the function execution is completed it returns to the position where it was called.

#include<stdio.h>
int main()
{
	message();
}

message()
{
	printf(" You are reading splessons ");
} 

Output:

You are reading splessons

Types of Functions in C

Functions in C Language are divided into two types.

  • Library Functions
  • User-defined Functions

Library Functions:

Library functions are built-in C-compiler.There are many library functions.Some of them are:
(i) main() : The execution starts from the main function
(ii) printf() : printf() function prints the output on the screen.
(iii) sacnf() : scanf() function reads the data given by the user.
(iv) getc() : getc() function gets the character value.

User-defined functions

User-defined functions are defined by the user as the requirement at the time of programming.
The user-defined functions can be accessed by using the following terms:

Function Prototype

Code can be divided into separate functions.The division will be in such a way that each function performs a specific task logically.

Function Declaration

Like variable declaration, every function in C need to be declared before it is used.The declaration should be done before the first call of the function.It gives the information about function name, return type and arguments of function.

Function declaration is also called as “function prototype”.

To be a prototype, function declaration need to have arguments.Although functions that return int values need not use prototype, but it is recommended.Function Body is not the part of function declaration.It is involved in function definition.

return_type function_name ( argument_list );

In the above syntax,

return_type : returns the data type of the function value.
function_name : This is function’s actual name.
argument_list : The argument list refers to the order,type and number of the parameters of a function.

Eg: int add(int a,int b)

Function Definition

Function Definition consists of all the code which needs to be executed. It contains Declarator ( enclosed in () ) and Body part ( enclosed in {}).

return_type function_name(argument list)
{
//body of function
}

In the above syntax, the body of the function has a group of statements that defines the functionality of function does.

int add( int a, int b)
{
	a++;
	b++;
	printf("sum=%d",a+b);
	printf("Welcome to splessons.com");
}

Function Arguments & Return Value

The variables that accept the arguments values can only be used in functions. These variables are also called as formal parameters of the function.

Inside of the function formal parameters behaves like other local variables and are created when entered into the function and destroys when exits the function.

Any Functions in C normally accepts the argument and it gives back a return value to the calling Program.So, there establishes two-way Communication between calling function and called function.

Function Call

There are two ways to pass the arguments while calling a function from a program.They are :

Call by value:

By default, all functions are passed by “call by value”. The value of the variable is passed as a parameter. Actual value cannot be modified by using the derived parameter i.e formal parameter.Both are given different memories.

#include<stdio.h>
#include<conio.h>
int swap(int , int); // Declaration of function
main( )
{
	int a = 10, b = 20 ; // call by value
	swap(a,b); // a and b are actual parameters
	printf ( "\n Before Swapping \n a = %d b = %d \n", a, b ) ;
	getch();
}
int swap( int x, int y ) // x and y are formal parameters
{
	int t ;
	t = x ;
	x = y ;
	y = t ;
	printf ("\n\nAfter swapping \n x = %d y = %d", x, y ) ;
}

Output:

After swapping
 x = 20 y = 10
 Before Swapping
 a = 10 b = 20

Call by reference:

By using call by reference,address of the variable is passed as a parameter. Actual parameter can be modified by using the derived parameter i.e formal parameter.Both are given same memories.

#include<stdio.h>
#include<conio.h>
int main( )
{
    int num1 = 35, num2 = 45 ;
    printf("Before swapping: num1 value is %d and num2 value is %d", num1, num2);

    /*calling swap function*/
    swapnum ( &num1, &num2 );

    printf("\nAfter swapping: num1 value is %d and num2 value is %d", num1, num2);
}

swapnum ( int *var1, int *var2 )
{
   int tempnum ;

   tempnum = *var1 ;
   *var1 = *var2 ;
   *var2 = tempnum ;
}

Output:

Before swapping: num1 value is 35 and num2 value is 45
After swapping: num1 value is 45 and num2 value is 35

Functions without parameters

Functions in C without parameters is nothing but the functions without arguments.There occurs two conditions of functions without parameters.

Function with no arguments and no return values

#include<stdio.h>
void area();  // Prototype Declaration
void main()
{
area();
}

void area()
{
    float area_circle;
    float rad;

    printf("\nEnter the radius : ");
    scanf("%f",&rad);

    area_circle = 3.14 * rad * rad ;

    printf("Area of Circle = %f",area_circle);
}

Output:

Enter the radius : 5
Area of Circle = 78.500000

Function with no arguments and return values

#include<stdio.h>

int sum();          

int main()
{
    int addition;
    addition = sum();                  
    printf("\nSum of two given values = %d", addition);
    return 0;
}

int sum()  
{ 
       int a = 50, b = 80, sum;   

       sum = a + b;
       return sum;       
}

Output:

Sum of two given values = 130

Functions with parameters

Functions in C with parameters is nothing but the functions with arguments.There occurs two conditions of functions with parameters.

Function with arguments and no return values

#include<stdio.h>
#include<conio.h>
//----------------------------------------
  void area(float rad);  // Prototype Declaration
//----------------------------------------
void main()
{
	float rad;
	   printf("\nEnter the radius : ");
	   scanf("%f",&rad);
	   area(rad);
	getch();
}
//----------------------------------------
void area(float rad)
{
	float ar;
	ar = 3.14 * rad * rad ;
	printf("Area of Circle = %f",ar);
}

Output:

Enter the radius : 5
Area of Circle = 78.500000

Function with arguments and return values

#include<stdio.h>
float calculate_area(int);
int main()
{
    int radius;
    float area;
    printf("\nEnter the radius of the circle : ");
    scanf("%d",&radius);
    area = calculate_area(radius);
    printf("\nArea of Circle : %f ",area);
    return(0);
}
float calculate_area(int radius)
{
    float areaOfCircle;
    areaOfCircle = 3.14 * radius * radius;
    return(areaOfCircle);
}

Output:

Enter the radius of the circle : 8

Area of Circle : 200.960007

Summary

  • A re-usability block function may or may-not possess arguments but can be called whenever required.
  • Function can be called by value or by reference.