Function in C programming

Function in C programming

Function in C programming


A function in C programming is defined as a self-contain block of statements that perform a selected task or job. A C program consists of one or more functions. this can be a logical unit composed of a spread of statement groups into one unit. Execution of every C program begins with main(). Additional functions are getting to be subordinate to main(), and maybe to at least one another. After the finished each function its operation, the control returns back to the main(). Then remaining statements of main() are executed

If a program contains multiple functions, their definitions may appear in any order, through they have to be independent of one after another.

A function in C programming will perform its intended action whenever it's accessed from some portion of the program. the same function is usually accessed from several different places within a program. Once the function has administered its intended action control goes to be returned to the purpose from which the function was accessed.

                          Function in C

Advantage of function in C programming

(1). Function increases code reusability.
(2). Program development is going to be faster.
(3). Program development are going to be easier.
(4). The length of the program will be reduced by using functions at appropriate places.
(5). The program debugging are going to be faster.
(6). The function reduces program complexity
(7). The program can be developed in different places.
(8). The program can be teased and compiled independently by different places
(9). The recursive call is possible through function

Types of function in C Programming 

C function used two types of function these are

Library function

Library function: Library function are those function which is already defined, compiled and placed in C library and they are not required to be written by a programmer. The function name, return type, arguments, and types have been already defined. These function required header file such as #include<stdio.h>, #include<conio.h>, #include<math.h>, #include<string.h> etc.

Example of library function in C programming is 

printf(), scanf(), gets(), puts(), sqrt(), pow(), strlen(), strcpy(), strcat(), strrev(), strupr(), strlwr(), strcmp() etc.

Example

C program to find the square root of any number

#include<stdio.h>

#include<conio.h>

#include<math.h>

void main()

{

   int n, a;

   printf("\n Enter the number");

   scanf("%d", &n);

   a = sqrt(n);

   printf("\n Square root of %d is %d", n, a);

   getch();    

}

Output
Square root of any number in C

C program using string function

Some header files use in C programming that are used to handle library function.

s. n.

Header File

Description

1

stdio.h

stdio.h is a standard input/output function which used to take input from users through and display the result. It contains all the library function regarding to the input/output function such as printf(), scanf();

2

conio.h

conio.h header file for console input/output header file, Which included in the program this is used to use, clrscr(), getch(), etc

3

math.h

This header file is used for mathematical functions such as pow(), sqrt(), sin(), tan(), cos() etc.

4

string.h

It is a string header file. It contains the function definition of string processing functions such as strlen(), strcart(), strcpy() etc.

5

stdlib.h

malloc(), calloc(), exist() etc. general library functions are contained by the header file stdlib.h

6

time.h

time.h is used to use all time related functions

7

assert.h

All diagnostic functions operates through assert.h header file  

8

errno.h

These header files contain error handling function

9

complex.h

All complex numbers manipulation library functions are contained by comple.h

10

signal.h

All signal handling functions are defined by signal.h

11

locale.h

It defines all localization c library function

12

signal.h

signal.h contains all signal handling functions

13

ctype.h

ctype.h contains all character handling functions

14

concale.h

Character Handling Functions are defined under concale.h

15

tgmath.h

Type-generic mathematical functions

 

User define function in C programming

User define function in C programming:- These are functions that are defined by users at the time of writing a program. The user has the choice to choose its name, return type, arguments, and types. These functions need function prototype to use. 

Examples of user define function in C programming

Area(), evenodd(), si() etc

Difference between library and user defined function

Click here

In this article, now discuss user define function

Components of functions in c programming

Components of function in C Programming

(1). Function prototype.

(2). Function definition.

(4). Call the function. 

 

Function prototype in C programming.

Function prototype in C programming provides the following information to the computer

(1). The type of the value return.

(2). The name of the function

(3). The number and the type of the arguments that must be supplied in a function call.

syntax 

ruturn_value function_name(arg1, arg2..........argn)

example

int sum(int,int):

In this example two arguments int types are passed inside a function name sum and it returns int value. Prototype only needed if function definition comes after use in a program. They are generally written at the beginning of a program, ahead of the main() function.

Function definition.

   - A function definition has two principal components

   - Function header or function declarator

   - Body of the function

The first line of a function definition is known as function declarator and is followed by the function body. Function declarator contains the value returned by the function followed by the function name and optionally a set of arguments, separated by commas and enclosed in parentheses. Each argument is preceded by its associated type declaration. An empty pair of parentheses must follow the function name if the function definition does not include any arguments.

Syntax

data_type function_name(data_type arg1, data_type arg2..............data_type argn)

Where data_type represents the data type of the item that is returned by the function, function_name represents the function name, and arg1, arg2, arg3.............argn are variables for arguments.

Syntax of function definition in C


Called the function of function call

A function called is specified by the function name followed by the arguments enclosed in parenthesis and terminated by a semicolon

Syntax

variable = function_name(arg1,arg2............,argn);

