In this lesson, you will learn.
A function is a self-contained block of statements or code segments that is used to perform a particular task.
For example: You can create a function named ‘factorial()’ and inside it, you can write a logic to find the factorial of any number.
Here is the syntax to define a function in C++.
Here,
Functions in C++ can be of different forms.
1. Function without arguments and without returning a value.
// 1. Function without arguments
// and without returning value
void starline()
{
// Function body
}
2. Function with arguments and without returning a value.
// 2. Function with arguments and
// without returning a value.
void findsum(int x, int y)
{
cout<<"Sum: "<<(x+y);
}
3. Function without arguments and with returning a value.
/* 3. Function without arguments and with
returning a value */
int getnumber() {
int num;
cout<<"Enter any number";
cin>>num;
return num;
}
4. Function with arguments and with returning a value.
/* 4. Function with arguments and with
returning a value */
float multiply(float x, float y)
{
return (x*y);
}
Here, we are adding a function show() in our C++ program.
#include<iostream>
using namespace std;
void show(); // Function prototype
int main()
{
...............
//code
show(); //Function Call
...............
//code
}
void show()
{
//Body of function
}
The following figure shows the flow of the code.
//FunctionTablePrintEx1.cpp
#include <iostream>
using namespace std;
void starline(); //function declaration (prototype)
int main(){
starline(); //call to function
cout << "Data type Range" << endl;
starline(); //call to function
cout << "char -128 to 127" << endl
<< "short -32,768 to 32,767" << endl
<< "int System dependent" << endl
<< "long -2,147,483,648 to 2,147,483,647" << endl;
starline(); //call to function
return 0;
}
//starline() - function definition
void starline() //function declarator
{
for(int j=0; j<45; j++) //function body
cout << '*';
cout << endl;
}
*********************************************
Data type Range
*********************************************
char -128 to 127
short -32,768 to 32,767
int System dependent
long -2,147,483,648 to 2,147,483,647
*********************************************
The function prototyping describes the function template to the compiler by giving details like the number, type of arguments, and the type of return value is also sometimes referred to as the function signature.
void starline();
The declaration tells the compiler that at some later point we plan to present a function called starline and terminate it with a semicolumn.
We can remove the function declaration and can place the function definition before the function call.
#include
using namespace std;
// starline() //function definition
void starline()
{
for(int j=0; j<45; j++)
cout <<"*";
cout << endl;
}
int main(){
------------
------------
starline();// Function call
------------
}
It is a simplistic approach and makes the program short, there is no need to provide the function declaration but it is less flexible.
In the case of multiple functions, the programmer has to arrange all functions like each one appears before a function call.
Many programmers prefer to put main() function first in the list because it is the entry point of the execution and the first function to be called.
So generally, we use the first approach i.e. declaration of function prototype before using it.
There are no reviews yet. Be the first one to write one.
You must be logged in to submit a review.