Chapter 5 - Function
Chapter 5 - Function
6
User defined Functions
10
Calling a Function
• A function is executed when it is called.
x=AddTwoNumbers();
11
Function Definition
• A function definition contains the statements that make up
the function.
• All function definitions have the following parts:
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);
}
#include <iostream>
Example 4 on tion
cti nc
fu n fu
Using namespace std; , ain
le d
int add(int, int); //function declaration or prototypingncel ore m
s ca bef
int main() n i e
tio com
{ ar a to
ecl as
int a,b; I f d nh
. B: nitio
cout<<"Enter Two integers"<<endl; N efi
d
cin>> a >> b;
cout<<“Sum"<<a <<"and"<<b<<"is"<<add(a,b); // function call
return 0;
}
int add(int a, int b) // function definition
{ int sum;
sum=(a + b); // Replace function call with sum
return sum;}
#include<iostream>
Example 5
using namespace std;
float AddTwoNumbers(float, float); prototype
int main()
{
float n, m;
cout<<“Enter first number:”;
cin>>n;
cout<<“Enter second number:”;
cin>>m;
cout<<n<<“+”<<m<<“=”<<AddTwoNumbers( n, m );
return 0;
}
» File scope
» Block scope
Example
#include<iostream>
using namespace std;
int main()
{
int x=0;
for(int b=0;b<5;b++)
{
int k=15;
x+=b;
‘b’ & ‘k’ are accessible only
cout<<b;in this block
}
cout<<x;
cout<<b;
‘b’ & ‘k’ are not declared
cout <<k; in this scope
return 0;
}
Local and Global Variables
• Local Variable: are variables defined inside a function or
inside a block
– They are known only to the function
– Its scope is from variable definition to the end of the entire program
{ f2();
25
cout << x << endl;}
I. Using Global Variables
#include <iostream.h>
int x = 0;
void f1()
x =0
{ x++;}
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(); {
1 f2();
cout << x << endl; 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();
}
{
void main()
f2(); {
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++;
}
{
void f2()
x+=4; f1(); {
} x += 4;
3 f1();
void main() }
{
void main()
f2(); {
cout << x << endl; 1 f2();
cout << x << endl ;
} }
29
#include <iostream.h>
int x = 0;
void f1()
{
x 5
4
x++;
void f1()
} {
void f2() x++;
5 }
{
x+=4; f1(); void f2()
{
} x += 4;
void main() 3 f1();
}
{
void main()
f2(); {
cout << x << endl; 1 f2();
cout << x << endl;
} }
30
#include <iostream.h>
int x = 0;
void f1()
{ x 5
4
x++;
}
void f2()
{
void f2()
x+=4; f1(); {
} x += 4;
f1();
void main() 6 }
{ void main()
f2(); {
1 f2();
cout << x << endl; cout << x << endl;
} }
31
#include <iostream>
using namespace std;
int x = 0;
void f1()
x 5
4
{
x++;
}
void f2()
{
x+=4; f1();
}
void main()
void main()
{ {
f2(); f2();
7 cout << x << endl;
cout << x << endl;} }
32
#include <iostream.h>
int x = 0;
void f1()
{
x 5
4
x++;
}
void f2()
{
x+=4; f1();
}
void main()
{
void main()
f2(); {
cout << x << endl; f2();
cout << x << endl;
} 8 }
33
#include <iostream>
int x = 0;
void f1()
{
x++;
}
void f2()
{
x+=4; f1();
}
int main()
{
f2();
cout << x << endl;
}
34
Parameters Pass in a
Function
Parameters Pass/Call by Value
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 parameters to the function.
mapped to the copies of The address of the
the arguments created. argument is copied into the
The changes made to the parameter.
parameter do not affect The changes made to the
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
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