or function _name(arg1,arg2,...........,argn);

where passing arguments arg1,  arg2, arg3...............are optional.

Example

(i) c=sum(a,b);

(ii) area(r);


Calling function,                          called function

main ()                                       //calling function

{-------------------------------

function ();

-----------------------------------    //function call

}

function ()                                      //called function(body of the function)

{-------------------------------------

Statements

--------------------------------------

}

In the above,  example the function () is called by the main () function and the code of the function (body of the function) is written after the main() function

Component of the function

Component of a function  

Syntax

Description

Function

Prototype

return_value function name (arg1,arg2………….argn)

Function prototype provides the type of the value return, the name of the function, the name and the type of the arguments that must be supplied in a function call

Function

definition

data_type function name(data_type arg1, data_type

arg2…………data_type argn)

Function definition has two principle

       Function header or function declaratory

       Body of the function

 

Function

call

Variable=function_name(arg1,arg2…….argn);

Or

Function_name(arg1,arg2……argn);

A function call is specified by the function name followed by the arguments enclosed in parenthesis and terminated by a semicolon.

 Categories of User – define function

·         1.  Function returning value and passing arguments

·         2. Function returning no value but passing arguments

·          3.  Function returning value and passing no arguments

  4. Function returning value and passing arguments

  Function returning value and passing arguments

Here an argument is passed from the calling function to the called function and there will be return statement in the called function

 Example

#include<stdio.h>

#include<conio.h>

int sum(int, int); // function prototype

void main()

{

          int a, b, s;

          printf("\n Enter two numbers");

          scanf("%d%d", &a, &b);

          s=sum(a,b);   // function call

          printf("\n sum=%d", s);

}

int sum(int a, int b)

{

          int c;

          c = a+b;

          return(c);

}

 

Output

Function returning and argument


Function returning no value but passing arguments

Here, an argument is passed from the calling function to the called function but there is no need for a return statement in the called function.

Example

#include<stdio.h>

#include<conio.h>

void sum(int, int);  // function prototype

void main()

{

          int a, b, s;

          printf("\n Enter two numbers");

          scanf("%d%d", &a, &b);

          sum(a,b);   // function called

}

void sum(int a, int b)

{

          int c;

          c = a+b;

          printf("\n Sum is %d",c);

}

Output

Without function returning but arguments



Function returning value and no arguments

Here, an argument is not passed from the calling function to the called function but there is return statement in the called function

Example 

#include<stdio.h> 

int sum(); 

void main() 

    int f;  

    f = sum(); 

    printf("\n sum is %d",f); 

int sum() 

    int a,b, c;  

    printf("\nEnter two numbers"); 

    scanf("%d %d",&a,&b); 

    c = a+b;  

    return(c);

}

 Output

Function without argument but returning value


 Function returning no value and passing no arguments:

Here, an argument is not passed from the calling function to the called function and there is no return statement in the called function

Example

#include<stdio.h>

#include<conio.h>

void sum(); 

void main () 

   sum(); 

void sum() 

    int a, b, c;

    printf("\n Enter two numbers");

          scanf("%d%d", &a, &b);

          c = a+b;

          printf("\n Sum is %d", c); 

}

Output

Without returning value and argument


Example

 C program to print area of rectangle using user define function

#include<stdio.h>

#include<conio.h>

void A(float,float);                          //function prototype

void main()                                     //calling function

{

            float l, b;

            printf("\n Enter length and breath");

            scanf("%f%f", &l, &b);

            A(l,b);                             //call function 

            getch();

}

void A(float l,float b)             //function declarator

{

            float area;                                               function body

            area=l*b;

            printf("\n Area=%f sq units",area);

}  

Output

Area of rectangle in C

Also check
Recursive function in C
Click here

Function with Arrays in C

Like the values of simple variables, it is also possible to pass the value of an array to a function. To pass an array to a called function, it is sufficient to list the name of the array, without any subscripts and the size of the array as arguments

Example

Write a C program to input 5 numbers and calculate their sum using array and function

#include<stdio.h>

#include<conio.h>

int sum(int a[]);

void main()

{

          int a[5], f, i;

          for(i=1;i<=5;i++)

          {

                   printf("\n Enter number");

                   scanf("%d",&a[i]);

          }

          f=sum(a);

          printf("\n sum is %d", f);

}

int sum(int a[])

{

          int c=0, i;

          for(i=1;i<=5;i++)

          {

                   c=c+a[i];

          }

          return(c);

         

}

Output

Sum using function and array


Write a C Program to find the length of a string using user define function

#include<stdio.h>

#include<conio.h>

int strlen(char str[]);

int main()

{

          char str[100];

          printf("\n Enter a string");

          gets(str);

          printf("\n The length of string is %d", strlen(str));

}

int strlen(char str[])

{

          int i;

          for(i=0;str[i]!='\0';i++)

          return(i);

}

 Output




Post a Comment

0 Comments