VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
Functions in c++
1. Amity School of Engineering & Technology
1
FUNCTIONS
Presented By:
Shivani Dubey
Priyanka
Yadav
Maaz Hasan
2. Amity School of Engineering & Technology
CONTENTS• Introduction.
• The Main Function.
• Function Prototyping.
• Call by Reference.
• Return by Reference.
• Inline Function.
• Default Arguments.
• Constant Arguments.
• Recursion.
• Function Overloading.
• Friend and Virtual Function.
• Math Library Function. 2
3. Amity School of Engineering & Technology
INTRODUCTION
• Divide and conquer :
– Construct a program from smaller pieces or components.
– These smaller pieces are called modules.
– Each piece more manageable than the original program.
– Building blocks of C++.
3
4. Amity School of Engineering & Technology
• void show(); //function declaration
main()
{
….
show(); //function call
….
}
void show(); //function definition
{
… //function body
…
} 4
AN EXAMPLE
5. Amity School of Engineering & Technology
THE MAIN FUNCTION
5
• When a program begins running,
the system calls the function main,
which marks the entry point of the
program.
• By default, main has the storage
class extern.
• Every program must have one
function named main, and the
following constraints.
6. Amity School of Engineering & Technology
FUNCTION PROTOTYPING
• A Function Prototype in C++ is a declaration of
a function that omits the function body but does specify the
function's return type, name and argument types. While a
function definition specifies what a function does, a function
prototype can be thought of as specifying its interface.
6
7. Amity School of Engineering & Technology
CALL BY REFERENCE
• The Call by Reference method of passing arguments to a
function copies the reference of an argument into the formal
parameter. Inside the function, the reference is used to access
the actual argument used in the call. This means that changes
made to the parameter affect the passed argument.
7
8. Amity School of Engineering & Technology
AN EXAMPLE
8
#include <iostream>
using namespace std;
// function declaration
void swap(int &x, int &y);
int main ()
{ // local variable declaration:
int a = 100; int b = 200;
cout << "Before swap, value of a :" << a << endl;
cout << "Before swap, value of b :" << b << endl; /* calling a
function to swap the values using variable reference.*/
swap(a, b); cout << "After swap, value of a :" << a << endl;
cout << "After swap, value of b :" << b << endl;
return 0;
}
9. Amity School of Engineering & Technology
RETURN BY REFERENCE
• A C++ program can be made easier to read and maintain by
using references rather than pointers. A C++ function can
return a reference in a similar way as it returns a pointer.
• When a function returns a reference, it returns an implicit
pointer to its return value. This way, a function can be used on
the left side of an assignment statement.
9
10. Amity School of Engineering & Technology
INLINE FUNCTION
• An Inline Function the programme
That has requested that the compiler insert
the complete body of the function in
every place that the function is called,
rather than generating code to call the
function in the one place it is defined.
10
11. Amity School of Engineering & Technology
DEFAULT ARGUEMENTS
• In computer programming, a default argument is an
argument to a function that a programmer is not required to
specify. In most programming languages, functions may take
one or more arguments.
• The default argument must be implicitly convertible to the
parameter type.
11
12. Amity School of Engineering & Technology
CONSTANT ARGUEMENTS
• The const variable can be declared using const keyword.
• The const keyword makes variable value stable.
• The constant variable should be initialized while declaring.
Syntax:
• (a) const <variable name> = <value>;
• (b) <function name> (const <type>*<variable name>;)
• (c) int const x // in valid
• (d) int const x =5 // valid
12
13. Amity School of Engineering & Technology
RECURSION
• Recursive functions:
– Functions that call themselves
– Can only solve a base case
– Divide a problem up into
• What it can do.
• What it cannot do.
• What it cannot do resembles
• original problem.
• The function launches a new copy of itself
(recursion step) to solve what it cannot do
13
14. Amity School of Engineering & Technology
FUNCTION OVERLOADING
• Function overloading is a feature found in that allows
creating several methods with the same name which differ
from each other in the type of the input and the output of the
function. It is simply defined as the ability of one function to
perform different tasks.
14
15. Amity School of Engineering & Technology
FRIEND & VIRTUAL FUNCTION
15
• A Friend Function access to private and protected data in that
class that it would not normally be able to as if the data was public.
• A Function of global or namespace scope may be declared as friend
of both classes.
• A Virtual Function is a function or method whose behaviour can
be overridden within an inheriting class by a function with the
same signature. This concept is an important part of
the polymorphism portion of Object-Oriented Programming (OOP).
16. Amity School of Engineering & Technology
MATH LIBRARY FUNCTION
• C Mathematical Operations are a group of functions in
the Standard Library of the C programming
language implementing basic mathematical functions.
16
17. Amity School of Engineering & Technology
REFERENCES
• Wikipedia.com.
• C++ By Yashwant Kanetkar.
• C++ By E Balagurusamy.
17