What are Functions in Programming
In Programming, Functions are a piece of code that has a finite number of statements that perform a specific task. The functions are designed to a reusable piece of code that can be invoked as many times as needed in the program. Functions in C/C++ has a main() function that is mandatory because the execution of program starts from main() function.
Functions typically take inputs called parameters or arguments, which are used in the function to perform some specific calculations and operations specified in the function body. They can also return a value by using return statement where, the function is called in the program.
Basic Syntax
Basic Syntax of function depends on the programming language you use. But almost every language use some core component of function which are as follows:
- Function declaration: which specifies you are defining a function.
- Function name: A unique name to identify a function.
- Parameter/Argument: Input on which the function will perform operations
- Function Body: block of code which tells the program what to do.
- Return Statement: Used to return the result of operation that function will perform (optional).
Basic Syntax in c++
In c++ you have to use return type then function name
int addition()
{
return 0;
}
Function Definition
Function definition consists of the function name , its parameters, function body and return statement if needed and return type in some languages. A function definition provides the actual implementation what the program will do. The function body consists of a number of statements that tell the program what to do.
Function Call
A function call is a statement that cause a function to execute. As the name suggests, a function call is something that tells the compiler to execute the function’s statements. you can call the function as many time as needed in the program.
Example of Functions In programming
In C++
//This program include two function main and display message
#include <iostream>
using namespace std;
//create a function
void displayMessage()
{
cout << "hello from Display message function"<< endl;
}
// main Function
int main()
{
cout << "Hello From Main" << endl;
displayMessage();
cout << "back in function main" << endl;
}
Output
Hello From Main
hello from Display message function
back in function main
Type of function in programming
There are two type of function in programming
- Library Function Also known as predefined functions. These are predefined in programming language libraries(a collection of code that provides a set of functionalities). There is no need to write function declaration or function definition to use these functions. you can just call these function into your main program to use them. Example: in c printf is a library function that tells program to print specific line. in python math module for mathematical functions. he Arraylist class from the java.util package for dynamic arrays.
- User Defined Functions These are the function that a user or developer declares, defines and call them in the main program. In some programming languages we can add user defined function in library to use it another program. User defined functions increase the scope of functionality , reuseability, organization and modularity in programming as we can add as many functions as we want.
Why are Functions Important?
We need function in object oriented programming because they provide numerous benefits to the developer. Some of the key benefits of using function are :
- Modularity: Function breaks down bigger complex problem into smaller manageable parts as independent program.
- Reusablility: You can call functions multiple times in different parts of the program as needed
- Abstraction: They allow Programmers to focus on how to solve a specific problem without worrying about their implementation.
- Organization: Function provide better organization of code by splitting different concerns into distinct units.
- Debugging: If a problem occurs during debugging it is easy to locate the problem and fix it by using function.
- Avoid Duplication: Without Function you have to write code similar for more than one time to perform a specific task more than one time in a program. With function you can just call it as many time as you need that specific functionality in the main program.