This tutorial has to do with function calls.
As programs get bigger, sometimes code needs to be repeated. Lets say you have
a math instruction to calculate degrees Fahrenheit. If your program was part of some
bigger scientific program, the bigger program might need to run your fahrenheit code multiple
times throughout its life. Instead of copying and pasting your code wherever it might be
needed, you can put your code in a function call.
The compiler takes the starting point of the function and places it at an address in the
exe. Then anytime your function is called, the code jumps to that address in the exe and
then executes it. A function can accept parameters and can return a single parameter.
You already use functions: printf, scanf, and getch are all function calls that someone else wrote.
Take a look at the following function, it adds two numbers together and returns the result:
//this first line is the declaration. It is telling the
//compiler that it accepts two integers and will return
//a single integer value.
int Add( int a, int b )
{
//this part is the function definition
//which defines what the function does
int sum;
sum = a + b;
return sum;
}
Ok, first notice that a function requires curly braces. This tells the compiler to put
all the code between the first and second curly brace somewhere in the exe so it can be called multiple times.
Next, notice I take the two integers passed to me, add them together and then tell the compiler
I want to return the sum. So lets say I want to use this function. First I put it in my .cpp
file above main. main itself is a function and we can't have two functions inside each other.
here is a sample program:
#include <stdio.h>
#include <conio.h>
int Add( int a, int b )
{
int sum;
sum = a + b;
return sum;
}
void main( void )
{
int firstNumber = 5;
int secondNumber = 7;
int answer;
//first we send 5 and 7 to the Add function and
//the Add function will return the value 12
//which will then go in the "answer" variable.
answer = Add( firstNumber, secondNumber );
//next we take 5 and 12 and send it to the Add function
//the Add function will return 17 back into the answer variable
answer = Add( firstNumber, answer );
}
A function can return any data type: float, char, int, etc. If you
don't want your function to return anything then use void. Here is a
function that would return nothing
void PrintANumber( int a )
{
printf( "your number is: %d", a );
}
It's tough at first to see the purpose of functions,
but they will become essential in your programming. They keep duplicate code from
having to be typed in many places and they help keep your program better organized and more clean.
In the following example, we try and verify the access code of a customer. We place
the entire verify block of code in a function and then call it from main. If this were a huge
mainframe program, anywhere in the program a customer's code had to be verified this function could be called.
__________________________________________________________
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <conio.h>
int VerifyAccessCode()
{
int numTries;
numTries = 0;
int accessCode;
accessCode = 300;
do
{
numTries++;
printf( " Enter access code :" );
scanf( "%d", &accessCode );
//continue to loop as long as the access code
//is not 300 and they have tried less than 5 times
}
while ( accessCode!= 300 && numTries < 5 );
if (accessCode == 300)
{
//we'll use 1 to represent success
return 1;
}
else
{
//we'll use 0 to represent failure
return 0;
}
}
void main( void )
{
int result = VerifyAccessCode();
//if we returned a 1 then the
//access code was verified in the function
if ( result == 1 )
{
printf( "Success!\n" );
}
else
{
//looks like result was not 1 (we returned a 0)
//so access is denied to the customer
printf( "Access Denied!\n" );
}
getch( );
return;
}
|