Functions

Function is a group of statement that perform together in a task. In every C program has at least one function which is main(). Function can be called multiple times to provide molecularity and reusability to the C program. We can use the functions in the program then the debugging of the code would be easier and errors are easy to be traced.

There are three phase of C functions

  • Function declaration
  • Function definition
  • Function Call
  • Function declaration: A function declaration is a statement describing an identifier such as the name of the variable or a function. In C program function must be declared globally and to tell the compiler about the function name,function parameter and return type.

    Syntax :

    return_type function_name(argument list);
  • Function definition: In function definition it contains the actual statements which are to be executed. Only one value can be returned from the function.It is important phase to which the control comes when the function is called.

    Syntax :

    return_type function_name(argument_list)
    {
    function body;
    }
  • Function Call: You can call the function anywhere in the program. The parameter list must not differ in function declaration and function calling. We can pass the same number of functions as it declared in the function declaration.

    Syntax :

    function_name(argument_list)

The Syntax of creating function in C language

return_type function_name(data_type parameter){
//code to be executed
}

There are two types of functions

  • Library functions
  • User-defined functions
  • Library functions: Library functions are functions which are declared in the header files such as scanf(),printf(), gets(),puts(),floor() etc.
  • User-defined functions User-defined functions is a block of code that performs specific task. It’s reduce the complexity of a big programs and optimize the code.

RETURN VALUE

  • In C function may or may not return value from the function.
  • If you don’t return the value from the function then we use void for the return type.
  • You want to return any value from the function you need to use any datatype such as int,char,long etc.
  • Return type depends on the value to be returned from the function.

Different phases of function calling

There are four different phases of function calls

  • function without arguments and without return value
  • function without arguments and with return value
  • function with arguments and without return value
  • function with arguments and with return value

Example for function without arguments and return value

#include <stdio.h>
void printName();  
void main ()  
{  
    printf("Hello ");  
    printName();  
}  
void printName()  
{  
    printf("theTechnicalFunda");  
}

Output

Hello theTechnicalFunda

Example for function without argument and with return value

#include  <stdio.h>
int sum();  
void main()  
{  
    int result;   
    result = sum();  
    printf("%d",result);  
}  
int sum()  
{  
    int a,b;   
    printf("\nEnter two numbers");  
    scanf("%d %d",&a,&b);  
    return a+b;   
}

Output

Enter two numbers 10 
10
20

Example for function with argument and without return value

#include  <stdio.h>
void sum(int, int);  
void main()  
{  
    int a,b,result;     
    printf("\nEnter two numbers:");  
    scanf("%d %d",&a,&b);  
    sum(a,b);  
}  
void sum(int a, int b)  
{  
    printf("\nThe sum is %d",a+b);      
}

Output

Enter two numbers 10 
10
The sum is 20

Example for function with argument and with return value

#include  <stdio.h>
int sum(int, int);  
void main()  
{  
    int a,b,result;    
    printf("\nEnter two numbers:");  
    scanf("%d %d",&a,&b);  
    result = sum(a,b);  
    printf("\nThe sum is : %d",result);  
}  
int sum(int a, int b)  
{  
    return a+b;  
}

Output

Enter two numbers:10
10 
The sum is : 20

Library Functions:

In C language library functions are inbuilt functions which are grouped together and placed in comman place is called library. Each library function perform specific operation. Function definition,declaration for macros are given in all header files. In C program header files using “#include” command to make use of the functions.

List of header files is given in the following table

S.No.Header fileDescription
1.stdio.hThis is a standard input/output header file. It contains all the library functions regarding standard input/output.
2.conio.hThis is a console input/output header file.
3.string.hIt contains all string related library functions like gets(), puts(),etc.
4.stdlib.hThis header file contains all the general library functions like malloc(), calloc(), exit(), etc.
5.math.hThis header file contains all the math operations related functions like sqrt(), pow(), etc.
6.time.hThis header file contains all the time-related functions.
7.ctype.hThis header file contains all character handling functions.
8.stdarg.hVariable argument functions are defined in this header file.
9.signal.hAll the signal handling functions are defined in this header file.
10.setjmp.hThis file contains all the jump functions.
11.locale.hThis file contains locale functions.
12.errno.hThis file contains error handling functions.
13.assert.hThis file contains diagnostics functions.

Submit your review
1
2
3
4
5
Submit
     
Cancel

Create your own review

The Technical Funda
Average rating:  
 0 reviews