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

C Functions

functions in c++

Uploaded by

harinipradeep15
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

C Functions

functions in c++

Uploaded by

harinipradeep15
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

What Are C++ Functions?

A function is defined as a group of statements or a block of code that carries out specific tasks.

Function - a subprogram that can act on data accepted as an input in the form of parameter, dose
some processing and return a value

Function has a specific purpose and a name

Every C++ program has at least one function, main(), where program execution begins and
Terminates and it might contain more than one function.
Why Use Functions in C++?

Functions are used to minimize the repetition of code, as a function allows you to write the code
inside the block. And you can call that block whenever you need that code segment, rather than
writing the code repeatedly. It also helps in dividing the program into well-organized segments.
Functions in C++ come in two
varieties:
Functions could be:
1. Pre-defined(built-in) library functions: Function which is pre-defined in the library. E.g:
pow(), sqrt(), cin, sin, tan etc..
2. Programmer-defined functions: the function which is made by the user (e.g., my_printf,area)
Elements of Function
These functions require three elements:
◦ Function declaration: used to rate the function
◦ Function calls: Invoke the function to execute the function
◦ Function definition: In this program will defines that how the function will perform there task.
Function Syntax

The syntax for creating a function is as follows:

Here, the return type is the data type of the value that the function will
return. Then there is the function name, followed by the parameters which
are not mandatory, which means a function may or may not contain
parameters.
Example:
Int Average(int x, int y, int z)
{
//function body
Declaration:

A function can be declared by writing its return type, the name of the function, and the
arguments inside brackets. It informs the compiler that this particular function is present. In C+
+, if you want to define the function after the main function, then you have to declare that
function first
Int avg(int s1,int s2,int s3);
Definition:

A function definition specifies the body of the function. The declaration and definition of a
function can be made together, but it should be done before calling it.
Int avg(int s1,int s2,int s3);
{
return(s1+s2+s3)/3;
}
Calling:
When you define a function, you tell the function what to do and to use that function; you have to
call or invoke the function. When a function is called from the main function, then the control of the
function is transferred to the function that is called. And then that function performs its task. When
the task is finished, it returns the control to the main function.
There are two types of functions in C++
Built-in functions
User-defined functions
Built-in Functions:
These are functions that are already present in C++; their definitions are already provided in the
header files. The compiler picks the definition from header files and uses them in the program.
User-defined Functions:

These are functions that a user creates by themselves, wherein the user gives their own
definition.
You can put them down as the following types:
No argument and no return value
No argument but the return value
Argument but no return value
Argument and return value
No argument and no return value: In this type, as the name suggests, it passes no arguments to
the function, and you print the output within the function. So, there is no return value.

No argument but return value: In this type, it passes no arguments to the function, but there is a
return value.
Argument but no return value: In this type of function, it passes arguments, but there is no
return value. And it prints the output within the function.
Argument and return value: In this type of function, it passes arguments, and there is also a
return value.
Function with no arguments and a return value:

Below is an example of a function, which takes 2 numbers as input


from user, and display which is the greater number.

#include<iostream.h> int greatNum() // function definition


// function declaration { int i, j, greatnum;
int greatNum(); cout<<"Enter the no to compare.";
int main() cin>>i>>j;
{ if(i > j) {
int res; greatnum=i;
// function call }
res = greatNum(); else {
cout<<" greater- no is”<< res; greatnum=j;
return 0; }
} return greatnum;
}
Function with no arguments and no return value: Such functions can
either be used to. display information or they are completely dependent
on user inputs
Below is an example of a function, which takes 2 numbers as input
from user, and display which is the greater number.

#include<iostream.h> // function definition


// function declaration void greatNum()
void greatNum(); { int i, j;
int main() cout<<"Enter the no you want to compare.";
{ cin>>i>>j;
// function call if(i > j) {
greatNum(); cout<<"The greater number is:”<<i;
return 0; }
} else {
cout<<"The greater number is:”<<j;
}
}
Function with arguments and no return value:

Below is an example of a function, which takes 2 numbers as input


from user, and display which is the greater number.

#include<iostream.h> // function definition


