Unit2 Chapter1
Unit2 Chapter1
Q.No:1
int main()
{
….
return 0;
}
Syntax:
...
return 0;
}
In c++ , the main() function returns a value of type int to the operating
system.The return value of main() function shows how the program exited. The
normal exit of program is represented by zero return value. If the code has errors,
fault etc., it will be terminated by non-zero value.
In C++ language, the main() function can be left without return value. By default, it
will return zero.
Most c++ compiler will generate an error or warning message if there is no return
statement.
Error: function should return a value.
Advantage of functions in C
o By using functions, we can avoid rewriting same logic/code again and again in a
program.
o We can call functions any number of times in a program and from any place in a
program.
o We can track a large program easily when it is divided into multiple functions.
o Reusability is the main achievement of functions.
o It reduces the length of source program.
Q.No:2
Syntax:
void display();
The parameters used in prototypes and function definitions are called formal parameters
and parameters used in function calls are called actual parameters.
Q.No:3
CALL BY REFERENCE
When we pass arguments by reference, the formal arguments in the
function definition become aliases to the actual arguments in the calling
function. So, if any changes are made in the formal arguments it will affect the
actual arguments.
Syntax:
Example program :
#include<stdio.h>
void square (int &m);
void main ()
{
clrscr ();
int n;
printf("enter one number:\n");
cin>>n;
square (n);
cout<<”n value is=”<<n;
getch ();
}
void square (int &m) // m is the alias variable
{
m=m*m;
cout<<"m value is = "<<m;
}
Output:
enter one number
5
m value is =25
n value is=25
RETURN BY REFERENCE
A function can also return a reference.
Syntax for function call
Functionname(arguments)=value;
Syntax for function definition:
Returndatatype& function name(datatype arg1, datatype arg2…… datatype
argn)
{
Statement block;
}
Example program:
#include<stdio.h>
int & max (int &x,int &y) ;
void main ()
{
clrscr ();
int a,b;
printf("Enter two numbers:\n");
cin>>a>>b;
max(a,b)=1;
cout<<”a value is=”<<a;
cout<<”b value is “<<b;
getch ();
}
int & max (int &x,int &y) // x,y are the alias variables
{
if (x>y)
return(x)
else
return(y)
}
Output:
b value is 1
The above program assigns 1 to variable a if it is larger , otherwise 1 to
variable b.
INLINE FUNCTIONS
Whenever a function is called, compiler takes a lot of extra time in executing
a series of instructions for tasks such as jumping to the function, saving registers,
pushing arguments into stack, and returning to the calling function. This process can
sometimes cause overhead in function calls, especially if the function is small and its
execution time is less than the switching time. This issue is resolved by using the
inline functions.
Inline function is a function that is expanded in line when it is called.(ie) the
compiler copies the function definition at the place of function call at compile
time.It improves the execution time and speed of the program.
Syntax:
inline returndatatype function name(datatype arg1, datatype arg2…… datatype argn)
{
Statement block;
}
#include<stdio.h>
inline void square (int m)
{
m=m*m;
printf("m value is = "<<m);
}
void main ()
{
clrscr ();
int n;
printf("enter one number:\n");
scanf ("%d", &n);
square (n);
cout<<”n value is=”<<n;
getch ();
}
DEFAULT ARGUMENTS
The default arguments are used when we provide no arguments or only
few arguments while calling a function. A default argument is a value in the
function declaration that is automatically assigned by the compiler if the
calling function does not pass any value to that argument.
Syntax :
#include<stdio.h>
void add (int m,int n=5);
void main ()
{
clrscr ();
int a,b;
printf("enter two numbers:\n");
cin>>a>>b;
add(a,b); //no missing argument
add(a); //function call with default argument; one argument missing
getch ();
}
void add (int m,int n)
{
int s;
s=m+n;
cout<<"sum is = "<<s;
}
Output:
enter two numbers
10
20
sum is =30
sum is=15
CONST ARGUMENTS
Syntax :
Example program:
#include<stdio.h>
void area (const int m,) ;
void main ()
{
clrscr ();
int n;
printf("Enter one number:\n");
cin>>n;
square(n);
getch ();
}
void square (const int m)
{
s=m*m;
m=m*m; //error will occur
cout<<"area is = "<<s;
}
FUNCTION OVERLOADING
Two or more functions with the same name is called function overloading. But
the types of the arguments and/or the number of arguments should be different.
Function overloading is achieved during compile time.
The function overloading in c++ feature is used to improve the readability
of the code. It is used so that the programmer does not have to remember various
function names.
Examples of overloaded functions
void add(int a, int b);
void add(int a, int b, int c); //number of parameters different
void add(double m, double n); //type of parameters different
Example program
#include<iostream.h>
#include<conio.h>
void add(int x,int y);
void add(int x,int y,int z);
void add(float p,float q);
void main()
{
int a,b,c;
float m,n;
cout<<”Enter three integer values”;
cin>>a>>b>>c;
cout<<”Enter two float numbers”;
cin>>m>>n;
add(a,d);
add(a,b,c)
add(m,n);
getch();
}
void add(int x,int y)
{
int s;
s=x+y;
cout<<”sum is “<<s;
}
void add(int x,int y,int z)
{
int s;
s=x+y+z;
cout<<”sum is “<<s;
}
void add(float p,float q)
{
float s;
s=p+q;
cout<<”sum is “<<s;
}
MATH LIBRARY FUNCTIONS
min() It used to find the highest value min(int x,int y) min(5, 10) 10
of x and y