Digital Reference

Introduction to programming --- C++

FUNCTIONS
Functions:

What is a function? In C++, a function is a set of commands written to perform a specific task in various situations. Do you recall the function getline()? As you’ve seen getline() can be used again and again in different situations. A good function is meant to perform one single task again and again, so that you don’t have to write the same thing all over again when you want another task of the same kind performed. For example, an addition function can add up any 2 numbers with just a call to the function:

int x = 3, y = 4;

int sum = addition(x,y);

Now, the variable sum equals 7. If you want to perform another addition, you can just use the addition function again, something like:

int sum=addition(5,5);

would give you a sum of 10;

This is basically what functions are. Now how do we write functions? Actually you knew how already. Remember the main() function? Yes that is a function, just like any other functions. So let’s see how to write an addition() function:

int addition(int x, int y)
{
    return (x + y);
}

This of course is a useless function, because why not just use the operator + instead? yea I know, this is a demonstration only! Now, let’s break this down:

int addition(int x, int y)

Well yes, you have to give the function a type, so that it would return that type of value. so int addition() declares an int function with the name addition. Inside the parenthesis, int x and int y are called parameters, or arguments, of the function. If you are not sure what a parameter is, look at this math function:

f(x) = x + 3 – 8

in this function x is the parameter, and what the output of this function is depends on what is passed to the parameter x. So f(3) would return you an output of –2.

Same in C++, the parameters int x and int y defines what type of values would be passed into the function. Once the function is called, for example: addition(2,5); 2 and 5 would be passed to the function, so that inside the function, variable x would hold the value 2, and variable y would hold the value 5. Likewise, return (x+y); is the same as return (2+5); So this function returns the sum of 2 and 5, which is 7, then 7 would go to wherever this function is called. So, if you called int sum=addition(2,5); the 7 would go to the right hand side of the equal sign, and thus sum would equal to 7.

Functions can return values of any type. Or they can return nothing. For example, this can happen when you just want the function to output something on the screen. This might not be called a function in other languages, like PASCAL, but it’s still a function in C++, and you would give the type void to such functions. Let’s look at this example:

void display(char x)
{
    cout<< x;
}

This is a function that does not return anything, but instead prints the parameter to the screen.

Well yes, it’s all good. But what if the user didn’t pass any argument to the function? So if you had void display(char x), and they just called display(); with nothing inside the parenthesis, what can you do to prevent an empty input? You can use default parameters. You assign something to the function if nothing is input. So in our case, void display(char x = ‘a’) would default x to ‘a’ if no input is received.

Function Overloading:

Function overloading can be done in C++. But what is function overloading? It means to declare two functions with the same name. Of course you know you cannot declare two variables with the same name. However, since functions in C++ are distinguished by the number, order, and type of the parameters, as long as two functions have different parameters, they can have the same name. Why is this useful? It adds flexibility for choosing functions based upon inputs. Well, if you don’t get what I mean by that now, you will as you program more. Just remember it is possible to overload functions in C++.

Recursion:

Recursion is basically the process, which a function calls itself. Conditions can be set to avoid infinite recursion. Let’s look at an exmple:

int addition(int x, int y)
{
      x = y-x;
      if (x>10)
          addition(x, y);
      else
         return x;
}

In this example, the function would return a value only if x is less than or equal to 10. Recursion can become very useful in situations like sorting arrays, when a standard set of tasks has to be performed again and again based on the result of each recursive run of the function.

Function Examples:

Before we go on to the next topic, let’s look back at strings. There are a few functions in the string.h library can help you with manipulating strings, and they are:

strcat(string 1, string 2) //returns a concatenation* of string 1 and string 2

strupr(string) //converts the whole string into uppercase letters

strlwr(string) //converts the whole string into lowercase letters

strlen(string) //returns an integer value of the length of the string

 

NEXT

Home
Overview
Variables
Conditions
Loops
Arrays
Functions
Files
User-Defined
Pointers
Credits
BBS Discussion
Search
Be Informed: Join us

Subscribe
Unsubscribe 
 
 

 

 

 

 

 

 

 

 

 

 

 

ThinkQuest Team C008294