Function Prototypes in C++

Introduction:
  • The functions are classified into the following types depending on whether the arguments (parameters) are present or not and whether the value is returned or not
  • These are also called function prototypes.
  • The following are the function prototypes.
                  i).   Function with no arguments and no return value.
                  ii).  Function with arguments and no return value.
                  iii). Function with arguments and with the return value.
                  iv). Function with no arguments and with the return value.
1. Function with no arguments and no return value:
  • The called program does not receive any data from the calling program and does not send back value to the calling program.
  • This function is used to perform the only operation they act independently (They read data values and print results in the same block.
Syntax:
Example: Addition of two numbers

#include<iostream.h>

#include<conio.h>

int add();//fun declaration

int a,b;

void main()

{

clrscr();

cout<<"Enter two values: ";

cin>>a>>b;

add();//func calling

getch();

}

int add() //fun definition

{

int c;

c=a+b;

cout<<"Sum is "<<c;

}

Output:
2. Function with arguments and no return value:
  • The called program receives some data from the calling program and does not send back any values to the calling program. (One-way communication).

Syntax:
Example: Addition of two numbers

#include<iostream.h>

#include<conio.h>

int add(int,int);//fun declaration

void main()

{

int a,b,c;

clrscr();

cout<<"Enter two values: ";

cin>>a>>b;

add(a,b);//func calling

getch();

}

int add(int x,int y) //fun definition

{

int z;

z=x+y;

cout<<"\nSum is "<<z;

}

Output:
3. Function with arguments and with return statement:
  • The called program receives some data from the calling program and sends back any values to the calling program. (Two-way communication)

Syntax:
Example: Addition of two numbers

#include<iostream.h>

#include<conio.h>

int add(int,int);//fun declaration

void main()

{

int a,b,c;

clrscr();

cout<<"Enter two values: ";

cin>>a>>b;

c=add(a,b);//func calling

cout<<"\nResult is ....."<<c;

getch();

}

int add(int x,int y) //fun definition

{

int z;

z=x+y;

return z;

}

Output:
4. Function with no arguments and with return value:
  • The calling program cannot pass any arguments to the called program but, the called program may send some return value to the calling program.
Syntax:
Example: Addition of two numbers

#include<iostream.h>

#include<conio.h>

int add();//fun declaration

int a,b;

void main()

{

clrscr();

cout<<"Enter two values: ";

cin>>a>>b;

cout<<"\n Result is...."<<add();//func calling

getch();

}

int add() //fun definition

{

int c;

c=a+b;

return c;

}

Output:

Share:

Views in SQL

Views:

   Ø View is a subset from one or more tables.

   Ø View is a virtual table, views contains rows and Columns.

   Ø We can present logical subsets or combination of data by creating views of tables.

   Ø A view is a logical table based on a table or another view.

 Advantages of views:

                   ·       Restricted Access to specific columns of a table there by                                                           providing additional security 

·       Oracle stores the definition of the view only

·       It simplifies queries

·       It avoids data redundancy

·       To hide the data complexity

Syntax: 

  1.  Create view View_Name as

      Select clm1, clm2 ..clmn

      From Table_Name

      Where <\Condition>

Some types of views are,

1.    Simple view

2.    Join view

3.    Read only view

4.    Updatable view 

5.    Reasonably view

Simple view:

·       The definition of the view will contain the column names given in the query.

Syntax: 

  1.   Create view <viewname> as query

Example:

  1. Create view empview

    As

    Select ename, deptno, sal

    From emp;

Join View:

·       Joining of tables results in creation of join views. A join view is a view that is created using a join condition. There are certain in join views.

Example:

  1. Create view join_view

    As 

    Select empno,ename.dept.deptno

    From emp e, dept d

    Where e.deptno = d.deptno;

Restrictions:

   Ø A view creation cannot contain on order by clause

   Ø If a view is defined with check option, a row cannot be inserted into or updated in the base table.

 Renaming the columns of view:

·       We can able to rename the column of the view but the column name should be meaningful.

Syntax:

  1.           Create view view_name (column1,column2,…)

              As

              Select column1,column2

              From base_table

              Where condition;

Example:

  1.           Create view employee_view (e_no, e_name)

    As

              Select empno,ename    

              From emp;

Describe the view:

  1. Desc employee_view;

 Read only views:

·       Certain views can be created only for selection purpose. Such views will not permit the user or the owner of the view to perform any DML operations.

Example: 

  1. Create view read_view

    As

    Select * from dept with read only;

Updatable views :

·       Views that allow data manipulation (insert, update, delete) are called updatable views.

Criteria for updatable views:

1.    The view must be created over a single base table.

2.    The view must include the primary key.

3.    The view should not contain any aggregate functions.

4.    The view should not contain any sub query in its select statement.

5.    The view should not contain DISTINCT, HAVING and GROUP BY clause in the SELECT statement.

6.    If the view is constructed over another view than the base view must be updated.

7.    The view should not contain any constants, strings and expressions in the SELECT statement.

8.    The view must contain all the NOT NULL columns of the base table.

Reasonably views:

·       Views that do not allow data manipulation are called reasonably views.

 Destroying views:

·       Views can be destroyed using the DROP view command.

Syntax:           

  1. DROP view <view name>;

Example:          

  1. DROP view empview;


 

 

 

 

Share:

C++ Function or Methods

Function:
  • A function is a set of instructions that are used to perform specified tasks that repeatedly occur in the main program.
  • It helps to avoid duplication of work and also breaks down a large program into a number of small functions.
  • It is used to divide the complex task into a manageable task.
  • Functions are classified into two types as shown below,

              User-defined functions
              Pre-defined functions
1.User-defined function:
  • The users have full scope to implement their own ideas in the function.
  • It defined by the users according to their requirements are called user-defined functions.
  • Elements of user-defined function:

               * Function declaration
               * Function definition
               * Function call

Syntax of user-defined function:

<Header_files>
Main_function()
{
variable_declaration;
//function declaration part
return_type or data_type function_name(parameters list);
// function calling part
function_name();//no parameter passed
or
function_name(parameter);//parameter passed
or
return value=function_name(parameter);
//printing statements
}
//function definition part
Data_type function_name(parameters list)
{
Local variable declaration;
......
Body of the function;
return (expression);
}

Parameters:
  • It provides data communication between the calling function and the called function.
  • There are two types of parameters,
    (i). Actual parameters:
  • Used in data transferred from the main function to the user-defined function.
    (ii). Formal parameters:
  • Used in data transferred from a user-defined function into the main function.
For example:


Where
          a, b are the actual parameters.
          x,y is the formal parameters.
Local and global variables:
Local variable:
  • The local variables are defined within the body of the function.
  • This variable used within the function only, other functions can not access these variables.
Example:

fun1(int a, int b)
{
          int c,d;
}

Global variable:
  • The global variables are defined outside the main () function, multiple functions can use these variables.
Example:

int x,y=10;
main()
{
          int a,b;
}

Example program for local and global variables.

#include<iostream.h>
#include<conio.h>
int i; //Global variable
void main()
{
          int j; //Local variable
void fun1();//Function declaration
i=0;
clrscr();
cout<<"\n Value of i in main function: "<<i;
fun1();//Function calling
cout<<"\n Value of i in after call: "<<i;
getch();
}
//Function definition
void fun1()
{
int k; //Local variable for fun1()
i=50;
}

Output:
The return statement:
  • A return statement is an optional part of the function.
  • It is used to populate the results without any printing function.
Syntax:

return; or return(expression);

Example:

if(x>0)
          return(1);
else
          return(0);

Function calling:
  • A function can be called by specifying the function name in the source program with parameters if presence within parentheses.
  • If a function is not declaring the variable to return, we must be specified the data type at the return value.

For example:

  • It is divided into two types,

1.Function call without a parameter:

#include<iostream.h>
#include<conio.h>
void main()
{
msg_func();//Function definition without parameter and
                          called the function
cout<<"\n Message from main program or function");
getch();
}
//Function definition
msg_func()
{
clrscr();
          cout<<"\n Message from msg_func() user defined function";
}

Output:
2.Function call with parameter:

#include<iostream.h>
#include<conio.h>
void main()
{
int a;
clrscr();
a=msg_func(10,20);//Function definition with parameter and 
                                   called the function
cout<<"\n Message from main program or function";
cout<<"\n The value of a is : "<<a;
getch();
}
//Function definition
int msg_func(int x,int y)
{
int z;
z=x+y;
cout<<"\n Message from msg_func() user defined function";
return(z);
}

Output:


Share:

Popular Posts

Search This Blog

Powered by Blogger.

Blog Archive

Service Support

Need our help to Learn or Post New Concepts Contact me

Blog Archive

Recent Posts

Service Support

Need our help to Learn or Post New Concepts Contact me