Function Definition and Declaration


What is a function ?

A task is a set of statements written to perform a specific task. The function can be built-in or user-defined. Built-in functions are declared and defined by the language itself, which we can use for our program. User-defined are the functions that we have to declare and define as per our need and use in a specific scope.

Function Declaration

A single function has four parts.

  • Return Type : Every function has a return type. It may be an integer; it may be float or anything else.
  • Function name : Every function has a name using which the user calls it in the program.
  • Parameters : There are two types of function, one is normal(without parameter) and other is parameterized. Parameters are used to access the data form program.
  • Function Body : Body means where we write the group of lines to perform the task.

E.g. int max(int, int); // Here the function max has return type integer and contains 2 parameters of type integer.

Function Definition

After function declaration, we have to write a function body that contains the group of statements used to perform the task. In a parameterized function, the function can access the main method data. All the lines of code are enclosed in the curly braces. In C language, the program also starts from main function i.e., main(). Every statement in the function ends with a ‘;’.