0% found this document useful (0 votes)
6 views5 pages

Functions in C++

The document provides an overview of functions in C++, explaining their structure, types, and usage. It details the components of a function, including return type, function name, parameters, and statements, while distinguishing between built-in and user-defined functions. Additionally, it outlines four ways to define functions based on return type and parameters, along with syntax and examples for each type.

Uploaded by

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

Functions in C++

The document provides an overview of functions in C++, explaining their structure, types, and usage. It details the components of a function, including return type, function name, parameters, and statements, while distinguishing between built-in and user-defined functions. Additionally, it outlines four ways to define functions based on return type and parameters, along with syntax and examples for each type.

Uploaded by

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

Functions in C++

Function is a block of code that perform a specific task. Functions are used to avoid repetition of code.
We can make a function for a specific task and can use it repeatedly in the program.

Basic structure of a function looks like this:

< Return Type> FunctionName (parameters or arguements) //prototype of function


{
Statement(s); //body of function
}

Return Type: it is the value that return the function from the body. It could be int, float, char and bool.
If no return type it should be void.

FunctionName: Name is the identifier by which a function is called.

Parameters/arguement: Parameter or argument consists of a data type followed by an


identifier(variable). It could be simple variable as int x, char y, float z, bool a. Values of parameters are
passed in function when function is called.

Statement(s): statements are code that specify the task of the function

Built in function: There are many built in or predefined function available in C++. For example:
pow(x,y), sqrt(x), cos(x), log(y), gcd(x,y) prime(n) etc

User defined function: We can define our own function known as user defined function.

There are three things associated with function.


1. Defining a function
Functions are mostly defined after the main() function. However they can also be defined before
the main() function.
Terminator is not used while defining a function.

2. Declaration of a function
If a Function is defined after the main() its prototype must be declared before the main()
otherwise a compiler error occur.
Terminator is required when a functions is declared.

3. Calling a function
Functions are called in the main() function or in other functions.

A function can be defined in four ways.


1. Function with no return type and no parameter or argument.
void FunctionName (void) or void functionName( )
{
Input and output will be from the body of function
}

2. Functions with no type and have parameter or argument (data).


void FunctionName( int )
{
Input data will be given as argument and output will be from the
function body
}

3. Functions have type but no argument.


int FunctionNamel()
{
Input will be from the body and function will return the integer value
from the body.
}

4. Functions have return type and argument


int FunctionName( int )
{
Input integer will be given as argument and function will return an
integer value from the body
}

Syntax ans examples of four types of functions

1. Function with void return type and no parameter or argument.


In such a type of functions input and output will come from the body of the function.
Syntax:
void FunctionName (void)
{
Statement(s);
}
Examples-1 Example-2
//Function that print greater of two numbers //Function that print a message on calling it
#include<iostream> #include<iostream>
using namespace std; using namespace std;
void greaterno(); //Declaration of a function void print(); //Declaration of a function
main() void sum();
{ main()
greaterno(); //calling a function {
} print(); //calling
void greaterno() //definition sum();
{ }
int x,y; void print() //definition of function
cout<<"Enter the value of x and y:"; {
cin>>x>>y; cout<<”This is a C++ function\n”;
if(x>y) }
{ void sum() // another example of function
cout<<x<<endl; {
} int x, y;
else if(y>x)
{ cout<<"Enter x y:";
cout<<y<<endl; cin>>x>>y;
} cout<<”sum=”<<x+y;
}
}

2. Functions with void type and some arguments


In such a type of functions input data will be given as arguments or parameters and output will
come from the body of the function.

Syntax:
void FunctionName (arguments)
{
Statement(s);
}

Here return type of function is void and arguments will be some data of type (int, float, char, bool,
double, long double).

Examples-1 Examples-2
#include<iostream> /Function that print sum of two numbers
using namespace std; #include<iostream>
void greaterno(int x, int y); using namespace std;
main() void sum(int, int);
{ main()
greaterno(20, 30); {
} sum(5, 7);
void greaterno(int x, int y) //definition }
{ void sum(int a, int b)
if(x>y) {
{
cout<<x<<endl; cout<<”sum=”<<a+b;
} }
else if(y>x)
{
cout<<y<<endl;
}
}
3. Returnable Functions without arguments
In such a type of functions input data is given from the body and output is retuned from the body.
return command followed by data, variable or expression is used in the body of a function to
return data. The data is stored back to function and can be used for any other process.
The return statement stops execution and returns to the calling function. When a return statement
is executed, the function is terminated immediately at that point, regardless of whether it's in the
middle of a loop, etc.

Syntax:
<return type> FunctionName ( )
{
statement(s);
return (data/variable)
}

Here return type of function will be some data type (int, float, char, bool, double, long double).

Examples-1 Examples-2
//Function that return greater of two numbers Define a function that can get two numbers and
#include<iostream> return sum of these numbers.
using namespace std; #include<iostream>
int greaterno(); //Declaration of a function using namespace std;
main() main()
{
{
cout<<greaterno(); //calling a function
} cout<<sum();
int greaterno() //definition }
{ int sum()
int x,y; {
cout<<"Enter the value of x and y:"; int x,y;
cin>>x>>y;
cout<<"Enter x,y:";
if(x>y)
{ cin>>x>>y;
return x; return x+y;
}
}
else if(y>x)
{
return y;
}
}

Important Note: All mathematical, relational and logical operators can be used on
returnable function.

4. Returnable Functions with some arguments


In such a type of functions input data is given as arguement and output is retuned from the body
using return statement.

Syntax:
<return type> FunctionName (arguments ) //prototype of function
{
statement(s); //body of function
return (data/variabale);
}

Here as usual return type of function will be some data type (int, float, char, bool, double, long
double) and argument will also be some data type (int, float, char, bool, double, long double).

//Function that print greater of two numbers Define a function that can get two numbers as
#include<iostream> argument and return sum of these numbers.
using namespace std; #include<iostream>
int greaterno(int, int); //Declaration
using namespace std;
main()
{ int sum(int x,int y);
cout<<greaterno(10, 30); //calling
} main()
int greaterno(int x, int y) //definition {
{ Int a=2, b=5;
if(x>y) cout<<sum(10,20<<endl);
{
cout<<sum(a, b)<<endl;
return x;
} }
else if(y>x) int sum(int x, int y)
{ {
return y; return x+y;
}
}
}
The value of a is given to x and value of b is given
to y on calling the function

You might also like