0% found this document useful (0 votes)
24 views10 pages

Unit2 Chapter1

Bca notes

Uploaded by

sahayaanbu907
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views10 pages

Unit2 Chapter1

Bca notes

Uploaded by

sahayaanbu907
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

UNIT II –Chapter 1

Functions in C++: Introduction – The Main Function – Function prototyping – Call


by Reference – Return by reference – Inline Functions - Default Arguments – const
Arguments – Function Overloading – Math Library Functions

Q.No:1

THE MAIN FUNCTION


The main function is a special function. Every C++ program must contain a
main() function. It serves as the entry point for the program. The computer will
start executing the code from the beginning of the main function.

Types of main Function:


1) The first type is – main function without parameters :
Syntax:

int main()
{
….
return 0;
}

2) Second type is main function with parameters :

Syntax:

int main(int argc, char * const argv[])

...

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

FUNCTION PROTOTYPING or FUNCTION DECLARATION


A function must be declared globally in a program to tell the compiler about the
function name, function parameters, and return type.

A function declaration (also known as function prototype) consists of four parts.

 Function type (return type)


 Function name
 Parameter list
 Terminating semicolon

Syntax:

return-datatype function name(datatype arg1, datatype arg2…… datatype argn);


Example:

void add(int a, int b);


We can also declare a function with an empty argument list, as in the below example

void display();

Points to note – Function Declaration

1. The parameter list must be separated by commas.


2. The types must match the types of parameters in the function definition, in number
and order.
3. Use of parameter names in the declaration is optional.

Ex: void add(int , int );

4. If the function has no formal parameters, the list is written as (void).


5. When the declared types do not match with the types in the function definition,
compiler will produce an error.

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:

returndatatype function name(datatype &arg1, datatype &arg2…… datatype &argn)


{
Statement block;
}

Example program :

// program to find the square of any number

#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

In the above example, the formal argument ‘m’ value is changed to


25.Hence, the actual argument ‘n’ value also changed to 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:

Enter two numbers


12
21
a value is 12

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 :

returndatatype function name(datatype arg1=value)


Example program:

#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

In C++, an argument to a function can be declared as constant.


The qualifier const tells the compiler that the function should not
modify the argument.
The compiler will generate an error when this condition is violated.
This type of declaration is significant only when we pass arguments
by reference or pointers.

Syntax :

returndatatype function name(const datatype arg1)


Example:

void add(const int a,int b);

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

C++ provides a large number of mathematical functions that can be used


for performing certain commonly used calculations. C++ Mathematical functions
are predefined functions. These functions are defined in the math.h header file.

Function Purpose Syntax example output

It returns the absolute value of an integer abs(int x) abs(-18) 18


number.Absolute value means number
abs()
without negative sign.

sqrt() It returns the square root of a positive sqrt(double x) sqrt(25) 5


number

Ceil() It returns the nearest integer number ceil(double x) ceil(12.4) 13


greater than the number passed as
argument

floor() It returns the nearest integer number less floor(double floor(12.7) 12


than the number passed as argument x)

pow() It returns the power value pow(float x, int y) Pow(3,2) 9

cos() returns the cosine of x cos(double x) cos(0) 1

sin() returns the sine of x sin(double x) sin(90) 1

tan() returns the tangent of x tan(double x) tan(45) 1

exp() returns e (2.7182818) raised to exp(double x) exp(10)


the xth power.

log(x) returns the natural (base e) logarithm of x. log(double x) log(10) 1

log10(x) returns the base 10 (or common) log10(double x) log10(10)


logarithm for x

min() It used to find the highest value min(int x,int y) min(5, 10) 10
of x and y

You might also like