1. Introduction to C++: A Comprehensive Guide with Code Examples - 2023 2. Mastering Data Types in C++: A Comprehensive Guide with Codes and Examples (2023) 3. Learn About Variables and Types of Variables in C++ | Codzify.com 4. Control Statements in C++: A Comprehensive Guide for 2023 5. C++ Tutorial: Understanding Switch Statements with Codes and Examples in 2023 6. Understanding Memory Allocation and Pointers in C++: A Beginners Guide 7. Functions in C++ 8. Call by value and Call by Reference in C++ in depth 9. Array in C++ 10. 2d arrays in C++ 11. Classes and Objects in C++ 12. Static Functions in C++ 13. Constructors and Destructors in C++ - A Complete Guide with Examples 14. Mastering Copy Constructor in C++ - Shallow vs Deep Copy with Examples | Codzify 15. Understanding Friend Functions in C++ Made Simple! 16. Inline Functions in C++ 17. this Pointer in C++ 18. Mastering Inheritance in C++: Types and Examples Explained 19. Types of Inheritance in C++ 20. Polymorphism in C++ Explained with Codes and Examples in 2023 21. Templates in C++ 22. Getting the Value of a MultiMap in C++: Step-by-Step Guide with Examples 23. Multimap Find and Replace Operator in C++: Step-by-Step Guide - Codzify Topics 24. Exploring the Next_Permutation Algorithm without STL in C++ - Codzify Topics 25. C++ - The Difference Between Map and HashMap in STL 26. Updating Values in a std::multimap in C++ - Codzify Topics 27. Which data structure sorts the elements on insertion in C++ STL? 28. Can we implement Red Black Tree in c++ by STL containers? 29. How to Dynamically Declare an Array of Objects with a Constructor in C++ - A Step-by-Step Guide 30. What is the difference between a pointer and an object in C++? 31. Mastering Red-Black Trees with STLs Internal Implementation: A Step-by-Step Guide

Functions in C++

Article by: Manish Methani

Last Updated: October 20, 2021 at 10:04am IST
2 mins 51 sec read

C++ Functions are defined as simply a set of statements which are executed one by one. Every C++ Program contains various functions which perform a particular task defined.

Assume Functions like,

Inside IT Companies there are different persons performing different functions like managers Function is different, Employees Function is different. Each Person performs his own task. Similarly, In Programming Language Functions are different blocks of statements which performs their own tasks.

Syntax

returnType functionName()
{
  statement 1;
  statement 2; 
}

returnType indicates the type that function returns. Type may be Integer , Float ,Double , void etc.

functionName() indicates the name of user-defined function.

statement 1; statement 2 indicates the statements you want to execute.

void main()
{
  displayMessage ();
  printf("Hey ! This is my first function :-: ");
}

displayMessage()
{
 printf("Yeppiee ! Main Method Called me :-: ");
}

Output:-

Yeppiee ! Main Method Called me :-: 
Hey ! This is my first function :-:

This was the very basic example for understanding what actually function does .

Main() Function

This is the entry point of any C++ Program. This is where the program starts. Yeah, of course, you may write every 10's of thousands of lines inside the main. But think Is it readable later in future? So why not to break those 1000 lines statements into Functions and let that function handle the statements. That's why Functions are introduced into C++ Language.

Function Declaration or Prototype of Function

Every C++ function returns integer value by default. If you don't specify any returnType to a function, it's integer always remember.

The program before Function Declaration.

In this program we calculate the square of a number which is of float type.

main() 
{
float a, b ;
printf ("
Enter any number ") ;
scanf ("%f", &a) ;

b = square (a) ;
printf ("
Square of %f is %f", a, b) ;
 }

 /* Square Function returns integer value by default here.
 Because if there is no returntype mentioned , Compiler will assume it 
 as integer datatype.*/ 

square (float x)
{
float y ;
y = x*x;
return (y) ; 
}

Output

Enter any number 3
Square of 3 is 9.000000 

Enter any number 1.5 
Square of 1.5 is 2.000000             /* is square of 1.5 is 2.000 ? */

Enter any number 2.5 
Square of 2.5 is 6.000000            /* is square of 2.5 is 6.000 ? */      

Since we did not specified returnType to Square function compiler assumed it as integer datatype. So square of 1.5 is 2.25 but since there is integer returntype ,0.25 will get discarded.That's why output is 2.000

Similarly,

Square of 2.5 is 6.25 but since there is integer returntype .25 will gets discarded. That's why output is 6.000

So to get desired output you have to tell the compiler that Function should return Float value.

That's why you have to specify the declaration of function first or you have to specify the prototype of a function.

Program after Function Declaration

main()
 {
 /* This is how you declare a function */
float square (float) ;

float a, b ;
printf ("
Enter any number ") ;
scanf ("%f", &a) ;

b = square (a) ;
printf ("
Square of %f is %f", a, b) ; 
}

/* Function Square with float returnType */
float square (float x)
{
float y ;
y = x * x;
return ( y ) ;
}

Output

Enter any number 1.5
Square of 1.5 is 2.250000 

Enter any number 2.5 
Square of 2.5 is 6.250000

Function Defination

Function defination provides the actual body of the function. The statements we had written inside square function is called as providing the body to any function.So for square function , Function Defination will look like this

float square (float x)
{
float y ;
y = x * x;
return ( y ) ;
}

Learn Flutter, FlutterFlow,
Firebase & Angular.

Looking to learn how to create the production-ready apps
from scratch? Well, you are at the right place.

₹1299 or $15.56

Learn to create the Dating App using No-Code Tool FlutterFlow

Level: Advanced

Explore Curriculum
₹1299 or $15.56

Flutter Mobile App Development Course

Explore Curriculum
₹1299 or $15.56

The Complete Angular Course

Explore Curriculum
₹1299 or $15.56

Dart Programming for Absolute Beginners

Coming Soon
FREE

Next.js course

Start Watching
FREE

Fundamentals of Computer Programming Languages

Start Watching
FREE

Learn HTML, CSS & Bootstrap

Start Watching

Latest Web Stories

1

Learn how to open WhatsApp using FlutterFlow | Step by Step Guide

Test your skills with these expert-led curated
Mock Tests.

C Programming Test

Test your C Programming skills with this comprehensive mock test on C Programming.

Take Test

Flutter Test

Solve most asked Interview Questions on Flutter and Test your foundational skills in flutter.

Take Test

GATE(CSE) Operating Systems

Solve most asked GATE Questions in Operating Systems and test your Gate Score.

Take Test

HTML,CSS Test

This is a mock test designed to help you assess your knowledge and skills in HTML and CSS.

Take Test

(GATE CSE) Data Structures & Algorithms Test

Solve most asked GATE Questions in Data Structures and Algorithms and test your Gate Score.

Take Test

Download the Codzify
Mobile App


Learn Anytime, Anywhere at your own pace. Scan the QR Code with your Mobile Camera to Download the Codzify Mobile App.

Codzify Mobile App Codzify Mobile App
Codzify Logo

Terms and Conditions    Cookie Policy   Refund Policy   Adsense Disclaimer