Functions

Lesson 16
Author : Afrixi
Last Updated : January, 2023
C - Programming Language
This course covers the basics of programming in C.

Functions in C are a way to group related pieces of code together and give them a name, so that you can call them from other parts of your program. Functions can help make your code more modular, easier to understand, and easier to maintain.

To declare a function in C, you specify the return type of the function, the name of the function, and any parameters it takes, like this:

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

This declares a function called add that takes two integer parameters and returns an integer. The body of the function adds the two parameters together and stores the result in a local variable called sum, then returns the value of sum.

To call a function in C, you simply use the name of the function followed by any arguments it takes inside parentheses, like this:

int result = add(3, 5);

This calls the add function with arguments 3 and 5, and stores the result (8) in a variable called result.

Functions can also have no return value (i.e. they return void), or they can take no parameters. In addition, functions can call other functions, and functions can be defined before or after they are called.

Functions are an important part of programming in C, and are used extensively in creating modular, reusable code.