C Function : Introduction to C Functions

A function is a block of code designed to perform a particular task. It contains organized and reusable code that will be executed by an event or a call to that function. By the using of functions we do not need to write the same code multiple times in order to achieve our goals.

Functions is C language are the basic building blocks of a program, All the C programs are written using the functions to improve the re-usability, understandability and to keep track on the code. The C functions can be classified into two categories, which are :

1. Library funtions
2. User-defined functions


1. Library Functions:

Library functions are pre-defined functions, means the functions which are already defined in C library. For example printf(), scanf(), malloc(), getc(), putc(), memcpy(), strcat() are defined in the C head files.

2. User-defined Functions :

The user-defined functions are defined by the users/programmers at the time of writing programs. These functions enables the code reusability and saves execution time and space, And also reduce the complexity of a big program and optimizes the code.


Defining  a function :

The general syntax of defining a function is as follows :
return_type Function_name( parameter_list... )  {
  // statements
}
Where :
  • return_type specifies the data type of value returned by the function. Also note that some functions perform the operation without returning a value. 
  • Function_name is name of the function. In C language the function name can contains letters, digits, underscores, but remember that the name of function would always start with letters or underscore and nothing else.
  • parameter_list is the list of parameter passed to the function. A parameter is like a placeholder. When a function is called or invoked, we can pass a value to the parameter. these are reffered as formal parameters.

Function Parameters :

Function parameters are nothing but the values passed to the functions. These values are also referred to as actual parameter or arguments. The parameter list on function definition refers to the type, order, and number of the parameters of a function. In C language a function may or may not have parameters.

Now lets see some example of function declaration.

Function without Parameter :
void print() {
  printf("Hello world!\n");
}
The above function prints "Hello world!" on the output screen. At the above function, we did not define any parameter, and also it doesn't return any data because of void data type.
#include <stdio.h>

void print() {
  printf("Hello world!\n");
}

int main() {
  print();
  return 0;
}
Output :

Hello world!

Function with Parameter :

The syntax for the function with parameter is :
return_type Function_name(data_type param1, data_type param2, ....)  {
  // statements
}
Where param1, param2 are nothing but the name of the formal parameters. Now lets see  an example:
int add(int a, int b){
  int result;
  result = a + b;
  return result;
}
The above code takes two parameters, add them and return to the caller function. Now lets see the example program:
#include <stdio.h>

int add(int a, int b){
  int result;
  result = a + b;
  return result;
}

int main() {
  int num;
  num = add(5, 5);
  printf("%d\n", num);
  printf("%d\n", add(14, 14));
  return 0;
}
Output :

10
28


Calling a function :

After creation of function we have to call that function in order to perform the defined task. When a function is called then the control of program execution is transferred to that function. The called function perform the defined task and return the result to caller function, and return control of program execution to the caller function.

To call a function in C, we simply need to pass the required parameters along with the function name, and if the function return a value, then we have to store the value. The syntax are as follows :

Function without Parameter :
function_name();
Function with Parameters :
function_name(prm1, prm2);
Example :
#include <stdio.h>

void print() {
  printf("Hello world!\n");
}

int multiply(int a, int b){
  int result;
  result = a * b;
  return result;
}

int main() {
  print();
  int val = multiply(5, 5);
  printf("%d\n", val);
  return 0;
}
Output :

Hello world!
25


Next Topic :