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

Programming_Week6

The document covers various concepts related to functions in programming, including function definitions, variable scope, passing values to functions, and the return keyword. It explains different types of variable scopes (global, function-level, and block-level), how to pass arguments by value and by reference, and introduces default arguments and function overloading. Examples are provided to illustrate these concepts, including function implementations for addition, finding the smallest integer, and swapping values.

Uploaded by

ayeshanoor777325
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Programming_Week6

The document covers various concepts related to functions in programming, including function definitions, variable scope, passing values to functions, and the return keyword. It explains different types of variable scopes (global, function-level, and block-level), how to pass arguments by value and by reference, and introduces default arguments and function overloading. Examples are provided to illustrate these concepts, including function implementations for addition, finding the smallest integer, and swapping values.

Uploaded by

ayeshanoor777325
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

Last Week….

 Functions
 Function Definition
 Declaration
 Function Call
 Scope of the variable
 Global Variable
 Function Level Variable
 Block Level variable
Passing Values To
functions
During function call we send
values which a function can
accept
 We can pass any type of
value to the function
 We have to define the data
type of the values in function
definition
Passing Values to a Function
 void add(int a, int b); OR void add (int, int);
 int main(){
 add (7,8);
 add (9,10);
 int x=5, y=10;
 add (x,y);
 return 0;
 }
