Programming

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 addition to calling built-in functions provided by libraries or programming languages, you can define your own custom functions tailored to your specific needs. This allows you to break down complex tasks into smaller, manageable pieces of code that can be reused.

To use a custom function in your program, you need to call it by its name followed by parentheses (). If the function has parameters, you provide values for those parameters within the parentheses.

Functions can be called from other functions or even recursively (a function calling itself). By organizing code into smaller functions with specific purposes, you can improve the readability and maintainability of your programs.

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

Show More

Kartik

Hi, My name is Kartik. I have expertise in Technical and Social Domains. I love to write articles that could benefit people and the community.

Leave a Reply

Back to top button