C++ Functions

Functions in C++ language are building blocks of a C++ program. A function is a sequence of statements to perform a specific task and it can be called by other functions. A function may take some input and perform a logical unit of task and may return the response to calling function.
The function in C++ language is also known as subroutine, procedure or method. Functions are the building blocks of modular and reusable code, allowing us to organize our programs, enhance readability, and encapsulate logic.

  • A C++ program is a set of functions calling each other.
  • We can use any number of functions in a C++ program.
  • All C++ programs must contains at least one main() function.
  • When a function exits, it return control to the calling function from where it is called.
  • There are two types of functions depending on whether a function is created by programmer or is pre-defined in library.

Advantage of Functions in C++ Programming

  • We can call a function any number of times in a program from any place which avoid rewriting same code again and again at various program.
  • A large C++ program can divide into set of functions, where each function owns the responsibility of performing one specific task. Functions are written in order to make C++ program modular.
  • We can add common utility functions in header files, So that It can be reused by other programs.
  • When a function exits, it return control to the calling function from where it is called.
  • Breaking a large C++ program into functions, makes it easy to maintain, more readable and easy to understand.

How Functions work in C++ Programming

  • First of all main() function of C++ program gets called by Operating system.
  • Execution of C++ program begins. Program's statements and expressions getting executed in top to bottom sequence.
  • When control reaches a function call lets say myFunction(int val); it pauses the execution of current function and control goes inside the called function myFunction.
  • Once execution of code inside myFunction body finishes control comes back to the calling function. It resumes the execution of calling function at the next statement following the function call of myFunction.
  • At the point of any function call, control keeps on jumping between calling function and called function.
  • C program terminates when execution of main function ends.

Function Declaration in C++

A function declaration in C++ tells the compiler about function name, function parameters and return value of a function. The actual body of the function can be defined separately. Like variable in C, we have to declare functions before their first use in program.

Syntax of Function Declaration in C++
return_type function_name(type arg1, type arg2 .....);
  • Function declaration is also known as function prototype.
  • Function declaration in C++ always ends with a semicolon.
  • By default the return type of a function is integer(int) data type.
  • Name of parameters are not compulsory in function declaration only their type is required. Hence following declaration is also valid. int getSum(int, int);
  • If function definition is written before main function then function declaration is not required whereas, If function definition is written after main function then we must write function declaration before main function.

For Example
  • Declaration of a function with no input parameters
    void printName();

  • Declaration of a function with one input parameter and integer return type
    int isPrimeNumber(int num);

  • Declaration of a function with multiple input parameter and integer return type.
    int getProduct(int num1, int num2);

  • Declaration of a function with no input parameters and integer return type.
    int getRandomNumber();


C++ Function Declaration Example Program

#include <iostream>
using namespace std;
 
// Function Declaration
int getSum(int a, int b);
 
int main(){
   int a, b, sum;
   cout << "Enter two numbers\n";
   cin >> a >> b;
   // Calling getSum function 
   sum = getSum(a, b);
   cout << "SUM = " <<sum;
   
   return 0;
}
// Function Defination
int getSum(int a, int b) {
   return a+b;
}

Output
Enter two numbers
3 6
SUM = 9
In above program, we declared a function named "getSum" before main, which takes two integer parameters. After main, we defined the function to add two numbers and return sum.

Function Definition in C++

A function definition in C++ language consists of function name, function parameters, return value and function's body.

Syntax of Function Definition
return_type function_name(type arg1, type arg2 .....) {
    body of a function
    return (expression);
}
  • return_type : The return_type is the data type of the value returned by the function. void data type is used when the function does not return any value. By default the return type of a function is integer(int).
  • function_name : This is a C++ identifies representing the name of the function. Every function must have a unique name in a C++ program. Name of a function is used to call a function.
  • type arg1, type arg2 ..... : It is a comma-separated list of data types and names of parameters passed by the calling function. The parameter list contains data type, order, and number of the parameters expected by a function at the time of calling it. Parameters field is optional, we can skip it if a function don't require any data from calling function.
  • body of a function : The body of a function contains a set of statements that will be executed when this function is called. It may define it's own local variables and call other functions.
  • return(expression) : This is used to send a value to calling function before termination of function. If the return type of function is void, you need not use this line. When control reaches return statement, it halts the execution of function immediately and returns the value of expression to calling function.
C++ Function Definition
Points To Remember
  • First line is called as Function Header and it should be identical to function Declaration/Prototype except semicolon.
  • Function's definition contains code to be executed when this function is called.
  • Name of arguments are compulsory here unlike function declaration.

C++ Function Definition Example Program

#include <iostream>
using namespace std;
 
// Function Defination
int getAreaOfSquare(int side){
   // local variable declaration
   int area;
   area = side*side;
   // Return statement
   return area;
}
 
int main(){
   int side, area;

   cout << "Enter side of square\n";
   cin >> side;
   // Calling getAreaOfSquare function 
   area = getAreaOfSquare(side);
   cout << "Area of Square = " << area;

   return 0;
}

Output
Enter side of square
5
Area of Square = 25

Conclusion

Functions are the bedrock of structured and modular programming in C++. Their ability to encapsulate logic, promote code reuse, and enhance readability makes them indispensable in software development. By understanding the nuances of function syntax, parameters, return values, and best practices, you empower yourself to write clean, maintainable, and efficient code.