Professional Documents
Culture Documents
Main function:
C does not specify any return type for the main function which is the starting
point for the execution of a program.
main()
{
............
// main program statements
............
}
This is perfectly valid because the main () in C does not return any value.
The functions that have a return value should use the return statement for
termination.
int main()
............
............
return 0;
}
FUNCTION PROTOTYPING:
The argument-list contains the types and names of arguments that must be
passed to the function.
Example:
We can also declare a function with an empty argument list, as in the following
example.
In C++, this means that the function does not pass any parameters.
This means that when the function is working with its own
arguments, it is actually working on the original data.
# include<iostream.h>
void main()
{
void swap(int &, int &);
int a, b;
cout<<"enter a and b";
cin>>a>>b
cout<<"before swapping"<<a<<b;
swap(a, b);
cout<<"after swapping"<<a<<b;
}
void swap(int &x, int &y)
{
int t;
t=x;
x=y;
y=t;
}
Return by Reference:
Since the return type of max () is int &, the function returns reference to x or
y(and not the values). Then a function call such as max (a,b) will yield a
reference to either a or b depending on their values. This means that this
function call can appear on the left-hand side of an assignment statement.
That is, the statement
Max (a,b) = -1
is legal and assigns -1 to a if it is larger, otherwise -1 to b.
Inline Functions:
By using these inline functions, the execution speed of the program becomes
fast. Whenever the function is defined as inline, and when it is executed, the
code in the inline function will be replaced in the calling function. In this there
will be no moving of control to the function and again returning back the control
to the calling function.
Example:
# include <iostream.h>
Inline float square (float x)
{
x=x*x;
return(x);
}
void main()
{
float num;
cout<<Enter a number<float>:;
cin>>num;
cout<<Its square=<<square(num);
}
DEFAULT ARGUMENTS:
In a C++ function call, when one or more arguments are omitted, the function
may be defined to take default values for omitted arguments by providing the
default values in the function prototype. These arguments are supplied by the
compiler when they are not specified by the programmer explicitly.
Example:
# include <iostream.h>
void show (int x=5, float y=1.1, char *z=JAVA)
{
cout<<Integer:<<x<<endl;
cout<<float:<<y<<endl;
cout<<string:<<z<<endl;
}
void main()
{
show(10, 9.3, C++);
show(20, 7.9);
show(30);
show();
}
OUTPUT:
Integer: 10 20 30 5
Float: 9.3 7.9 1.1 1.1
String: C++ JAVA JAVA JAVA