C++ Functions:Defining, Declaring, Calling, Arguments

C++ Functions:Defining, Declaring, Calling, Arguments
A function can be referred to be a section which is defined by a name within which multiple task takes place. The best example of the function is the main() function which we declare before we start writing any C++ program. To make a code more simplified and well defined and quick executable it is divided into sub parts of several functions to perform a specific task.
    Declaring a function consists of several parts better classified as the return type, name, parameters. Return type specifies the value which is to be returned by the function after executing all its codes its types are: void, int, float,double, long, char, string, class;etc. Name as stated is the name of the function defined. Parameters to be explained can be stated as the values passed through the function so that they can be used in execution without any further definition. Then finally comes the body of the function. Body as the word itself defines is the set of code executed within the scope of function.

  For example if we define any user defined function as max:
     
// function returning the max between two numbers
 
int max(int num1, int num2)  {
   // local variable declaration
   int result;
 
   if (num1 > num2)
      result = num1;
   else
      result = num2;
 
   return result; 
}
 
Function declaration just doesn't mean defining it in the main() function. Defining a function means defining its prototype its parametric form and also calling it in the main function. But a very important point is to be noted, the parameters of a function are not that important. A function can be without parameter it is not an error but a procedure. It is advised to do both function prototype as well as definition because a function is normally declared to perform at different platforms and to be in the safer side it should be declared at the top only.
   Calling a function means invoking a set of arguments or giving the whole access of program in different hand. The parameters declared in the main function are passed through the function storing its returned value(if available) in a third variable and displaying the result. The parameters are used to invoke the function at particular location.

#include 
using namespace std;
 
// function declaration
int max(int num1, int num2);
 
int main () {
   // local variable declaration:
   int a = 100;
   int b = 200;
   int ret;
 
   // calling a function to get max value.
   ret = max(a, b);
 
   cout << "Max value is : " << ret << endl;
 
   return 0;
}
 
// function returning the max between two numbers
int max(int num1, int num2)  {
   // local variable declaration
   int result;
 
   if (num1 > num2)
      result = num1;
   else
      result = num2;
 
   return result; 
}
A function can be called in three different ways call by value, pointer, reference:
Call By Value: This method copies the original value provided to the function. In case any changes to be made to the parameters the original values are also effected.
 Call By Pointer: This deals with the address of the parameters provided. Any change applied to the values are affected to the parameters only not to the originals variables.
Call By Reference: This procedure copies the value of parameters into a temporary variable and any change made while execution is affected to the temp variable and the original is remained unchanged.
       By default C++ uses call by value to declare any function. To change our preference we need to declare the parameter in different forms as per requirements.Else while execution the original variables will come up with changed value and the original value will be lost.

File:Basic informations and funtions,Input/output procedures through C++

File:Basic informations and funtions,Input/output procedures through C++


So far in C++ we have come across iostream headers for input output purposes defining cin, cout functions. But as mentioned previously those input values were temporary and couldn’t be used for future access. But it is never so that we cannot store the inputted values for future access in fact the can be stored as a whole sort of document. The procedure is popularly known as files one can define a file, can be a text document also. As we declared a separate header file for input output purposes in the previous section in the same way we need to declare a header file to use a file. The header file is better known as fstream. Basically fstream function is a predefined class type and hence its object needed to be defined in the main function as follows:
Void main(){
Fstream f1;
F1.open(ios::out);
………
}
The above mentioned file is to open a predefined file. Where open() is a function used to open a file. It’s a keyword one can mention to. Similarly, to read a file we need to use the keyword read(). Files lead to a lot of memory leaks and so we need to close this class. Again for it we need to use a different keyword that is close().Files are mainly used to store information inputted by user in a document form. As the data is inputted through the keyboard it reaches the memory from the memory it moves to the file it is to be stored in. When the value is called to be displayed the vice versa process takes place that is the value first moves to the memory then the memory directs it to the monitor or the display the data to user. This includes a lot of background process one of the most vital one is conversion high level language to machine level language when user gives the input and the vice versa that is machine level to high level when user asks for display. A very good example of abstraction of data. A file can be opened in many forms, some of them are as follows:
      1.       ios::in->to open a file in input mode
      2.       ios::out->to open a file in output mode
      3.       ios::binary->to open a file in binary mode
      4.       ios::ate->to set the pointer of the file at the end
      5.       ios::app->it will write the output in the file from the end of the data without deleting the previous ones
      6.       ios::trunc->it will delete all previous data present in file and rewrite in it

