You are on page 1of 9

OBJECT: TO Function

BECOME FAMILIAR WITH FUNCTIONS IN

C++.

The function is one of the fundamental building blocks of C++ programming language. A function is a sub program that contains some program statements. The function collects a number of program statements and forms a unit with a particular name. This function then can be called at any stage in the program. The function name declaration follows the same rules of identifiers. The parentheses () are always followed by the function name in the function. The basic idea to create the functions is to divide a large program containing hundreds of statements in the small units or blocks so that the program becomes more clearer and conceptable. A single program may contain hundreds or even thousands of function but each program should contain the main function because main function is the gate way to enter in C++. The function also contains some return type and parameters. The return type is type of some data that will be returned by the function at its end. The return type may be of type int, type float, type long, type long double, type char, void (empty) or user defined data type. The return type is given before the function name. The parameters are the series of variables separated by comma contained in the parentheses that pass the arguments through the function.

Return type Function name Parameters

void point(int x, int y) { statement; statement; statement; }


Function Body

The Function Declaration In case of variables we observed that we cannot use variables without declaring them, here the scenario is same i.e. we cannot use our own defined functions without declaring them because the compiler is unfamiliar with the functions developed by the users so the programmer should first tell the compiler that there is a function which has this return type data and takes these arguments. The function should be declared before it is called otherwise the compiler will complain. The function declaration includes the function return type, the function name and the declaration of its parameters in the parentheses and the semicolon at the last. The function declaration is also known as prototype because it is the blueprint for the function. The function declaration tells the compiler that the function looking like this is on the way in the program and do not be confused with it. The syntax for declaring a function is,
Return type Function name Parameters

int charline(int a, char ch);

Note: semicolon here

In above function declaration syntax a user defined function named charline is declared. It has integer return type and the two parameters i.e. integer a and character ch. Note that the function can have any return type and can contain number of parameters in the parentheses. The Function Definition The function definition tells the actual purpose and process of the function. The function definition contains the block of code that is executed when ever that function is called. The whole definition of the function resides in the curly braces of the function body. The function definition starts with the declarator. Declarator is same as declaration but with out the semicolon i.e. void charline(int a, char ch) then the body of the function follows. The function return type, the function name and the parameters in the parentheses should match the function declaration correspondents. If the function is first declared before the calling then you can place the function definition in any place through out the program but if you do not have declared the function then you should place the function definition first before its calling. This approach of placing the definition first by eliminating the function declaration is not considered as standard approach and suits only in very small programs. The better approach is to declare the function at the top before it is called rather than by placing definition first.

Note that the declarator is always not terminated by the terminator. When ever the function is called each time the control is transferred to the body of the that function. The syntax of the function definition is as under,

Declarator

int charline(int a, char ch) { for(int i=1; i<=a; i++) cout<<ch; cout<<endl; }

Note: no semicolon here

Function body

Function calling When the function is declared and defined, now to utilized and make the most of function, the function is called. The function calling transfers the control from the point of calling directly to the function definition. The function calling carries the constant values or variables in the arguments in the parentheses (if used) and passes them from the function definition and evaluates the result. The function calling does not contain the return type and always terminated by the terminator. The same function can be called multiple number of time in a program, which evaluates the beauty of the breaking the program in to functions. The syntax of calling the function is given as,

Function name Constant arguments

charline(10, *);

Note: semicolon here

Table 6.1, Function components.


Component Purpose Example

Declaration (Prototype) Call Defining Function Declarator

Specifies function name, argument types, and return value. Alerts compiler (and programmer) void funct(); that function is coming up later. Causes the function to be executed.

funct();

