Programming_Week5
Programming_Week5
cout<<"*";
}
cout<<"\n";
}
return 0;
On each iteration of upper loop inner loop
} complete its iterations from start to end
Example
using namespace std;
#include<iostream>
int main(){
for(int i=1;i<=10;i++){
for(int j=1;j<=i;j++){
cout<<"*";}
cout<<endl;
}
return 0;}
Example
using namespace std;
#include<iostream>
int main(){
for(int i=10;i>=0;i--){
for(int j=1;j<=i;j++)
cout<<"*";
cout<<endl;
}
return 0;
}
Exampleusing namespace std;
#include<iostream>
int main(){
for(int i=1;i<=10;i++){
for(int j=1;j<=10-i;j++){
cout<<" ";
}
for(int j=1;j<=i;j++){
cout<<"*";
}
cout<<endl;
}
return 0;
Code
Arrangement
Structured Approach
Styles
Action oriented approach
Consistof blocks(function)+
Structures
Object Oriented Approach
Consist of classes
Class
is type object( action+
properties)
Using Function
Three important aspects for
using functions:
1. Function Definition(code
of function):
It is just body of function which
is mostly written below the
main function
2. Function declaration
3. Function call
User-defined Functions
(Definition)
return-value-type function-
name(parameter-list)
{
declarations and statements
}
Return-Type: No(void) return or type
Function-name: Any(identifier rules)
Parameter: No(void) or no. of
parameters with types
Body of the function
Simple Function
(Function Definition)
void line( ){
for(int i=0;i<45;i++)
cout<<“*”;
cout<<endl;
}
Function
declaration
Function declaration is given if
the function definition written
below the main function
Function declaration consists
of:
void line();
Function call
A function is executed only when it
is called
Mostly a function is called inside
the main (or in other function)
Code written inside function will
never execute if it is not called
line();
#include <iostream>
void line( ); \\ Function Declaration
using namespace std;
int main ( ) {
Using Function
line( ); \\ Function Call
cout<<"Data Type Memory Reserved\n";
line(); \\ Function Call
cout<<"bool 1 Bit"<<endl
<<"char 1 Byte"<<endl
<<"int 4 Byts"<<endl;
line( ); \\ Function Call
return 0;
}
void line( ){
for(int i=0;i<40;i++)
Function
cout<<"*"
Definition
cout<<endl;
}
Example
• void line(){
#include<iostream> • for(int i=0;i<45;i++)
void line(); • cout<<"*";
void hash(); • cout<<endl;
void dash(); •}
int main(){ • void hash(){
dash(); • for(int i=0;i<45;i++)
for(int i=0;i<5;i++) • cout<<"#";
line();
• cout<<endl;
• }
dash();
• void dash(){
for(int i=0;i<5;i++)
• for(int i=0;i<45;i++)
hash();
• cout<<"-";
return 0; • cout<<endl;
} •}
Example
Write a function which input 5 integers and
calculate•average
#include <iostream>
• void average();
• using namespace std;
• int main() {
• average();
• return 0;
•}
• void average(){
• int a,b,c,d,e;
• cout<<"\n Enter Five
Numbers:";
• cin>>a>>b>>c>>d>>e;
•
Example Write a function which input an integer number
and tells whether it is even or odd (using switch)
• #include <iostream>
• void evenOdd();
• using namespace std;
• int main() {
• evenOdd();
• return 0;
•}
• void evenOdd(){
• int number;
• cout<<"\n Enter a Number:";
• cin>>number;
• switch(number%2){
• case 0: cout<<"\nNumber is even";
break;
• case 1: cout<<"\n Number is odd";
•
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