Programming_Week6
Programming_Week6
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
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