Functions

0 allanson5363445 November 25, 2020

A function is defined as a self-contained block of statements that performs a particular task or job. This is a logical unit composed of a number of statements grouped into a single unit. It is often defined as a section of a program performing a specific job. Each program has one or more functions.

We have many advantages in using functions.

  • Manageability
  • Code Reusability
  • Non-redundant (non-repeated) programming (i.e. avoids redundant programming)
  • Logical Clarity
  • Easy to divide the work into many different programmers

The syntax of the function is:

return_type function_name (parameter_list)
{
   //Statement(s)
}

Let’s take an example to make clear concepts on function.

#include <stdio.h> 

int sum(int a, int b){
    return a + b;
}

int main(){
    //calling `sum` functions
    printf("Sum of 5 + 6 = %d", sum(5, 6));
    printf("Sum of 5 + 10 = %d", sum(5, 10));

    return 0;
}

The output of the above program is:

Sum of 5 + 6 = 11
Sum of 10 + 5 = 15

In this example, a function called sum() is defined once and it is called two times to calculate the sum of two numbers. The detail of the defining function will be discussed later in this chapter.

Function Declaration or prototype

The function declaration or prototype is a model or blueprint of the function. If the function is used before they are defined, then a function declaration or prototype is necessary which provides the following information to the compiler.

  • The name of the function
  • The type of value returned by the function
  • The number and the type of arguments that must be supplied while calling the function.

When a user-defined function is defined before the use, there is no need for function declaration.

The syntax of the function declaration is:

return_type function_name (type1, type2, type3, ....)

Here return_type specifies the data type of the value returned by the function. 

The function declaration and declarator or header in the function definition must use the same function name, a number of arguments, argument types, and return types. 

We will write the above program using a function declaration.

#include <stdio.h> 

//function declaration or prototype
int sum(int a, int b);

int main() 
{

    //calling `sum` function 
    printf("Sum of 5 + 6 = %d", sum(5, 6));
    printf("Sum of 5 + 10 = %d", sum(5, 10));

    return 0; 
}

int sum(int a, int b){
    return a + b;
}

The output of the above program remains the same. It is important to note that the function prototype has semicolumn at the end and to specify the name of formal arguments/variables is not mandatory but the type is mandatory.

Types of Function

We have two types of functions in the C programming language.

Types of Function
Types of Function

1. Built-in Function

There are the functions that are already written, compiled and placed in C library and they are not required to be written by a programmer. The function’s name, its return type, their argument number, and types have been already defined. We can use these functions as required. For example printf()scanf()etc are the example of library functions.

Let’s look at one example to make clear concepts on built-in function.

#include <stdio.h>
#include <math.h> 

int main(){

    /**
     * Calling built-in function
     * pow(x, y)
     * We are directly calling this function
     * */
    printf(pow(2, 3));

    return 0;
}

The output of the above program is:

8

In the above program, We have used the pow() function is already written in math.h library. This function helps us to find the power of any number.

User-defined Function

There are functions that are defined by the user at the time of writing a program. the user has the choice to choose its name, return type, arguments, and types. In the above example, the function sum() is user-defined function. 

A complex program can be divided into a number of user-defined functions.

Let’s look at an example to make clear concepts on user-defined functions.

#include <iostream>
using namespace std;

int sum(int x, int y);

int main(){

    int x, y;

    printf("Enter First Number : ");
    scanf("%d", &x);

    printf("Enter Second Number : ");
    scanf("%d", &y);

    /**
     * Here we are calling user-defined functions
     * named as sum
     * */
    printf("Sum = %d", sum(x, y));

    return 0;
}

int sum(int x, int y){
    return x + y;
}

The output of the above program is:

Enter First Number : 12
Enter Second Number : 23
Sum = 35