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” ;
}