In this post, you will learn about how to use functions in c++ with examples.
C++ FUNCTION
- Group of statements that together perform a specific task. Every c++ program contains at least one function, which is main () function
- Defines the behavior of a class
- Implementation of variables
- A function can be either static or non-static function (instance method)
- Function can have a list of formal parameters and may or may not a return value
TYPES OF FUNCTIONS
- C++ supports two types of functions. They are:
- Built-in function or pre-defined function or library function
- User defined function
1. Pre-defined function
- It is already created by developer and added in c++ compiler
- Once header files are imported, then user can access its predefined functions in the code
- No need to provide any implementation (definition) for this pre-defined function. Because its definition is already created and attached with corresponding header files.
- Example
- cout, cin, endl, clrscr(), etc, …
2. User defined function
- Here function is created by programmer or user
- It is defined by user
- It is called by using its name.
FUNCTION DECLARATION
- Functions are declared inside the body of a class, normally after the declaration of variables
SYNTAX
<r.type> method-name(a1, a2, … an)
{
// method body
}
Where,
a1, a2, … an formal parameters list
- Function declaration has four parts
a. Function name
- Specify the name of the method
b. Return Type
- Specify the type of value, the function will return
- Return type can be: void, int, double, char, etc…
c. List of parameters
- Function can take zero or more number of dummy arguments
- Formal-parameter-list is always enclosed in parenthesis
- The parameters are separated by comma operator
d. Body of the function
- Function body is enclosed in curly braces
- Here the user must give the implementation of variables / tasks.
FUNCTION MODIFIERS
S.N | MODIFIER | MEANING |
1. | private | It is accessed within a class only |
2. | public | It is accessed anywhere in the programming |
3. | protected | Same as private, also it is accessible in derived class if inheritance is involved |
MAIN() FUNCTION
- Main() method is an entry point in C++ program
- It can be either int or void as return type
Example
void main() { … }
int main() { return 0; }
INVOKING FUNCTIONS
- Process of activating a method is called invoking or calling.
- If function is used without class, then it is accessed by using its name only
Example
2. If function is an instance type, it must be called with help of object name
Example
3. If method is a static type, it must be called with help of class name
Example
NESTING OF FUNCTION
- If a function can call single function or multiple functions, then it is called as nesting of function
- A function can call another method of the same class by just mentioning the function name.
I. NESTING OF FUNCTION
SOURCE CODE
#include<iostream>
using namespace std;
void m1()
{
cout<<“Chennai City \n”;
}
void m2()
{
cout<<“Salem City \n”;
}
void m3()
{
cout<<“Madurai City \n”;
}
void nest()
{
// call all methods via nesting
m1();
m2();
m3();
}
// main() method
int main()
{
cout<<“—————————————\n”;
cout<<“\tNesting of functions\n”;
cout<<“—————————————\n”;
nest();
return 0;
}
OUTPUT
More Useful Posts
-
How to convert Word to Number in Bash Script
FacebookTweetPinLinkedInEmailShares0SharePrint In this bash tutorial, you will get convert word to number in bash script with examples.This is implemented using for loop, tr command, switch case in shell…
-
How to Count Word Frequency in Bash Script
FacebookTweetPinLinkedInEmailShares0SharePrint In this bash tutorial, you will get the program about how to count word frequency in bash script. This is done by using for loop,…
-
Bash Script – How to convert Number to Words
FacebookTweetPinLinkedInEmailShares0SharePrint In this tutorial, you will see how to convert numbers to words using bash script. This shell script can be done with the help of switch case…