Functions Introduction

A function is a block of statements that performs a specific task. It contains the set of programming statements enclosed by {}. A function can be called multiple times to provide reusability and modularity to the C program. It is also known as procedure or subroutine in other programming languages.

Sometime we need to perform same set of operations at many different places in a program. Also we might need to perform the same task more than once. In such case you have two options.

  1. Use the same set of statements every time you want to perform the task
  2. Create a function to perform that task, and just call it every time you need to perform that task.

Using option (2) is a good practice and a good programmer always uses functions while writing codes in C

Advantage of functions in C

There are the following advantages of C functions.

  • To improve the readability of code.
  • We can avoid rewriting same logic/code again and again in a program.
  • We can call C functions any number of times in a program and from any place in a program.
  • We can track a large C program easily when it is divided into multiple functions.
  • Improves the reusability of the code which is the main achievement of C functions.

Types of Functions

Functions are divided into two categories in C programming:

C Function
Library Functions

The functions which are declared in the C header(Predefined standard library functions) files(.h files like stdio.h) such as scanf(), printf(), gets(), puts(), ceil(), floor() etc.

The list of mostly used header files is given in the following table.

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

The functions which are created by the C programmer is called user defined functions. It reduces the complexity of a big program and optimizes the code.

Syntax
return_type function_name(argument list) {
    Set of statements– Block of code
}

return_type

Return type can be of any data type such as int, double, char, void, short etc.

function_name

It is the name of the function and can be anything. However it is advised to have a meaningful name for the functions for better readability and it would be easy to understand the purpose of function just by seeing it’s name.

argument list

Argument list contains variables names along with their data types. These arguments are kind of inputs for the function. For example – A function which is used to add two integer variables, will be having two integer argument.

Block of code

Set of C statements, which will be executed whenever a call will be made to the function.

In C function there are three aspects.

  • Function declaration A function must be declared globally in a c program to tell the compiler about the function name, function parameters, and return type.

  • Function call Function can be called from anywhere in the program. The parameter list must not differ in function calling and function declaration. We must pass the same number of functions as it is declared in the function declaration.

  • Function definition It contains the actual statements which are to be executed when the function is called. Only one value can be returned from the function.
SNC function aspectsSyntax
1Function declarationreturn_type function_name (argument list);
2Function callfunction_name (argument_list)
3Function definitionreturn_type function_name (argument list) {function body;}
Returning from function

A C function may or may not return a value from the function. If you don't have to return any value from the function, use void for the return type.

Example without return value.

snippet
void hello(){
printf("hello c");
}

If you want to return any value from the function, you need to use any data type such as int, long, char, etc. The return type depends on the value to be returned from the function.

Example with return value.

snippet
int get(){
return 10;
}

In the above example, we have to return 10 as a value, so the return type is int. If you want to return floating-point value (e.g., 10.2, 3.1, 54.5, etc), you need to use float as the return type of the method.

snippet
float get(){
return 10.2;
}

Different aspects of function calls

A function may or may not accept any argument. It may or may not return any value. Based on these facts, There are four different aspects 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 argument and return value

Example 1

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

Example 2

snippet
#include<stdio.h>
void sum();
void main()
{
    printf("\nGoing to calculate the sum of two numbers:");
    sum();
}
void sum()
{
    int a,b; 
    printf("\nEnter two numbers");
    scanf("%d %d",&a,&b); 
    printf("The sum is %d",a+b);
}
Output
Going to calculate the sum of two numbers: Enter two numbers 10 24 The sum is 34

Example for Function without argument and with return value

Example 1

snippet
#include<stdio.h>
int sum();
void main()
{
    int result; 
    printf("\nGoing to calculate the sum of two numbers:");
    result = sum();
    printf("%d",result);
}
int sum()
{
    int a,b; 
    printf("\nEnter two numbers");
    scanf("%d %d",&a,&b);
    return a+b; 
}
Output
Going to calculate the sum of two numbers: Enter two numbers 10 24 The sum is 34

Example 2: program to calculate the area of the square

snippet
#include<stdio.h>
int sum();
void main()
{
    printf("Going to calculate the area of the square\n");
    float area = square();
    printf("The area of the square: %f\n",area);
}
int square()
{
    float side;
    printf("Enter the length of the side in meters: ");
    scanf("%f",&side);
    return side * side;
}
Output
Going to calculate the area of the square Enter the length of the side in meters: 10 The area of the square: 100.000000

Example for Function with argument and without return value

Example 1

snippet
#include<stdio.h>
void sum(int, int);
void main()
{
    int a,b,result; 
    printf("\nGoing to calculate the sum of two numbers:");
    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
Going to calculate the sum of two numbers: Enter two numbers 10 24 The sum is 34

Example 2: program to calculate the average of five numbers.

snippet
#include<stdio.h>
void average(int, int, int, int, int);
void main()
{
    int a,b,c,d,e; 
    printf("\nGoing to calculate the average of five numbers:");
    printf("\nEnter five numbers:");
    scanf("%d %d %d %d %d",&a,&b,&c,&d,&e);
    average(a,b,c,d,e);
}
void average(int a, int b, int c, int d, int e)
{
	float avg; 
	avg = (a+b+c+d+e)/5; 
	printf("The average of given five numbers : %f",avg);
}
Output
Going to calculate the average of five numbers: Enter five numbers:10 20 30 40 50 The average of given five numbers : 30.000000

Example for Function with argument and with return value

Example 1

snippet
#include<stdio.h>
int sum(int, int);
void main()
{
    int a,b,result; 
    printf("\nGoing to calculate the sum of two numbers:");
    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
Going to calculate the sum of two numbers: Enter two numbers:10 20 The sum is : 30

Example 2: Program to check whether a number is even or odd

snippet
#include<stdio.h>
int even_odd(int);
void main() {
    int n, flag = 0;
    printf("\nGoing to check whether a number is even or odd");
    printf("\nEnter the number: ");
    scanf("%d", & n);
    flag = even_odd(n);
    if (flag == 0) {
        printf("\nThe number is odd");
    } else {
        printf("\nThe number is even");
    }
}
int even_odd(int n) {
    if (n % 2 == 0) {
        return 1;
    } else {
        return 0;
    }
}
Output
Going to check whether a number is even or odd Enter the number: 100 The number is even
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +