Chapter 5 - Function
Chapter 5 - Function
Chapter-5
Function
1
Why use Function?
You can simplify programming tasks by breaking programs into smaller
logical components.
Function are used for divide a large code into module, due to this we can
easily debug and maintain the code.
E.g. if we write a calculator programs at that time we can write every
logic in a separate function [For addition sum(), for subtraction sub()].
Any function can be called many times.
Code Re-usability
Develop an application in module format.
Easily to debug the program.
Code optimization: No need to write lot of code.
2
Function Definitions
Function definition: statements that make up a function
A function is a group of statements that together perform a
specific task.
Every C++ program has at least one function, which is main().
Every function that is called in a program must be defined
somewhere (once only).
The syntax is: return_type function_name(parameter)
{
function body;
}
3
Cont…
Definition includes:
return type: data type of the value that function returns to the
part of the program that called it
name: name of the function. Function names follow same
rules as variables
parameter list: variables containing values passed to the
function
body: statements that perform the function’s task, enclosed
in {}
4
Cont…
5
Function Return Type
If a function returns a value, the type of the value must
be indicated:
int main()
If a function does not return a value, its return type is
void:
void printHeading()
{
cout << "Monthly Sales\n";
}
6
Function Declarations
A function declaration is the process of tells the compiler about a
function name.
Then, the actual body of the function can be defined separately.
The syntax is: return_type function_name(parameter);
7
Function Calling
The syntax is: function_name(); // or
variable=function_name(argument);
8
Cont…
Example: sum() function
#include<iostream.h>
Print out:
void sum(); // declaring a function
int a = 11, b = 22, c;
Sum: 33
void main()
{
sum(); // calling the function
}
10
Type of Function
There are two type of function in C++ Language. They
are:
Library function or pre-define function.
User defined function.
11
Library Function
Library functions are the built-in function in C++ programming.
Programmer can use library function by invoking function
directly; they don't need to write it themselves, for example:
#include <iostream>
#include <cmath> // includes the content of cmath file.
using namespace std;
int main() Print out:
{ Enter a number: 26
double number, squareRoot;
Square root of 26 = 5.09902
cout << "Enter a number: ";
cin >> number;
// sqrt() is a library function to calculate square root.
squareRoot = sqrt(number);
cout << "Square root of " << number << " = " << squareRoot;
return 0;
}
12
Sending Data into a Function
Can pass values into a function at time of call:
c = pow(a, b);
13
A Function with a Parameter Variable
void displayValue(int num)
{
cout << "The value is " << num << endl;
}
14
Passing value or data to function
There are two ways to pass value or data to function.
call by value
call by reference
Call by value: In call by value, original value can not be changed or
modified.
Call by reference: In call by reference, original value is changed or
modified because we pass a reference (an address). So actual and
formal arguments shares the same address space. Hence, any
value changed inside the function, is reflected inside as well
as outside the function.
15
Cont…
The value of num1 and num2 are initialized to
variables a and b respectively. These
arguments a and b are called formal arguments. For
example:
16
Return Statement
A function can return a single value to the calling program using
return statement. In the previous program, the value of add is
returned from user-defined function to the calling program using
statement below:
17
Returning a Value From a Function
A function can return a value back to the statement that called
the function.
You've already seen the pow function, which returns a value:
double x;
x = pow(2.0, 10.0);
18
Cont…
In a value-returning function, the return statement can be used
to return a value from function to the point of call. Example:
19
A Value-Returning Function
Return Type
int sum(int num1, int num2)
{
double result;
result = num1 + num2;
return result;
}
20
Cont…
int sum(int num1, int num2)
{
return num1 + num2;
}
21
Cont…
Example: Call by Value function
#include<iostream.h>
void main(){
int a=111, b=222;
swap(a, b); // passing value to function
cout<<"Value of a: "<< a <<endl;
cout<<"Value of b: "<< b <<endl;
}
22
Cont…
Example: Call by Reference function
#include<iostream.h>
void main(){
int a=111, b=222;
swap(&a, &b); // passing value to function
cout<<"Value of a: "<< a <<endl;
cout<<"Value of b: "<< b <<endl;
}
23
Summary: Argument Passing
Arguments passed by value and by reference.
For example:
int i=1;
24
Difference Between Call by Value and Call by Reference.
25
Function Overloading
Two or more functions having same name but different argument(s) are
known as overloaded functions.
In Function Overloading “Function” name should be the same and the
arguments should be different.
Here, all 4 functions are overloaded functions called “test” because
argument(s) passedintto these
test()functions
{ } are different.
For example: int test(int a) { }
float test(double a) { }
int test(int a, double b) { }
N.B., the return type of all these 4 functions are not same. Overloaded
functions may or may not have different return type but it should have
different argument(s).
26
Cont…
double test(int a) { }
int test(int b) { } // ERROR CODE!!!
27
28
?