Input Operator:: Cascading of I/O Operators
Input Operator:: Cascading of I/O Operators
Input Operator:: Cascading of I/O Operators
Constants refer to fixed values that do not change during the execution of a
program.
Basic Data types:
What is a Function?
• A number of statements grouped into a single logical unit is referred to as a Function.
• A function is a self-contained block of statements that perform a coherent task of some kind.
• Every C program can be thought of as a collection of these functions.
• The function main() in the program is executed first. So, When are the other functions of the
program executed?
• Normally, the other functions are executed when the function main() calls them directly or
indirectly.
•It is necessary to have a single function main() in every C program, along with other functions
used/defined by the programmer.
•The function main() is also a user defined function except that the name of the function, the
number of argument, and the argument types are defined by the language. The individual
statements and statement blocks are written by the programmer in the body of the function
main().
•This example has a function PrintMessage() in addition to main().
•Here, through main(), we are calling the function PrintMessage(). By calling we mean that the control
passes to function PrintMessage().
•The activity of main() is temporarily suspended, it falls asleep while the PrintMessage() wakes up and
goes to work.
•When the PrintMessage() function runs out of statements to execute, the control returns to main(),
which comes to life again and begins executing its code at the exact point where it left off.
•Thus the main() becomes the calling function and PrintMessage() becomes the called function.
From the above two examples, we can conclude that:
a) C program is a collection of one or more functions.
b) If a program contains only one function, it must be main().
c) If a C program contains more than one functions, then one of these functions must be
main(), because execution always begins with main().
d) There is no limit on the number of functions that must be present in a C program.
e) Each function in a C program is called in the sequence specified by the function calls in
main().
f) A function gets called when the function name is followed by a semicolon.
g) A function is defined when the function name is followed by a pair of braces in which one or
more statements may be present.
h) Any function can be called from any other functions.
i) A function can be called any number of times.
j) The order in which the functions are defined in a program and the order in which they are
called need not necessarily be same.
k) A function can call itself. Such a process is called Recursion.
l) A function can be called from any other function, but a function can not be defined in
another function. Thus the following code would be wrong.
•Return Value:
• The last statement of the function return x;
causes the function cube to return the value stored in x.
Placement of function
Function Declaration: it informs the compiler that cube is a function that returns an integer value
and accepts an integer as an argument to the function.
A semicolon must always follow the closing brace of the function declaration.
Function declaration informs the compiler about the function interface as it appears from outside.
It says nothing about the code that comprises the function cube.
Concepts associated with Functions
1. Function Declaration or Function Prototype
2. Function Definition (function declaratory
and function body)
3. Combination declaration and function
definition
4. Passing Arguments
5. Return statement
6. Function call
1. Function Declaration
A function declaration provides the following information to the compiler:
The name of the function
The type of the value returned
The number and type of arguments that must be supplied in a call to a function.
When a function call is encountered, the compiler checks the function call with its declaration,
so the correct argument types are used.
Syntax of function Declaration:
Ret_type function_name(type, type, ….., type);
A function can return any data type, if there is no return value, the keyword void is placed
before the function name.
The function declaration terminates with semicolon.
Function Declaration also called Prototypes, since they provide a model or blue print of the
function.
2. Function Definition
Function Definition is similar to function declaration but does not have a semicolon.
The first line of the function definition is called a function Declarator.
This is followed by function body. It is composed of statements that make up the function,
enclosed in curly braces.
The declaratory and declaration must use the same function name, number of arguments,
argument types and the return type.
No function definition is allowed within a function definition.
3. Elimination of Function Declaration
C gives the freedom to place function declarations anywhere in the program.
If the functions are defined before they are called, then the declarations are unnecessary.
In a program where there are several functions that call each other, the programmer has to
arrange the functions, such that they are defined before being called by another function.
However, in large programs it is preferable to declare all the functions in the beginning, to avail
the flexibility of arranging the function in any order.
4. Function Return Value
oFunctions in C may or may not have any return values.
oIf a function does not return any value, the return type in the function definition and function
declaration is specified as void.
oOtherwise, the return type is specified as a valid data type.
oThere may be multiple return statements in the middle of a function.
oA return statement at the end is optional for functions without return values.
oThe return statement can take the following forms:
return;
or
return (expression);
oThe plain return does not return any value, it acts much as the closing brace of the function.
oWhen a return is encountered, the control is immediately passed back to the calling function.
oIf a function is specified as returning a value, the return must have a value associated with it.
5. Function Call
oA function is a dormant entity, which comes to life when a call is made to the function.
oA function call is specified by the function name followed by the values of the actual
parameters enclosed in the parenthesis, terminated by a semicolon.
oA function that does not return any value may not be used in any expressions, but can be called
in to perform certain tasks specified in the function.