The function itself. Contains the lines of code that void funct() constitute the {// lines of code} First line of definition

void funct()

The simple function

Program (funct.cpp)
#include <iostream.h> #include <conio.h> void charline(int a, char ch); void main() { clrscr(); charline(45, *); charline(45, _); charline(25, +); getch(); } void charline(int a, char ch) { for(int i=1; i<=a; i++) cout<<ch; cout<<endl; } The output must be: ********************************************* --------------------------------------------+++++++++++++++++++++++++

Passing arguments to the function The arguments are the data with any data type, which are passed through the function. When the user creates certain function, the function provides some parameters and the arguments are the data that are placed in the place parameters to pass those data through that function. The data type of the arguments and the data type of the parameters should match with each other. You can pass an argument through the function either by constants or by variables. Passing constant as arguments For passing the constant arguments you have to place the constants of any data type in the arguments where as the data types of the constant arguments and the parameters should match. When you have passed the constants as the arguments now you can get a fixed particular result. Examine the below program:

Program (ConstArg.cpp)
#include <iostream.h> #include <conio.h> void charline(int a, char ch); void main() { clrscr(); charline(45, *); charline(45, _); charline(25, +); getch(); } void charline(int a, char ch) { for(int i=1; i<=a; i++) cout<<ch; cout<<endl; }
In the above program the constant arguments are passed through the function charline. In first function call the integer constant 45 and the character constant * are passed as the arguments. In second function call the integer constant 45 and the character constant - are passed as the arguments and in the last function call the integer constant 25 and the character constant + are passed as the arguments. Now these constant arguments are place in the memory of the parameter variable, the

integer constants 45, 45, 25 are placed in the parameter a and the character constants *, -, + are place in the parameter ch. In the body of the function charline the for loop is executed integer constant number of time and prints the character constant that much number of time. So if you are passing the constants as arguments then the values for the arguments are set to remain constant and can not be altered. Also the data type of the constant arguments should match the corresponding parameters data type. Passing variables as arguments The phrase passing the variable does not mean that the whole variable is passed as the argument but the data/value stored in that variable is passed thought the function as the argument. When passing the variable as arguments the data type of the variable should match the data type of the corresponding parameter. The advantage of the variable arguments is that unlike constant arguments, you can place the values in the arguments according to the situation and make the function and program general purpose. Examine the below program:

Program (VarArg.cpp)
#include <iostream.h> #include <conio.h> void charline(int a, char ch); void main() { int intin; char chin; clrscr(); cout<<Enter the character: ; cin>>chin; cout<<Enter the number of times: ; cin>>intin; charline(intin, chin); getch(); } void charline(int a, char ch) { for(int i=1; i<=a; i++) cout<<ch; cout<<endl; }

In the above program the variables intin and chin are passed through the function as the arguments. The program at the startup will ask you to enter the values of character chin and the integer intin then it passes the value placed in the these variable as the arguments through the function. In this program you can pass the arguments according to the situation so the program has become generalized by the use of variable arguments. Returning Value from the function The function can return a value after passing the arguments through its definition by the return statement. When you define a function you first give its return type that can be integer, floating point, character or void (empty). The return statement in the function body returns a value of the type that was declared as the return type of the function when the function completes its execution. This return value is send to the calling of the function. The data type declared as the return type of the function should match with the value that the function returns. Examine the below program:

Program (ReturnValue.cpp)
#include <iostream.h> #include <conio.h> int add(int a, int b); void main() { int num1, num2; clrscr(); cout<<Enter first number: ; cin>>num1; cout<<Enter second number: ; cin>>num2; cout<<The addition is: <<add(num1,num2); getch(); } int add(int a, int b) { return (a + b); }
In the above program the function add is declared with the return type of integer, which means that when ever the function is called it will return an integer type value and contains two integer type parameters i.e. a and b. When the function add is called, the values of num1 and num2 are passed through the function definition and the addition of num1 and num2 is returned to the calling as the integer type value.

Reference Arguments When we pass the argument with the variables the copy of that variable is passed through the function and no effect is implemented on the actual variable but when the variables are passed by the reference the actual variables are affected and modified by the function. The reference provides another name for what ever the variable is used and the address of the variable is passed through the function. The references are most commonly used for passing the arguments through the function. We use the reference argument when we need to modify the actual variable. When declaring a reference variable the ampersand sign & is used which tells the compiler that the variable used is a reference variableanother name for what ever the variable is used in the arguments. The ampersand sign can be used with the variable name or with the data type as:

int &var1; int& var1;

//Perfectly Legal //Perfectly Legal

Program (Reference.cpp)
#include <iostream.h> #include <conio.h> void swap(int& a, int& b); void main() { int num1, num2; clrscr(); cout<<Enter first number: ; cin>>num1; cout<<Enter second number: ; cin>>num2; cout<<num1 = <<num1<<endl; cout<<num2 = <<num2<<endl; swap(num1, num2); cout<<num1 = <<num1<<endl; cout<<num2 = <<num2<<endl; getch(); } void swap(int& a, int& b) { int temp; temp=a; a=b; b=temp; }

In above program the user defined function swap is declared, which contains two reference arguments and a and b are two reference variables which will pass through the body of the function and the effects will be found on these variables. The program first will input the values of num1 and num2 from the user then these variables are passed through the function swap which will swap the values of num1 and num2. Now you will observe that the values of num1 and num2 are swapped which is the indication that some effect is implemented on these variables by the function.

You might also like