0% found this document useful (0 votes)
23 views

For C++ Jatin

Uploaded by

Jatin Kaushik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

For C++ Jatin

Uploaded by

Jatin Kaushik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Function

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

• It is a declaration of the function


that tells the program about the
type of the value returned by the
function and the number and type
of arguments.
Function Definition and
Declaration
• The prototype declaration
looks just like a function
definition except that it has
no body.
• A declaration introduces a
(function) name to the
program whereas a
definition is a declaration
that also tells the program
what the function is doing
and how it is doing.
Function Declaration Example

Here,
• return type - int
• name of the function - sum
• argument list - (int n1, int n2)

Since every function prototype is followed by a semicolon, so at last there will be ;


as in the above function prototype.
Tip - A function declaration can skip the argument names but a function definition,
can not.
Use of Void
• It specifies an empty set of values, and it is used as the return type for
functions that do not return a value.
• A function that does not return a value is declared as follows:

• 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:

Tip - If a function takes no argument, you


should specify void in its prototype.
Global and local
prototypes
• A function prototype can
either appear before the
definition of calling the
function, such prototypes are
known as a global prototypes.

.
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

You might also like