Defining functions in C language

In the C language, functions are independent units of code that can be accessed from anywhere in the program. They aid in code organization and increase reusability. A function in C consists of a name, a return type, a list of optional parameters, and a body.

Syntax of function:

return_type function_name(parameter list) {
// Function body
}
  • return_type: The data type of the value that the function returns to the calling program. It can be void if the function does not return any value.
  • function_name: The name given to the function. It should be a valid identifier.
  • parameter list: The list of parameters or arguments that the function accepts. It can be empty if the function does not require any input.
  • function body: The set of statements that make up the function. It contains the instructions that perform the desired task.

Example:

#include <stdio.h>

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

int main() {
   int x = 5, y = 10, z;

   z = sum(x, y);
   printf("The sum of %d and %d is %d\n", x, y, z);

   return 0;
}

In this example, the sum function takes two arguments a and b, which are integers, and returns their sum. The main function declares two variables x and y of type integer and initializes them to 5 and 10, respectively. It then calls the sum function with x and y as arguments and stores the result in the variable z. Finally, it prints the value of z.

Functions in C can have a return type of void, which means they do not return any value. Here’s an example of a function that takes an integer as an argument and prints it:

#include <stdio.h>

void printNumber(int num) {
   printf("The number is %d\n", num);
}

int main() {
   int x = 10;

   printNumber(x);

   return 0;
}

In this example, the printNumber function takes an integer argument num and prints it using the printf function. The main function declares a variable x of type integer and initializes it to 10. It then calls the printNumber function with x as the argument.

The arguments to functions in C can also have default values. This is an illustration of a function that accepts two integer arguments and outputs their sum. It defaults to zero if one of the inputs is missing:

#include <stdio.h>

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

int main() {
   int x = 5, y = 10, z;

   z = sum(x, y);
   printf("The sum of %d and %d is %d\n", x, y, z);

   z = sum(x, 0);
   printf("The sum of %d and %d is %d\n", x, 0, z);

   return 0;
}

In this example, the sum function takes two integer arguments a and b, and returns their sum. If b is not provided, it defaults to zero. The main function calls the sum function twice, once with both arguments provided, and once with only the first argument provided. The output shows the sum of both calls.

In conclusion, C functions are an effective tool for arranging code and enhancing code reusability. They can be called from anywhere in the program and can have a return type, arguments, and default values for arguments.