ADVERTISEMENT
ADVERTISEMENT

What is function in C with example?

A function in C is a section of code that executes a certain task and returns a value. The parameters are used to pass information to the function whereas the function name is used to call the function within the programme. According to the function definition, a value of a particular data type may be returned by the function. A programme can be organised and divided into smaller, easier-to-manage portions using functions, which also allow for code reuse and improve readability. In order to organise, structure, and reuse code to make programmes more understandable, manageable, and error-prone, functions are a key component of C programming.

Three important parts of functions are

  • Function Declaration
  • Function Calling
  • Function Definition

Function Declaration

A function declaration provides information to the compiler about the name, return type, and parameters of a function. The body of the function is not included in the function declaration. It also is known as the "function prototype."

  • "return_type" is the data type of the value that the function returns. If the function does not return a value, the keyword "void" is used.
  • "function_name" is the name of the function, which follows the same naming conventions as variables in C.
  • "parameter_list" is a list of variables that the function takes as input, separated by commas. If the function takes no input, the parameter list is empty.

Syntax of Function Declaration is

return_type function_name(parameter_list);

For example:

int add(int x, int y);

Function Calling

In a programme, a function is called or invoked by including the function name inside parentheses, followed by the function's arguments. The function call prompts the programme to navigate to the function definition and run the function's code.

Syntax for Function Calling is

function_name(argument_list);

For Example

int main()
 {
    int num1 = 5, num2 = 7,result;
    result = add(num1, num2);  // Function Calling
    printf("Sum of %d and %d is %d", num1, num2, result);
    return 0;
}

It is important to note that the parameter list in the function declaration and definition must match the argument list in the function call, and the return type of the function should match the expected data type in the function call.

Function Definition

The function body and declaration are both parts of a function definition. It provides a full implementation of the feature.

return_type function_name(parameter_list)
{
    // function body
}

For Example

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

Need of Functions 

The requirement for functions in C can be wrapped as follows:

  • Code Organization: Functions help in the division of a programme into more manageable, smaller units. It is simpler to understand a program's overall structure and flow when it is divided into functions.
  • Reusability of Code: Functions make it possible to reuse code. The amount of code needed to be written is decreased and future adjustments are made easier by allowing a single task to be defined once in a function and called as many times as required.
  • Improved Readability: Functions provide a way to give a specific task a name, making it easier to understand what the code is doing. This improves the readability of the program, and also makes it easier to debug and maintain the code.
  • Data Abstraction: Functions provide a way to pass data to a function and return the result, which helps in data abstraction. It allows the programmer to think about the problem at a higher level of abstraction.
  • Modularity: Functions help to create modular programs, which are easy to understand, modify, and debug. A program can be divided into smaller modules, each of which can be written and tested separately.

Overall, functions are a fundamental aspect of C programming, they are essential for organizing, structuring, and reusing code, making programs more readable, maintainable, and less prone to errors.


ADVERTISEMENT

ADVERTISEMENT