[Tutorial] Functions
AirTrake :: Development :: C/C++
Page 1 of 1 • Share •
[Tutorial] Functions
Hello AirTrake community,
today I want to tell you 'How to make own functions'!
Well, first of all let me say what functions actually are and where they are used.
On a normal project you can't write a nice code without functions. They are used to make the code shorter or to make things easier.
Functions can even return values, so you can use them on many ways like: if( IsValiedName(name) ), this will return 1 in case of it's useable and otherwise 0.
<--- PART 1 --->
Ok, now let me start with a simple example.
This function won't return anything so let's make the return value 'void'.
Our function should write 'Hello from function' on the console, so no 'arguments' because we won't give the function anything.
Let's declare it!
This should be put on the top of your code, because we declare it but didn't define it yet. (To avoid errors and tell the programm that we'll define a function)
Let's define it now!
How to use this?
Again, what we've done here:
- told the programm that we'll define a function.
- declared the function
- used our function in 'main' function
<--- PART 2 --->
In this part I want to explain you how to use arguments (The values between ( and ) ).
Let's make a function that will multiplicate two numbers that we'll give.
Declaration:
On this function, we've to give our function "Multi" two parameters which need to be 'double'.
(double example, 'double example = 5.4;'
And I want that this programm put the result on the screen.
Let's define it:
Now we see that num1 and num2 are normal values of the type 'double' and can be used in the function.
Notice: 'result = (num1 * num2)' is not necessary on this example, you also can print it like this: std::cout << num1*num2 << std::endl;
How to use?
Also:
<--- PART 3 --->
Returning a value from a function!
Finally we can use our functions in 'if' our other craps.
I want to use my 'Multi( double num1, double num2);' example again.
But now we don't want the function to print on the screen, we want to save it in a variable or print it directly from main.
Instead of 'void' it's now 'double' because we want to print a number that is 'double'
Let's declare it:
You can see here that we used 'double Multi' instead of 'void'.
Let's define it:
Let's use it:
<--- PART 4 --->
Initializing by default values if no argument has been given.
At declaring functions we can set default values
Example:
And of course we can use pointer or references as arguments.
Examples:
One more notice:
If you want to return 'char' you won't get what you need, it's a bit more complicated because the variables in the functions get destroyed.
Ok guys, finally we're done!
Thanks for reading and don't forget to comment and report bugs.
If you've any questions set them here.
today I want to tell you 'How to make own functions'!
Well, first of all let me say what functions actually are and where they are used.
On a normal project you can't write a nice code without functions. They are used to make the code shorter or to make things easier.
Functions can even return values, so you can use them on many ways like: if( IsValiedName(name) ), this will return 1 in case of it's useable and otherwise 0.
<--- PART 1 --->
Ok, now let me start with a simple example.
This function won't return anything so let's make the return value 'void'.
Our function should write 'Hello from function' on the console, so no 'arguments' because we won't give the function anything.
Let's declare it!
void SayHello( void );
This should be put on the top of your code, because we declare it but didn't define it yet. (To avoid errors and tell the programm that we'll define a function)
Let's define it now!
void SayHello ( void ) //again
{
std::cout << "Hello from function!\n";
}
How to use this?
void SayHello ( void ); //Telling our programm that we'll create a function
int main ( void ) //don't need the parameters of main (also a function), just a test programm
{
SayHello (); //No parameters, so there is nothing between ( and )
}
void SayHello ( void ) //Declaring it.
{
std::cout << "Hello from function!\n";
}
Again, what we've done here:
- told the programm that we'll define a function.
- declared the function
- used our function in 'main' function
<--- PART 2 --->
In this part I want to explain you how to use arguments (The values between ( and ) ).
Let's make a function that will multiplicate two numbers that we'll give.
Declaration:
void Multi ( double num1, double num2);
On this function, we've to give our function "Multi" two parameters which need to be 'double'.
(double example, 'double example = 5.4;'
And I want that this programm put the result on the screen.
Let's define it:
void Multi ( double num1, double num2 ) {
double result = 0;
result = ( num1 * num2 ) ; //Getting what we want.
std::cout << result << std::endl; //printing our result.
}
Now we see that num1 and num2 are normal values of the type 'double' and can be used in the function.
Notice: 'result = (num1 * num2)' is not necessary on this example, you also can print it like this: std::cout << num1*num2 << std::endl;
How to use?
int main ( void )
{
Multi( 5.5 , 11.11 );
}
Also:
int main ( void )
{
double FirstNumber = 5.5;
double SecondNumber = 11.11;
Multi( FirstNumber , SecondNumber );
}
<--- PART 3 --->
Returning a value from a function!
Finally we can use our functions in 'if' our other craps.
I want to use my 'Multi( double num1, double num2);' example again.
But now we don't want the function to print on the screen, we want to save it in a variable or print it directly from main.
Instead of 'void' it's now 'double' because we want to print a number that is 'double'
Let's declare it:
double Multi ( double Num1, double Num2 );
You can see here that we used 'double Multi' instead of 'void'.
Let's define it:
double Multi ( double Num1, double Num2 ){
return (Num1*Num2); //Here we do it, just get what we need, don't printing anything anywhere!
}
Let's use it:
int main ( void ){
double result = Multi ( 1.1 , 2.2 ); //We can initialize 'result' directly by calling our function!
std::cout << Multi (1.1 , 2.2) << std::endl; //Or using it in other ways
//At least what I've promised:
if ( Multi(1.1,2.2) == 5.0 ) //Can check if the result is 5.0
//Other example:
while ( Multi ( x, y ) < 5 )
}
<--- PART 4 --->
Initializing by default values if no argument has been given.
At declaring functions we can set default values
Example:
double Multi ( double x = 1.0, double y = 1.0);
And of course we can use pointer or references as arguments.
Examples:
double Multi ( double *x, double &y );
One more notice:
If you want to return 'char' you won't get what you need, it's a bit more complicated because the variables in the functions get destroyed.
Ok guys, finally we're done!
Thanks for reading and don't forget to comment and report bugs.
If you've any questions set them here.
Re: [Tutorial] Functions
Very basic, but nice tutorial.
Just some questions:
[?] How it comes that the main method is written like this "int main(void)" ? I have read somewhere that it does return a number. It just got no return statement. Is that meant by this? Or can you also write "void main()" ?
[?] Can you also return an element of an array?
Like, let's say I create an array which holds integers only. Can I simply write something like:
?
Just some questions:
[?] How it comes that the main method is written like this "int main(void)" ? I have read somewhere that it does return a number. It just got no return statement. Is that meant by this? Or can you also write "void main()" ?
[?] Can you also return an element of an array?
Like, let's say I create an array which holds integers only. Can I simply write something like:
- Code:
int function(element)
{
return array[element];
}
?
Giovanni- Newbie
- Posts: 9
Join date: 2012-01-02
Location: Doenerland
Re: [Tutorial] Functions
Why you don't try it out?
And main have to return an integer!
isn't that actually a string?
And you return strings/arrays also like this:
return array;
But remember: chars aren't easy to use.
And main have to return an integer!
- Code:
int function(element)
{
return array[element];
}
isn't that actually a string?
And you return strings/arrays also like this:
return array;
But remember: chars aren't easy to use.
Similar topics» Easy Paint Tool SAI - Tutorial
» [Tutorial]Wie man mit GIMP Felle erstellen kann
» Sections - Ein Schritt-Für-Schritt Tutorial
» WettRennen Tutorial
» Haus Bau Tutorial
» [Tutorial]Wie man mit GIMP Felle erstellen kann
» Sections - Ein Schritt-Für-Schritt Tutorial
» WettRennen Tutorial
» Haus Bau Tutorial
AirTrake :: Development :: C/C++
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum