C++ is one of the most versatile languages in the world. It is used nearly everywhere for everything… systems programming (operating systems, device drivers, database engines, embedded, Internet of Things, etc.)

Enter Header Image Headline Here

Saturday, 22 April 2017

C++ Functions

                                         C++  Functions


In Programming, function refers ti a segment that groups code to perform a specific task. 


Depending on whether a function is predefined or created by the programmer, there are two types of function: 

1.   Library Function
2.   User-defined Function

Library Function  -


Library functions are the built-in function in C++ programming.

Programmer can use library function by invoking function directly, they don't need to write it themselves.


Example :   Library  Function



















Output :



In the example above , squrt() library function is invoked to calculate the square root of number.


Notice code #include<cmath> in the above program,Here, cmath is a header file. The function definition of Sqrt() (body type function) is present in the cmat   header file. 


You can use all function defined in cmath when you include hte content of file cmath  in this program using #include <cmath>


Every valid C++ program has at least one function, that is main() function


User- defined Function :


C++ allows programmer to define their own function.

A user-defined function group code to perform a specific task and that group of codes defined in the body of function.


How user-defined function works in C Programming ?


When  a program begins runnung, the system calls the main() function, that is, the system Starts executing codes from main() function.  

When control of the program reaches to function name() inside main(), it moves to void function name() and all codes inside void function name () is executed.

Then, control of the program moves back to the main function where the code after the call to the function name () is executed .


Example User Defined Function  :

C++ program to add two integers, Make a function add()     to add integers and display sum in       main () function.






















Output  :






0 comments:

Post a Comment

Popular Posts

Recent Posts

Categories

Unordered List

Text Widget