In C , C++
What is function?
The functions are like subtasks. They receive some
information, do some process and provide a result.
We have a main() [ function ] in every C program
Writing a function
When we write a function. It must start with a name,
parentheses and sourrounding braces just like a main().
Function are very important in code resuse.
Categories of functions
There are two categories of functions
1. Function that return a value
2. Function that do not return a value
Function that 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
Function that do not return a value
Suppose we have a function that display some information
on the screen so this function is not return a value to the
Structure of function
The declaration syntax of a function is as follows
declarations and statements
The first line is the function header and the declaration
and statement part is the body of a function
Structure of a function
D. Declarations and Statements:
Frist line of the function
Function may or may not return a value, if a function a value
that must be of a valid data-type(unique I mean only one
data-type) may be int,float,char,etc
Int data-type only return integer value not float, char or string
How can we return some value from a function?
The keyword is return which 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.
Do not Return-value-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 is used in c , c++ language
The same rules of variable naming conventions are applied
to functions name. Function name should be self-explanatory
like square, squareRoot, circleArea etc.
argument-list: Argument list contains the information which
we pass to the function. Some function does 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.
Decleration and statements
This is the body of the function. It consists of declarations
The task of the function is performed in the body of the
//This function calculates the square of a number and returns it
int square(int number)
int result = 0;
result = number*number;
Function Calling mechanism
How a program can use a function? It is very simple. The
calling program just need 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-type of arguments
See the example…
// This program calculates the square of a given number
# include <iostream.h>
int number, result;
// Getting the input from the user
cout << “ Please enter the number to calculate the
// Calling the function
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 neto 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.