What are functions in C and C++?

Functions in C and C++ are named blocks of code that perform a specific task. They provide a way to organize and modularize code, making it easier to write, understand, and maintain complex programs.

A function is defined using a function declaration or prototype, followed by its implementation. The function declaration provides information about the name, return type, and parameters of the function without defining its body. The implementation contains the actual code that executes when the function is called.

In both languages, functions have a return type, a name, a parameter list (optional in C), and a body. The return type specifies the data type of the value that the function will return. The name is used to identify the function and can be used to call it from other parts of the program. The parameter list contains variables or values that are passed to the function as inputs. The body contains the code that defines what the function does.

Why use a Function?

It is used where a repeated task has to be done. It also makes your code neat and simple to understand. Many professional programmers advise using functions because it is the best practice for coding and debugging any error. You should also prefer to use functions in your programme. It divides your code into simpler blocks and thus reduces redundancy.

Functions are of two types:

  • Predefined Functions: These types of functions have been previously defined and they can be used by using Header files. For example, main() is predefined, and the compiler looks for it.
  • User-Defined Functions: These types of functions are defined by the User, and used later in the program.

Working of Function

what are functions in c and c++?

Return Type: It tells us what data type a particular function will give. Examples, void, float, double, int, etc. Note that with the void, you cannot use return.

Function Name: It is the unique name of the function. If more than one name is defined in a program, the compiler will throw an error message. It could be any name.

Argument Name and Type: It tells us what type of value a function will take and how many arguments are needed to be passed through it. There is no limit to the number of arguments.

Pass By Value: Here, the actual parameters are copied to the function’s formal parameters and the two types of parameters are stored in different memory locations. This ensures that any changes made inside the function are not changed in the actual parameters of the calling function.

Pass By Reference: Here, both are referred to the same address location; thus, any change made inside a function also changes the actual parameters of the calling function.

A simple example of Function :

Here a simple demonstration of function is given, along with an explanation in comments.

#include<iostream>
using namespace std;

int sum ( int a , int b );      // Function Declaration

int main()
{
   int a , b ;
   cout << "Enter 2 numbers " << endl;
   cin >> a >> b;
   int c = sum (a,b);        // Function Calling with arguments passing
   cout << "Sum of two numbers:  "<< c;
   return 0; 
}

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

Output

Enter 2 numbers

5 , 7

Sum of two numbers: 12

How to Use Functions in C and C++

A function is a self-contained block of code that performs a specific task and can be called multiple times within a program. Functions help improve code readability, modularity, and maintainability. In both languages, a function consists of a return type, function name, parameters (optional), and a body. The syntax typically follows this structure:

return_type function_name(parameters) {
    // Function body
    return value; // Optional, depending on return type
}

For example, a simple function that adds two numbers in C or C++ looks like this:

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

This function can be called in main() as follows:

int result = add(5, 10);

Functions in C and C++ can be classified into user-defined functions and library functions (like printf() in C or cout in C++). They can also be pass-by-value or pass-by-reference, depending on whether changes made within the function should reflect outside it. C++ further enhances function usage with features like function overloading and default parameters, allowing multiple functions with the same name but different parameter lists. By using functions efficiently, programmers can write efficient, structured, and reusable code.

Also Read: MVC (Model-View-Controller) and its benefits

Related Articles

Back to top button