www.pickatutorial.com Computer Tutorials
Top Tutorials: C/C++ | C#.NET | PHP MySQL | Java | Java Script | jQuery | HTML | xHTML | HTML5 | VB Script| CSS
Lessons Lesson 10: Functions Bookmark and Share
Home
Lesson 1
Lesson 2
Lesson 3
Lesson 4
Lesson 5
Lesson 6
Lesson 7
Lesson 8
Lesson 9
Lesson 10
Lesson 11
Lesson 12
Lesson 13
Lesson 14
Lesson 15
Lesson 16
Lesson 17
Lesson 18
Lesson 19
Lesson 20
Lesson 21
Lesson 22
Lesson 23
Lesson 24
Lesson 25
Lesson 26
Lesson 27
Lesson 28
Lesson 29
Lesson 30
Lesson 31
Lesson 32
Lesson 33
Lesson 34
Lesson 35
Lesson 36
Lesson 37
Lesson 38
Lesson 39
Lesson 40
A function is a stand-alone block of code that performs a specific task. For example to find the square of a given integer. The functions are like subtasks. They receive some information, do some process and provide a result. Functions are invoked through a calling program. Calling program does not need to know what the function is doing and how it is performing its task. There is a specific function-calling methodology. The calling program calls a function by giving it some information and receives the result. We have a main ( ) in every C program. ‘main ( )’ is also a function. When we write a function, it must start with a name, parentheses, and surrounding braces just like with main ( ). Functions are very important in code reusing. There are two categories of functions:
1. Functions that return a value
2. Functions that do not return a value
Suppose, we have a function that calculates the square of an integer such that function will return the square of the integer. Similarly we may have a function which displays some information on the screen so this function is not supposed to return any value to the calling program.

Structure of a Function

The declaration syntax of a function is as follows:

return-value-type function-name( argument-list )
{
declarations and statements
}

The first line is the function header and the declaration and statement part is the body of the function.

return-value_type:

Function may or may not return a value. If a function returns a value, that must be of a valid data type. Return type may be int, float, char or any other valid data type. The keyword return is used to return some value from the function. It does two things, returns some value to the calling program and also exits from the function. We can only return a value (a variable or an expression which evaluates to some value) from a function. The data type of the returning variable should match return_value_type data type. There may be some functions which do not return any value. For such functions, the return_value_type is void. ‘void’ is a keyword of ‘C’ language. The default return_value_type is of int data type i.e. if we do not mention any return_value_type with a function, it will return an int value.

Function-name:

The function can have any name following the rules of a variable name, but it should have a self-explanatory name like square, squareRoot, circleArea etc.

argument-list:

Argument list contains the information which we pass to the function. Some functions do not need any information to perform the task. In this case, the argument list for such functions will be empty. Arguments to a function are of valid data type like int number, double radius etc.

Declarations and Statements:

This is the body of the function. It consists of declarations and statements. The task of the function is performed in the body of the function.

Example Code:

//This function calculates the square of a number and returns it.
int square(int number)
{
int result = 0;
result = number * number;
return result;
}

Calling Mechanism:

How a program can use a function? It is very simple. The calling program just needs to write the function name and provide its arguments (without data types). It is important to note that while calling a function, we don’t write the return value data type or the data types of arguments.

Example Code:

//This program calculates the square of a given number
#include <iostream.h>
main()
{
int number, result;

result = 0;
number = 0;
// Getting the input from the user
cout << “Please enter the number to calculate the square ”;
cin >> number;
// Calling the function square(int number)
result = square(number);
cout << “ The square of “ << number << “ is “ << result;
}

Declaration and Definition of a Function

Declaration and definition are two different things. Declaration is the prototype of the function, that includes the return type, name and argument list to the function and definition is the actual function code. Declaration of a function is also known as signature of a function. As we declare a variable like int x; before using it in our program, similarly we need to declare function before using it. Declaration and definition of a function can be combined together if we write the complete function before the calling functions. Then we don’t need to declare it explicitly. If we have written all of our functions in a different file and we call these functions from main( ) which is written in a different file. In this case, the main( ) will not be compiled unless it knows about the functions declaration. Therefore we write the declaration of functions before the main( ) function. Function declaration is
a one line statement in which we write the return type, name of the function and the data type of arguments. Name of the arguments is not necessary. The definition of the function contains the complete code of the function. It starts with the declaration statement with the addition that in definition, we do write the names of the arguments. After this, we write an opening brace and then all the statements, followed by a closing brace.

Declaration:

int square ( int );

Definition:

int square ( int number)
{
return (number * number ) ;
}

Example Code:

//This program calculates the square of a given number
#include <iostream.h>
// Function declarations.
int square(int);
main()
{
int number, result;
result = 0;
number = 0;
cout << “Please enter the number to calculate the square ”;
cin >> number;
// Calling the function square(int number)
result = square(number);
cout << “ The square of “ << number << “ is “ << result;
}
// function to calculate the square of a number
int square ( int number)
{
return (number * number ) ;
}


NEXT>>>>>Lesson 11. Header Files



Home - Advertise - Contact - Disclaimer - About Us
© Since 2006 pickatutorial.com -- All Rights Reserved.