• void add( int a, int b){
• int add;
• add= a+b;
• cout<<“ Addition is:
“<<add<<endl;
Problem

Write a function which


takes three integers as
parameters and display the
smallest integer
 #include<iostream>
 using namespace std; Example
 void s(int,int,int );
 int main(){
 s(123,135,140);
 int a,b,c;
 cin>>a>>b>>c;
 s(a,b,c);
 return 0;
}
 void s(int a,int b,int c){
 int small=a;
 if(b<small)
 small=b;
 if(c<small)
 small=c;
 cout<<"Samll
Functions With Return
Value
 Function can return single
value to the calling function
 We Use keyword return for
this purpose
 We have to define the data
type of value returned by the
function
 Function can return only one
value
 Function return value and
control to the calling function
Example
 #include<iostream>
 using namespace std;
 int add(int,int,int,int);
 int main(){
 add(2,2,2,2);
 int c;
 c=add(2,2,2,2);
 cout<<"Add is:"<<c<<endl;
 cout<<"Add is:"<<add(3,3,3,3);
 return 0;
 }
 int add(int a,int b,int c,int d){
 int sum=a+b+c+d;
 return sum; \\ can also written as:
return a+b+c+d;
 }
return Keyword
 Returnkeyword used to
perform two activities:
Return the Control
Return the Value & Control
 Only one return is executed
 Nocode statements after the
execution of return is
executed
 #include<iostream> Example
 using namespace std;
 int small(int,int);
 int main(){
 int x,s;
 cout<<"Enter two values:";
 cin>>x>>s;
 int z=small(x,s);
 cout<<"\nSmall value is:"<<z;
 return 0;
}
 int small(int x,int y){
 (x<y )? return x: return y;
}
return Keyword (Example)
 #include • void evenOdd(){
<iostream> • globalVariable=20;
 void evenOdd(); • cout<< globalVariable;
 using namespace • int number;
std;

• cout<<"\n Enter a
int
Number:";
globalVariable;

• cin>>number;
int main() {

• if(number==1) {
evenOdd();
 • cout<<"Number
globalVariable=1
is 1"; return;
0;
 cout<<
• }
globalVariable; • cout<<"Number is not
Returning control by

functions
A function can end its
execution in three ways:
1. By facing the ending
brace } of function
2. After executing return
statement
3. After returning a value
Scope of Variable
Scope of the Variable

 Scope mean visibility


(Accessibility )
 Visibility is downward OR after
variable declaration
A variable declared inside a block
has visibility within that block only
 Variables defined within the
function has a scope that is
Visibility of Variable (Scope of
Variable)
 Global Scope/Global Variable:
Anything identified or declared
outside of any function is visible to
all functions in that file
 Function level scope/ Function
Level Variable Declaring
variables inside a function can be
used in the whole function
 Block level scope/Local Variable
Variables or integers declared
inside block are used inside block
Global Variable

Function Variable

Local Variable
 #include<iostream>


using namespace std;
int add(int,int,int,int);
Example
 int mul(int,int,int,int);
 int min(int,int,int,int);
 int g1=10;
 int main(){
 int c=add(2,2,2,2);
 cout<<“Multiplication:"<< mul(2,2,2,2);
<<endl;
 cout<<“Subtraction:"<< min(2,2,2,2);
<<endl;
 return 0;
 }
 int g2=11;
 int add(int a,int b,int c,int d){
 return a+b+c+d;
 }
 int g3=12;
 int mul(int a,int b,int c,int d){
 return a*b*c*d;
 }
 int g4=13;
 int min(int a,int b,int c,int d){
Reference Variables
 Reference
variables are alias
or another name of variables
 Defined using & operator
 Must be declared and
initialized in same statement
Reference Variables
 #include <iostream>
 using namespace std;
 int main() {
 int a=10;
 int &n=a;
 cout<<a<<endl;
 cout<<++n<<endl;
 cout<<a++<<endl;
 cout<<n;
 return 0;
 }
Passing Arguments to a
Function
 Arguments are passed in two
way
Call by Value
Call by Reference
Call by Value

Problem: Write a function which


take two value as argument and
then swap the values.
 using namespace std;
 #include<iostream>
 void swap(int,int);
 int main(){
 int x=7,y=9;
 swap(x,y);
 cout<<"\n x is:"<<x<<" y is:"<<y;
 return 0;
 }

• void swap(int a,int b){


• int temp=a;
• a=b;
• b=temp;

Call by reference
 We also can pass the value by
reference
 In
this case we pass reference of
the variable rather than value
 Weuse & operator for this
purpose
 To
call by reference we cannot
pass value, we have to pass
memory address of variable
Call By Value Call By Reference
#include<iostream> #include<iostream>
using namespace std; using namespace std;
void f( int x){ void f( int &x){
x=40; x=40;
} }
int main(){ int main(){
int a=10; int a=10;
cout<<“a=“<<a<<en cout<<“a=“<<a<<en
dl; dl;
f(a); f(a);
cout<<“a=“<<a<<en cout<<“a=“<<a<<en
dl; dl;
Swap Function with reference
 void swap(int& a,int& b)
{
 int temp=a;
 a=b;
 b=temp;
}
 #include<iostream>
 void swap(int&,int&);
 int main(){
 int x=7,y=9;
 cout<<"\n x is:"<<x<<" y
is:"<<y;
 swap(x,y);
 cout<<"\n x is:"<<x<<" y
is:"<<y;
 return 0;
}
Default
Arguments of
Functions
Default Arguments
 Functioncall commonly pass a
particular value of an argument
we cannot skip value
 Programmer can provide default
value for arguments from right
to left
 When that argument is omitted
in program the compiler use the
default value
Default Arguments (Example)
 #include<iostream>
 using namespace std;
 int add(int a,int b,int c=0,int d=0,int e=0){
 return a+b+c+d+e;
 }
 int main(){
 cout<<"\nCall with two arguments:"<<add(4,5);
 cout<<"\nCall with three arguments:"<<add(4,5,6
 cout<<"\nCall with four arguments:"<<add(2,3,4,5
 cout<<"\nCall with five arguments:"<<add(1,3,4,3
 return 0;
 }
 #include<iostream> Example
 using namespace std;
 int add(int a,int b,int c=0,int d=0,int
e=0);
 int main(){
 cout<<"\nCall with two
arguments:"<<add(4,5);
 cout<<"\nCall with three
arguments:"<<add(4,5,6);
 cout<<"\nCall with four
arguments:"<<add(2,3,4,5);
 cout<<"\nCall with five
arguments:"<<add(1,3,4,3,3);
 return 0;
}

Exercise
 Writea five argument
function for multiplication
 Give default value to the
last three argument to the
last three variables
 Call it in main differently
Function
Overloading
Function Overloading
 Function is identified with:
name
no of arguments and their
type
 Definingmultiple functions with
same name but different
arguments is function
overloading
 Compiler calls appropriate
function according to the
Function Overloading (Example)
• int add(int a,int b){ return a+b; }
• float add(float a,float b){ return a+b; }
• int add(int a,int b,int c){ return a+b+c; }
 #include<iostream>
 usingnamespace std;
 int main(){
 float x=3.5,y=5.6;
 cout<<"\nCall with integer:"<<add(4,5);
 cout<<"\nCall with floats:"<<add(x,y);
 cout<<"\nCall with 3
arguments:"<<add(2,3,4);
 return 0;

You might also like