// function declaration void greatNum(int a, int b){
void greatNum(int a, int b); if(a > b) {
int main() cout<<" greater- number is:”<<a;
{ }
int a, b; else {
cout<<"Enter the no to compare."; cout<<" greater- number is:”<<b;
cin>>a>>b; }
// function call }
greatNum(a,b);
return 0;
}
Function with arguments and a return value: This is the best type, as
this makes the function completely independent of inputs and outputs,
and only the logic is defined inside the function body.
Below is an example of a function, which takes 2 numbers as input
from user, and display which is the greater number.
#include<iostream.h> // function definition
// function declaration int greatNum(int a, int b){
int greatNum(int a, int b); int greatnum;
int main() if(a > b) {
{ greatnum=a;
int a, b, res; }
cout<<"Enter the no to compare."; else {
cin>>a>>b; greatnum=b;
// function call }
res=greatNum(a,b); return greatnum;
cout<<" greater- no is”<< res; }
return 0;
}
Recursion function
Recursion in C++ is a technique in which a function calls itself repeatedly until a given condition
is satisfied. In other words, recursion is the process of solving a problem by breaking it down
into smaller, simpler sub-problems.
Syntax of recursion
return_type recursive_func {
....
// Base Condition
// Recursive Case
....
}
A function that calls itself is called a recursive function. When a recursive function is called, it executes a
set of instructions and then calls itself to execute the same set of instructions with a smaller input. This
process continues until a base case is reached, which is a condition that stops the recursion and returns a
value.
Base Condition
The base condition is the condition that is used to terminate the recursion. The recursive function will
keep calling itself till the base condition is satisfied.
Recursive Case
Recursive case is the way in which the recursive call is present in the function. Recursive case can contain
multiple recursive calls, or different parameters such that at the end, the base condition is satisfied and
the recursion is terminated.
Functions with Default Parameters
In C++ programming, you can provide default values for function parameters.
The idea behind default argument is simple. If a function is called by passing
argument/s, those arguments are used by the function.
But if the argument/s are not passed while invoking a function then, the default
values are used.
e.g. void add(int a, int b, int c, int d = 4);
All of the default parameters must be the rightmost parameters of the function
In a function call where the function has more than one default parameter and a value to
a default parameter is not specified
◦ You must omit all of the arguments to its right

Default values can be constants, global variables, or function calls


Working of default arguments
Common mistakes when using Default argument
void add(int a, int b = 3, int c, int d = 4);
◦ The above function will not compile. You cannot miss a default
argument in between two arguments.
◦ In this case, c should also be assigned a default value.
void add(int a, int b = 3, int c, int d);
◦ The above function will not compile as well. You must provide default
values for each argument after b.
◦ In this case, c and d should also be assigned default values.
◦ If you want a single default argument, make sure the argument is the
last one. void add(int a, int b, int c, int d = 4);
You cannot assign a constant value as a default value to a reference
parameter
Inline functions
An inline function is a function that is expanded in line when it is called. When the inline
function is called whole code of the inline function gets inserted or substituted at the point of
the inline function call. This substitution is performed by the C++ compiler at compile time. An
inline function may increase efficiency if it is small.
Syntax
inline return-type function-name(parameters)
{ // function code }
#include <iostream>
using namespace std;
inline int cube(int s) { return s * s * s; }
int main()
{
cout << "The cube of 3 is: " << cube(3) << "\n";
return 0;
}
Output the cube of 3 is 27
Function overloading
Function overloading is a feature of object-oriented programming where two or more functions
can have the same name but different parameters.
When a function name is overloaded with different jobs it is called Function Overloading
. In Function Overloading “Function” name should be the same and the arguments should be
different.
•Parameters should have a different type
add(int a, int b)
add(double a, double b)
In C++ programming, two functions can have same name if number and/or
type of arguments passed are different.
These functions having different number or type (or both) of parameters are
known as overloaded functions. For example:
◦ int test() { }
◦ int test(int a) { }
◦ float test(double a) { }
◦ int test(int a, double b) { }

Here, all 4 functions are overloaded functions because argument(s) passed


to these functions are different.
Overloaded functions may or may not have different return type but it
should have different argument(s).
Using different names to related function makes the situation more
Call by value and call by
reference in C++

Call by value in C++


In call by value, original value is not modified.
In call by value, value being passed to the function is locally stored by the function parameter in
stack memory location. If you change the value of function parameter, it is changed for the
current function only. It will not change the value of variable inside the caller method such as
main().
Call by reference in C++
In call by reference, original value is modified because we pass reference (address).
Here, address of the value is passed in the function, so actual and formal arguments share the
same address space. Hence, value changed inside the function, is reflected inside as well as
outside the function.

You might also like