Wednesday 11 April 2018

Passing Parameters Types in C/C++


Parameter passing is also divided to the following types:
1.      Pass by value.
2.      Pass by reference.
            Pass by value
In pass by value method the value of actual parameter is passed to the formal parameter in the function header at the time of function call.

The previous example uses “pass by value” method.

Pass by reference
In pass by reference method the address of the actual parameter is passed to the formal parameter in the function header at the time.
In this method the values of actual parameters can be changed by the Formal parameters.

Example:
The above example in “pass by value” is now “pass by reference”:

#include<conio.h>
#include<iostream.h>

void square_of_value(int&); à function declaration

void main()
{
clrsrc();
int value = 0;
cout<< “ Enter a value: “ ;
cin>> value;                                                                  
square_of_value( value );  à function call
getch();                                                                               
}

void square_of_value(int &num) à function header
{
cout << “square of the given number is: ”<< num ;
}

Passing Parameters of a function in C/C++


Parameters passing to function

Parameter passing to a function is a process of passing values from function call to the function header.
Example:

#include<conio.h>
#include<iostream.h>

void square_of_value(int); à function declaration

void main()
{
clrsrc();
int value = 0;
cout<< “ Enter a value: “ ;
cin>> value;                                                                   
square_of_value( value );  à function call
getch();                                                                               
}

void square_of_value(int num) à function header
{
cout << “square of the given number is: ”<< num ;
}

Actual parameter
Actual parameter is a parameter that is used in function call. It is passed to the function.

Formal parameter
A parameter used in function header is called as Formal parameter.

Different Types of functions in C/C++ ?


            Function types


            There are two types of functions which are as follow:
1.      User-defined Functions
2.      Built-in Functions


User-defined function

User-defined function is a function that is defined by the user. A program may have many user-defined functions or may not have user-defined functions.
Example:
int odd(int);
 int even(int);

Built-in functions

Built-in function is a function which already exists in any programming language.
It is also called as library function.
Example:
cin>>, cout>>

How to call function in C/C++ ?

Function Call

Function call is a process of calling a function. It disturbs the normal flow of execution of a                program.

When a function is called the control moved to that function and after executing the instructions of the called function came back to main() function and execute the remaining instructions of the main().

Here is a complete example to understand the function declarationfunction definition and function call.

#include<conio.h>
            #include<iostream.h>

            void display();

            coid main(){
            clrscr();
            display();
getch();
}

void display()
{
cout<< ”Hello World” ;
}

Sunday 10 December 2017

What is control structure and how many types are ?

Control structure:

Control structure defines the flow of execution of a program.
There are following control structures:
1.      Sequential
2.      Iteration
3.      Conditional
4.      Functions

           Sequential

The sequential control structure is a type which defines that the flow of execution will in a sequence.

Here is an example to understand it
#include<iostream.h>
#include<conio.h>
void main()
{
  clrscr();
  cout << “Hello world” ;
  getch();
}
The above program is the example of sequential control structure. It means that firstly clrscr(); function will be executed and then cout<< “Hello world” ; statement will be executed and then getch(); function will be executed. It is the normal flow of the execution of a program.

           Iteration

Iteration means repetition. Iteration means repeating a block of statement instead of writing the same statement many times.

Here is an example to understand:
#include<iostream.h>
#include<conio.h>
void main()
{
            clrscr();
            int c=0;
            while(c<=5)
                        cout << “I love Pakistan” << endl ;
            getch();
}
            Output:

                        I love Pakistan
I love Pakistan
I love Pakistan
I love Pakistan
I love Pakistan

The above example is the example of iteration control structure. It will execute until value of c doesn’t reached to 5. Means it repeats cout << “I Love Pakistan” << endl ; five times.
The above output can be generated by the following program.
#include<iostream.h>
#include<conio.h>
void main()
{
  clrscr();
  cout << “I love Pakistan” << endl ;
  cout << “I love Pakistan” << endl ;
  cout << “I love Pakistan” << endl ;
  cout << “I love Pakistan” << endl ;
  cout << “I love Pakistan” << endl ;
  getch();
}
            There are the main types of loops:
1.      while loop
2.      do-while loop
3.      for loop
Later we will discuss about the types of loop in details.

            Conditional

Conditional control structure defines the flow of execution of a program in a condition.
Here is an example to understand the conditional control structure.
#include<iostream.h>
#include<conio.h>
void main()
{
  clrscr();
  int marks;
  cout<< “Enter marks: ” ;
  cin>> marks;
  if (marks > 40)
       cout<< “ Congratulation! You have passed ” ;
  getch();
}
In the above example if you input marks to 40 the output will be “Congratulation! You have passed” otherwise there will be nothing in the output of the program.

           Function:

A Function is a set of instructions which is used to perform a specific task. Every C++ program has at least one function which is main(). You can divide your program info different function depending on you.
Example:
A simple example of the function is the “clrscr();” function which is used to clear the output window.