Friday 10, February 2012
Welcome Guest, Register | Login  
      Home    |    Tutorials    |    Free Ebooks    |    Free Scripts    |    Articles    |    Blog     |    Forum    |    About Us    |    Contact Us

C++ Functions

A function is a sub-program that your program can call to perform a task. When you have a piece of code that is repeated often you should put it into a function and call that function instead of repeating the code.
Creating your own function
You declare a function in a similar way as you create the main function. You first put void then the function name and some brackets. The function code goes between curly brackets after that. Here is a function that prints Hello.

void PrintHello()
{
   cout << "Hello\n";
}
Calling your function
Once you have created the function you must call it from the main program. Here is an example of how to call the PrintHello function from above.

void PrintHello()
{
   cout << "Hello\n";
}
 
void main()
{
   PrintHello();
}

Local and global variables
If you declare a variable inside a function then it is only accessible by that function and is called a local variable. If you declare a variable outside of all functions then it is accessible by any function and is called a global variable.
int g; // Global variable
 
void MyFunction()
{
   int l; // Local variable
   l = 5;
   g = 7;
}
 
void main()
{
   g = 3;
}

Parameters
You must use parameters to pass values to a function. Parameters go between the brackets that come after a function. You must choose the datatype of all parameters. We will now create a function that receives a number as a parameter and then prints it.
void PrintNumber(int n)
{
   cout << n;
}
 
void main()
{
   PrintNumber(5);
}
If you want to pass more than one value then you must separate parameters with a comma.
void PrintNumber(int n, int m)
{
   cout << n << m;
}
 
void main()
{
   PrintNumber(5, 6);
}

You can either pass a parameter by reference or by value. The default is by value which means that a copy of the variable is made for that function. If you use a * in front of the parameter then you will be passing only a pointer to that variable instead of making another copy of it.

void PrintNumber(int *n)
{
   cout << *n;
}
 
void main()
{
   int i = 5;
   PrintNumber(&i);
}

Return values
A function can return a value that you can store in a variable. We have been using void in the place of the return variable until now. Here is an example of how to return the number 5 from a function and store it in a variable.

int GetNumber()
{
   return 5;
}
 
void main()
{
   int i = GetNumber();
}

 
     
   
C++ Tutorial
 
  Introduction to C++
Introduction to C++
  Variables in C++
Variables in C++
  if statement in C++
if statement in C++
  Looping in C++
Looping in C++
  break & continue statements
break & continue statements
  Switch Case Statement
Switch Case Statement
  C++ Functions
C++ Functions
  Pointers in C++
Pointers in C++
  Arrays in C++
Arrays in C++
  Strings in C++
Strings in C++
  Structures in C++
Structures in C++
  C++ File I/O
C++ File I/O
  Typecasting in C++
Typecasting in C++
  C++ Classes
C++ Classes
  Recursion in C++
Recursion in C++
  Inheritance in C++
Inheritance in C++
  Initilization Lists in C++
Initilization Lists in C++
  Enumerated Types in C++
Enumerated Types in C++
  Templates in C++
Templates in C++
  C++ Preprocessors
C++ Preprocessors
 
 
 
Web Designing Tutorials
  HTML Tutorial
HTML Tutorial
  DHTML Tutorial
DHTML  Tutorial
  CSS Tutorial
CSS Tutorial
  XHTML Tutorial
XHTML Tutorial
 
Programming Languages Tutorials
  C Language Tutorial
C Language Tutorial
  C++ Tutorial
C++ Tutorial
  Java Language Tutorial
Java Language Tutorial
  Data Structure Theory Tutorial
Data Structure Theory Tutorial
 
Server Side Scripting Tutorials
  PHP Tutorial
PHP Tutorial
  SQL Tutorial
SQL Tutorial
  ASP Tutorial
ASP Tutorial
 
Client Side Scripting Tutorials
  JavaScript Tutorial
JavaScript Tutorial
  VBScript Tutorial
VBScript Tutorial
 
 
 
POPULAR E-BOOKS
 
Download The 7 Great Lies of Network Marketing  Ebook The 7 Great Lies of Network Marketing
   
Download Wealthy Wal s Guide to Making Money Online Ebook Wealthy Wal s Guide to Making Money Online
   
Download C++ Essential Ebook C++ Essential
   
Download The Affiliate Marketers Master Guide Ebook The Affiliate Marketers Master Guide
   
Download Pay Per Click Marketing Guide Ebook Pay Per Click Marketing Guide
   
 
Studiesinn.com © 2012 All Rights Reserved.
Website Designed & Developed by TechXprtz