Functions in C++

2 mins 51 sec read Basic


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 ) ;
}

How was the tutorial? Nice. Right?



Deep Concept videos to crack the highly-piad interviews.

Chekout out our Youtube Channel to get detailed video content on important topics in interviews.



What is your Interview Score?

Test your skillset with the curated questions created by experts around the globe.

Book a free test slot. Now !.

It usually takes 30 mins for an online test and this test will be MCQ based. Get detailed analytics based on your test and recommedations with personalised roadmaps.

Book a test slot. Now!

Have questions about the trial coding class?

Chat with our experts to discuss

Connect on WhatsApp


Recommended tutorials

#online compiler for c     #python for programming    

#dfs and bfs algorithm     #programming with c language

#storageclass in C    #listcomprehension in python