lec-1
lec-1
lec-1
Lecture
Programing
1
Programming in C++
Error Messages
• Programs in C++ are written in blocks
• Every block has name
• The name of monitor block is main
• Every statement is ended with semi-colon(;)
• Our first program will display a message on output
screen
First Program
#include<iostream>
using namespace std;
int main() //start of the function
{
cout<<“Hello Word \n";
return 0; //Return Control to OS
}
Simple Program
output
#include<iostream>
Using namespace std;
int main()
{
int base,power,result=1;
cout<< " Plz enter base --> ";
cin>>base;
cout<< " Plz enter power --> ";
cin>>power;
for(int i=0;i<power;i++)
{
result=result*base;
cout<<"\nThe result is: "<<result;
}
return 0;
Nested Loops
Examples:
1- #include<iostream>
int main()
{
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
cout<<"*";
}
cout<<"\n";
}
return 0;
}
2- #include<iostream>
int main()
{
for(int i=1;i<=10;i++)
{
for(int j=1;j<=i;j++)
{
cout<<"*";
}
cout<<endl;
}
return 0;
}
•A function is a block of code which only runs when it is called. You can pass data,
known as parameters, into a function. Functions are used to perform certain actions, and
they are important for reusing code: Define the code once, and use it many times.
1)Library Functions
2)User defined Functions
Library Functions:
Library functions are the built-in functions in C++. Programmers can use library function by
invoking function directly. They don’t need to write it by themselves.
User defined Functions:
C++ allows programmers to define their own functions. A user defined function group code to
perform a specific task and that group of code given a name (identifier). When the function is
invoked from any part of the program, it all executes the code defined in the body of function.
Library functions
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int n ;
cout<<" Enter your number you want square";
cin>> n;
cout<<"Sqrt: " <<sqrt(n);
cout<<"Pow: " <<pow(2, n);
}
User-defined Functions
Name
No Return No Parameter
void line ()
{
for(int i=0;i<45;i++)
{
cout<<“*”;
}
cout<<endl;
}
Using Function
Function Declaration
Function Definition
It is a body of function which is below or before the main
Function call
Function Declaration
Function Call
Function Definition
Passing Values To functions
• Global Scope
Anything identified or declared outside of any function is visible
to all functions in that file
• Function level scope
Declaring variables inside a function can be used in the whole
function
• Block level scope
Variables or integers declared inside block are used inside
block
Global Variable
Local Variable
Default Arguments
https://pediaa.com/what-is-the-difference-between-staructured-and-object-oriented-
programming/#:~:text=The%20main%20difference%20between%20structured,of
%20objects%20and%20their%20interactions.