Singly LinkList in java

Singly LinkList in java
As mentioned in the previous post LinkList is a very vital data structure which are declared dynamically. Its of two types Singly and Doubly. Today we are going to discuss on singly link list. Singly Link list consists of two parts the value part and the reference part. The first part or the value part stores the value of node input by user. The second part or the reference part stores the referring value of the next node. This form of link list can be very useful to implement data structures such as stack, queues;etc.
     Here the first node is commonly known as the start node. It points to the first node storing values. The last node is commonly known as last. As it is the last node in the whole list so it doesn't have any node to point towards and hence it contains a null value in the reference part.
     In case of link list if asked in examination we need to just write the function because the programs are too long and hence just to check the caliber one is asked to write the function only.
     It has three basic operations:
  1. Adding
  2. Displaying
  3. Removing/Deleting 

Adding a new node:

   In case of add we first of all will take the input of the first node both its value as well as the reference value. And one important thing to note every next(reference) value must be initially declared with null then comes a do while loop which encounters with a 'k' value means it will take entry until the user wants to have the node to be generated. In the loop we will first take the entry of the info part then the reference will be declared with a null and will copy its initial reference value to the node added previously. After all entry we will declare a call function of it in the main part.
public static void create(link node){
        int k;
     Scanner sc=new Scanner(System.in);
     System.out.println("Enter the value of node:");
     node.info=sc.nextInt();
     node.next=null;
   do{
     link temp=new link();
     System.out.println("Enter the temp node:");
     temp.info=sc.nextInt();
     temp.next=null;
     node.next=temp;
     node=temp;
     System.out.println("Enter 0 to continue and 1 to exit:");
     k=sc.nextInt();
       }while(k==0);

Linked List

Linked List
Linked List in java is a set of two information. The first part contains the value and the second part consists of the reference of the next node. The first node is better known as the start node. The last node of it consists of null because the last node need not to refer to any node. It inherits the Abstract class and implements a list form. We can better denote it with a train, where start is the engine and the last node null value is the cross mark of the trains last compartment. LinkList allocates dynamically which results to consumption of space as well as time. Avoids haphazard storage of values.

Few points to be noted:
  • It can contain duplicate elements
  • It maintains order of insertion
  • It is a non-synchronized form
  • Scene there is no shifting so compiling time is less
  • It can either be used as a link-list, stack or queue
LinkList are of two types :
  1. Singly LinkList
  2. Doubly LinkList
Singly LinkList is a set of two parts one is the information part which stores the value inserted by user for the node and the other part is the reference part which refers to the next node. It is an one sided list. The first link is popularly known as start followed by the first valued node.

Doubly LinkList is the set consisting of three parts the first part contains the reference of the previous node, the second part consists of the info part inserted by the user and the third and the last part consists of the reference value referring to the next node available.
Method to declare a LinkList is: 
public class LinkedList extends AbstractSequentialList implements ListDequeCloneable, Serializable 

C++ program to learn the basics of using a file

C++ program to learn the basics of using a file
This is a program that would help you to learn about the basics of a file through c++. This would teach ue that how can we acess a file, of how can we open it, how can we cloase it, how can we acess the documents present in a file. Its easy to use just install a c++ compiler copy this program to a new notepad and save it as a .cpp file and copy it in the bin of c++ compiler. Its all done now just open compiler search for the file open it and run it.

#include<stdlib.h>
#include<conio.h>
#include<fstream.h>
#include<ctype.h>
void main()
{   clrscr();
    fstream f1("mydata.dat",ios::in);
    char ch;
    int cnt=0;
    char c;
    if(!f1)
    {  cout<<"\n cannot open";
       getch();
       exit(-1);
    }
    cout<<"\n the text document is :";
    while (!f1.eof())
    {  f1.get(c);
       cout<<c;
    }
    f1.close();
    f1.open("mydata.dat",ios::in);
    while (!f1.eof())
    {  f1.get(ch);
       if(isalpha(ch))
       cnt++;
    }
    cout<<"\n number of alphabets are:"<<cnt;
    f1.close();
    getch();
}