#include <stdio.h>
#include <iostream>
using namespace std;
// declare function 1 no argument
void myFunc_1();
// declare function 2 with an argument
void myFunc_2(int y);
// declare function 3 with an argument, returns an integer
int myFunc_3(int z);
int mySum1; // declare variables integer (global for this cpp file)
int mySum2; // when a variable is declared on the top, outside the functions
int x; // it means that all functions in this cpp-file have access to it.
int main()
{
// Part I///////////////////////////////
x = 5;
myFunc_1(); // call function 1
cout << mySum1 << endl; // display mySum1
// Part II//////////////////////////////
myFunc_2(5); // call function 2, pass in the value 5 as an argument.
cout << mySum2 << endl; // display mySum2
// Part III/////////////////////////////
cout << myFunc_3(5) << endl; // display the returned value of function 3
getchar(); // wait for a key to be pressed
return 0;
}
void myFunc_1() // define function 1
{
mySum1 = x + 5;
}
void myFunc_2(int y) // define function 2 with an argument
{
mySum2 = y + 3;
}
int myFunc_3(int z) // define function 3 with an argument, must return an integer
{
return z + 2;
}
/*
Learn to declare and define different functions.
and see how different functions handles the same type of task,
I have used different numbers so that you easier can tell them appart.
Let us take a closer look at a function declaration:
----------------------------------------------------------------
void myFunc_1();
----------------------------------------------------------------
void - means that the function is empty, it returnes no value
myFunc_1 - the name of the function
() - this means that there are no arguments
; - end function declararion
----------------------------------------------------------------
Decription:
We declare our function to give the other functions access to it.
----------------------------------------------------------------
Let us take a closer look at a function definition:
----------------------------------------------------------------
void myFunc_1()
{
mySum1 = x + 5;
}
----------------------------------------------------------------
void - means that the function is empty, it returnes no value
myFunc_1 - the name of the function
{ - start function definition
() - this means that there are no arguments
mySum1 = x + 5; - this is what our function does..
} - end function definition
----------------------------------------------------------------
Decription:
We must define what our function will do.
This function takes the number 5,
and adds it with the value from the argument called x
and stores the value in the variable mySum.
----------------------------------------------------------------
Let us take a closer look at a function call:
----------------------------------------------------------------
myFunc_1();
----------------------------------------------------------------
myFunc_1 - the name of the function
() - there is no argument passed in
; - end the function call
----------------------------------------------------------------
Decription:
You must call your function in order to "put it to work".
----------------------------------------------------------------
*/
Download Visual C++ Source Code
I hope you found this c++ tutorial useful!
Don't forget to mention Apron Tutorials in your References!