For C++ Jatin
For C++ Jatin
Prototype
By- Jatin Kaushik
Function
• A function is a block of
code which only runs when it is
called. You can pass data, known
as parameters, into a function.
Functions are used to perform
certain actions, and they are
important for reusing code.
Function prototype
Here,
• return type - int
• name of the function - sum
• argument list - (int n1, int n2)
• By declaring a function's return type void, one makes sure that the
function cannot be used in an assignment statement.
Tip - If a function does not return a result, declare the result type as
void.
Use of void
• A function that does not require any
parameter (i.e., it has an empty
argument list) can be declared as
follows:
.
or
• Within the definition of calling
function, such prototypes are
known as local prototypes.
Function prototype example
• Let's take an example program, demonstrating function prototype and
function definition in C++.
/* C++ Function Prototype and C++ Function Definition */
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int add(int, int); // function prototype
int subtract(int, int); // function prototype
int multiply(int, int); // function prototype
int divide(int, int); // function prototype
void main()
{
clrscr();
int a, b;
cout<<"Enter any two number: ";
cin>>a>>b;
cout<<"\nSummation = "<<add(a, b);
cout<<"\nSubtraction = "<<subtract(a, b);
cout<<"\nMultiplication = "<<multiply(a, b);
cout<<"\nDivision = "<<divide(a, b); getch();
}
int add(int x, int y) // function definition
{
int res; res = x + y; return res;
}
int subtract(int x, int y) // function definition
{
int res; res = x - y;
return res;
}
Sample runs of the program
Thank you