Chapter 5 Function
Chapter 5 Function
6
User defined Functions
10
Calling a Function
• A function is executed when it is called.
• Function main is called automatically when a program starts,
but all other functions must be executed by function call
statements.
• A function call is simply the name of the function followed by a
set of arguments enclosed in parentheses and a semicolon.
• E.g.
x=AddTwoNumbers();
11
Function Definition
• A function definition contains the statements that make up
the function.
• All function definitions have the following parts:
• Return type ,Name, Parameter list and Body :
12
Syntax to create a function
type FunctionName( type Parameter1, type
Parameter 2,…)
{
//Body of function goes here
}
Example: 1) int areaOfRectangle(int base, int height)
{
int area=base*height;
return area;
}
2) float cube(float x)
{
float cube=x*x*x;
return cube;
}
Example 1
#include <iostream>
using namespace std;
void greet() --------------------------------------- //3rd
{
cout << "Hello there!"; ---------------------------//4th
}
int main() -------------------------------------------//1st
{
greet(); -------------------------------------------//2nd
return 0; ------------------------------------------//5th
}
Example 2
#include<iostream>
using namespace std;
Function prototyping
float AddTwoNumbers();
int main()
{
float x;
Function calling
x=AddTwoNumbers();
cout<<“a+b=”<<x;
return 0;
} Function definition
float AddTwoNumbers()
{
float a=2.5;
float b=7.5;
float c=a+b;
return c;
}
Example 3
#include< iostream>
using namespace std;
int sum (int x, int y); //declaring the function
int main()
{
int a = 10;
int b = 20;
int c = sum (a, b); //calling the function
cout << c;
}
int sum (int x, int y) //defining the function
{
return (x + y);
}
h a
n
#include <iostream>
Example 4 fin
i tio
d e
Using namespace std; tion
n c
u
int add(int, int); //function declaration or prototypinglled, f
n ce on
int main() a ti
i s c fu n c
{ tion ain
l ara m
int a,b; d ec fore
: If e be
B
cout<<"Enter Two integers"<<endl; N. com
cin>> a >> b; to
void f2()
{
x+=4;
f1();
}
int main()
{
f2();
cout << x << endl;
}
26
#include <iostream.h>
int x = 0;
void f1()
{ x 0
x++;
}
void f2()
{
x+=4; f1();
}
int main()
{
void main()
f2(); {
cout << x << endl; 1 f2();
} cout << x << endl ;
}
27
#include <iostream.h>
int x = 0;
void f1()
{
x 0
4
x++;
}
void f2()
{
x+=4; f1(); void f2()
} {
2 x += 4;
void main() f1();
{ }
f2(); void main()
{
cout << x << endl; 1 f2();
} cout << x << endl ;
}
28
#include <iostream.h>
int x = 0;
void f1()
{
x 5
4
x++;
} void f1()
{
void f2() 4 x++;
{ }
x+=4; f1(); void f2()
{
} x += 4;
void main() 3 f1();
{ }
Return by Reference
Parameters Pass
Pass/Call by value Pass/Call by reference
Copies of the arguments are Pass by reference is the
created . second way of passing
The parameters are mapped parameters to the function.
to the copies of the The address of the
arguments created. argument is copied into the
The changes made to the parameter.
parameter do not affect the The changes made to the
arguments. parameter affect the
arguments.
It uses & symbol for
representation
36
Argument passing By Argument passing By
Value Reference
We call function addition passing We call function addition passing
{
int divide(int a,int b)
Function overloading
return (a/b);
}
Are functions with the same
float divide(float a,float b) name having:
{
return(a/b); Different number of
} argument
int main()
Different type of argument
{
int x=5,y=2; float n=5.0,m=2.0; Both
cout<<divide(x,y);
cout<<"\n";
cout<<divide(n,m);
return 0;}
Example 1 Example 2
int addition (int a, int b) #include<iostream>
using namespace std;
{ int r; int addition (int a, int b)
r=a+b; {
int r;
return (r);} r=a+b;
float addition (float a, float b) return (r);}
{ float r; int addition (int a,int b,int c)
{
r=a+b; int r;
return (r);} r=a+b+c;
return (r);
int main () }
{int z; float y; int main ()
{
z = addition (5,3); int z;
y = addition (5.5,3); float y;
cout << "The result is " << z= addition (5,3);
y = addition (5.5,3);
z<<endl<<y; cout << "The result is " << z<<endl<<y;
return 0;} return 0;}
Exercise 1
#include<iostream>
using namespace std;
int area(int length,int width);
int main()
{int l;
int w;
int areaofrect;
cout<<"enter lenth of the rectangle:";
cin>>l;
cout<<"Enter width of the rectangle:";
cin>>w;
areaofrect=area(l,w);
cout<<"Area of the rectangle:"<<areaofrect<<endl;
return 0;
}
int area( int l,int w)
{
return l*w;}
Exercise 2:
#include<iostream>
using namespace std;
int AreaCube(int length,int width=5,int height=1);
int main()
{int width=2;
int height=3;
int area;
area = AreaCube(length, width, height);
cout<<"First area equals:"<<area<<"\n";
area = AreaCube(length, width);
cout<<"second area equals:"<<area<<"\n";
area = AreaCube(length);
cout<<"Third area equals:"<<area<<"\n";
return 0;}
AreaCube(int length, int width, int height)
{
return(length * width * height);
}
Recursive functions:
• Is Function that calls themselves.
Example 6
long factorial(long n)
{
if(n==0 )return 1;
else
return n*factorial(n-1);
}
Or
int Factorial (unsigned int n)
{
return n == 0 ? 1: n * Factorial (n-1);
}
Recursively
• //factorial calculator
#include<iostream.h>
long factorial (long a)
{
if(a>1)
return (a* factorial(a-1));
else
return 1;
}
int main()
{
long l;
cout<<"Type a number:";
cin>>l;
cout<<"!"<<l<<"="<<factorial(l);
return 0;}
Passing array as argument to a function
We pass both array and its size to a
function.
Array are passed as reference variable
without (&)
Example.
double read(int a[],int size);
When we call function with array parameter
we simply pass array name and size.
read(a,size);
Example
#include <iostream>
using namespace std;
void printArray(int arr[], int size)
{
for (int i = 0; i < size; i++)
cout << arr[i] << " "<<endl;
}
int main()
{
int myArray[] = {1, 2, 3, 4, 5};
int arraySize=sizeof(myArray) / sizeof(myArray[0]);
printArray(myArray, arraySize);
return 0;
}
Exercise on functions
1. What are the differences between the function prototype and the function
definition?
The function prototype declares the function; the definition defines it. The
prototype
ends with a semicolon; the definition need not.
The declaration need not include names for the parameters; the definition
must.
57
5. What is a local variable?
A local variable is a variable passed into or declared
within a block,
6. What is scope?
Scope refers to the visibility and lifetime of local and
global variables.
Scope is usually established by a set of braces.
7. What is recursion?
Generally refers to the ability of a function to call itself.
8. When should you use global variables?
typically used when many functions need access to the
same data.
58
Exercise on Array
1. What are the first and last elements in SomeArray[25]?
2. How do you declare a multidimensional array?
eg int Array[5][3];
3. Initialize the members of an array declared as SomeArray[2][3]
[2].
For example, SomeArray[2][3][2] is
a three-dimensional array. The first dimension has two elements,
the second has three, and the third has two.
3. SomeArray[2][3][2] = { { {1,2},{3,4},{5,6} }, {7,8},{9,10},{11,12}}};
4. How many elements are in the array SomeArray[10][5][20]?
5. How does a linked list differ from an array?
6. How many characters are stored in the string “Jesse knows C+
+”?
16 characters = 13 letters+2 space+one null char
7. What is the last character in the string “Brad is a nice guy”? Null
character 59
Exercise on loops
C++ POINTERS (2020) - Introduction to C++ pointers (for beginners) PROGRAMMING TUTORIAL - YouTube
61