introduction to C++ FUNCTIONS

Qaybta Xirfada Sayniska iyo iwm

Moderators: Moderators, Junior Moderators

Addoow
SomaliNet Super
SomaliNet Super
Posts: 7611
Joined: Sat Aug 16, 2008 4:13 am
Location: Hawiye Pride!

introduction to C++ FUNCTIONS

Post by Addoow »

simple introduction to function prototype,function argument and parameters,function definitions and function call.


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

void hadal(void) ;//function prototype
int main()
{
using namespace std;

cout<<"I am calling a function named hadal."<<endl;
hadal ();//function call
system("pause");
return 0;
}

void hadal (void) //function definition.

{
using namespace std;

cout<<"I am here dude! what is that you want."<<endl;

system("pause");

}


Executed in the IDE of Devc++


Image

Uploaded with ImageShack.us
Addoow
SomaliNet Super
SomaliNet Super
Posts: 7611
Joined: Sat Aug 16, 2008 4:13 am
Location: Hawiye Pride!

Re: introduction to C++ FUNCTIONS

Post by Addoow »

A C++ Program which prints out the conversions of Celsius to Fahrenheit degree in a range of 5 to 50 values.


#include<iostream.h>
#include<iomanip.h>
int main()
{
using namespace std;
const int MAX=50;
const int MIN=5;
const int STEP=5;

cout<<"C.DEGREES F.DEGREES\n"
<<"--------- ---------\n";
int ctemp;
float ftemp;

cout<<setiosflags(ios::showpoint)
<<setiosflags(ios::fixed)
<<setprecision(2);

ctemp=MIN;
while(ctemp<=50)
{
ftemp=(9.0)/(5.0)*ctemp+32;
cout<<setw(2)<<ctemp<<" ";
cout<<setw(13)<<ftemp<<endl;
ctemp=ctemp+MIN;


}




system("pause");
return 0;
}

THE PROGRAM BEING TESTED IN DEVC++ ENVIRONMENT.

Image

Uploaded with ImageShack.us
Addoow
SomaliNet Super
SomaliNet Super
Posts: 7611
Joined: Sat Aug 16, 2008 4:13 am
Location: Hawiye Pride!

Re: introduction to C++ FUNCTIONS

Post by Addoow »

ANOTHER similiar one which prints out number plus its square and cube in the range of 1 to 20 using the while loop.


#include<iostream.h>
#include<iomanip.h>
int main()
{
using namespace std;
int num;
cout<<"NUMBER SQUARE CUBE\n "
<<"------ ------ ----\n"
<<" "<<endl;

num=1;
while(num<=10)
{

cout<<setw(2)<<num<<" "
<<setw(2)<<num*num<<" "
<<setw(3)<<num*num*num<<endl;

num=num+1;

}




system("pause");
return 0;
}




After Its execation it looks like this.
Image

Uploaded with ImageShack.us
User avatar
afisoone
SomaliNet Super
SomaliNet Super
Posts: 5509
Joined: Wed Jul 22, 2009 9:46 pm
Location: We all want to become president even though Amisom controls Mogadishu

Re: introduction to C++ FUNCTIONS

Post by afisoone »

waaga computer programing kucusbaa ayaan aad u jeclaa c++...

Hada sida quranka ayaan u xafidey. I do not use a lot nowadays ...
User avatar
IRONm@N
SomaliNet Super
SomaliNet Super
Posts: 5122
Joined: Tue May 01, 2001 7:00 pm
Location: Jannatul-Fardowsa
Contact:

Re: introduction to C++ FUNCTIONS

Post by IRONm@N »

This is a program I wrote in my c++ class

A PROGRAM TO PRINT THREE OVERLOADED FUNCTIONS
OF AN ARRAY
*/

#include <iostream>

using namespace std;



int main()
{
void print(const int data [], int size = 10); // int version
void print(const float data [], int size = 10); // float version
void print(const char data [], int size = 10); // char version

int x[10] = {1,2,3,4,5,6,7,8,9,10};
float y[5] = {1.0,2.1,3.2,0.1, .05};
char z[6] = {'A', 'B', 'C', 'D', 'E', 'F'};

print (x);
print (y,5);
print (z,6);

cout << endl;
system("pause");

return 0;

}

// printing array in integers
void print(const int data [], int size = 10)
{
int i;

for( i = 0; i < size; i++)
cout << data << " ";
cout << endl;
}

// printing array in floats
void print(const float data [], int size = 10)
{
int i;

for( i = 0; i < size; i++)
cout << data << " ";
cout << endl;
}

// printing array in characters
void print(const char data [], int size = 10)
{
int i;

for( i = 0; i < size; i++)
cout << data << " ";

}

/* OUTPUT

1 2 3 4 5 6 7 8 9 10
1 2.1 3.2 0.1 0.05
A B C D E F
Press any key to continue . . .

*/
haxxor
SomaliNetizen
SomaliNetizen
Posts: 973
Joined: Thu Oct 28, 2010 2:48 pm

Re: introduction to C++ FUNCTIONS

Post by haxxor »

Or just do it in PHP, it's much more easier...

Code: Select all

<?PHP

function math($a, $b) {
$c = a + b;
};

math(1, 2);
The red lined text gives you answer of 3.
Locked
  • Similar Topics
    Replies
    Views
    Last post

Return to “Careers - Engineering, Science & Computers”