Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Functions in c++

The document provides an overview of functions in C++, including their definition, types (built-in and user-defined), and advantages such as modular programming and code reusability. It explains the structure of functions, including prototypes, declarations, and calls, as well as different categories of functions based on return values and arguments. Additionally, it discusses the concepts of call by value and call by reference, and introduces arrays as a way to store related variables.

Uploaded by

shivaal.s56
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Functions in c++

The document provides an overview of functions in C++, including their definition, types (built-in and user-defined), and advantages such as modular programming and code reusability. It explains the structure of functions, including prototypes, declarations, and calls, as well as different categories of functions based on return values and arguments. Additionally, it discusses the concepts of call by value and call by reference, and introduces arrays as a way to store related variables.

Uploaded by

shivaal.s56
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 19

Functions in C++

1
Function
A function is a block of code that performs a
specific task.
Example: Suppose we need to create a program
to create a circle and color it. We can create two
functions to solve this problem:
•a function to draw the circle
•a function to color the circle

2
Types

1.Built in functions :- are part of compiler package.


Part of standard library made available by compiler.
Can be used in any program by including respective
header file.

2.User defined functions:- Created by user or


programmer. Created as per requirement of the
program.
Advantages
• Support for modular programming
• Reduction in program size.
• Code duplication is avoided.
• Code reusability is provided.
• Functions can be called repetitively.
• A set of functions can be used to form
libraries.
User defined function
Void main()
{
Statement 1;
Statement 2; multiply();
Statement3;
;
; Sum();
return0;
}

multiply()
{
;
}
Parts of a function

main function
{
function prototype declaration
function call
-------;
}

function declaratory/definition
{
------;
return statement
}
Function prototype
A function prototype is a declaration of a function that tells the
program about the type of value returned by the function, name of
function, number and type of arguments.

Syntax:
Return_type function_name (parameter list/argument);
int add(int,int);
void add(void);
int add(float,int);
4 parts
i. Return type Variable declaration
ii. Function name Data_type variable_name ;
int x=5;
iii. Argument list float marks; int price;
iv. Terminating
semicolon
Function call
A function must be called by its name followed by
argument list enclosed in semicolon.

Syntax: function_name (parameter list/argument);

add(x,y);
add(40,60);
add(void); or add();
Note: data type not to be mentioned.

Suppose int add(int,int); //prototype


Now to this function add(x,y); //function
or add(40,60); call
Function categories

i) Function with no return value and no


argument. void add(void);
ii) Function with arguments passed and no
return value. void add(int,int);
iii)Function with no arguments but returns a
value. int add(void);
iv)Function with arguments and returns a
value. int add(int,int);
I. Function with no return value and no
argument
void main()
{
void disp(void);
//prototype
disp(); //caller function
No value returned from No arguments passed
calle to caller function return 0; from caller to calle
}

void disp() //calle function


{
cout<<“--------”<<endl;
}
//program to print square of a number using functions. void
main()
{
void sqr(void);
sqr();
getch();
return 0;
}

void sqr()
{
int no;
cout<<“enter a no.”; cin>>no;
cout<<“square of”<<no<<“is”<<no*no;
}
ii. Function will not return any value but passes argument
#include<iostream.h>
#include<conio.h>
void add(int,int);
int main()
{

int a,b;
add(a,b);
cout<<“enter values of a and b”<<endl;
cin>>a>>b; a
add(a,b);
getch(); return 0;
} b
void add(int x,int y)
{
int c; c=x+y; void
cout<<“addition is”<<c; add(int
}
x,int y);
iii) Function with arguments and return value

main function
{
int sqr(int); //function prototype
int a,ans;
cout<<“enter a number”; cin>>a;
ans=sqr(&a);
cout<<“square of number is”<<ans; //function call
getch();
return 0;
}

int sqr(int X) //function declaratory/definition


{
return(X*X);
}
iv) Function with no arguments but returns a value
int main()
{
int add(void); int z; Function call
z=add(); add(x,y);
cout<<sum of 2 nos is”<<z;
getch(); return 0;
i.e
} z=add(x,y);

int add(void);
{
int a,b;
cout<<“enter 2 nos”;
cin>>a>>b;
return(a+b);
}
Call by value
A function can be invoked in two manners (i)call by value
(ii)call by reference
The call by value method copies the value of actual parameters into
formal parameters i.e the function creates its own copy of
arguments and uses them.

add(a,b); Values of variables a


and b are passed to

Call by value
} X,Y

where the values of void add(int x,int y); Now if you change
variable are passed
to functions { the value of X and Y,
those changes are
--------; not seen in a and b

}
Call by
reference
In call by reference method in place of calling a value to the function being
called , a reference to the original variable is passed .i.e the same variable
value can be accessed by any of the two names.
In function call
add(a,b); We write reference
variable for formal
} arguments

No need for return


void add(int &x,int &y); &X,&Y will be the
statement { reference variable for
a and b. if we change
--------; X and Y, Value of a
and b are changed
} accordingly
Arrays
• Declare and initialize a one-dimensional
array
• Enter data into a one-dimensional array
• Display the contents of a one-dimensional
array
• Pass a one-dimensional array to a function
• Calculate the total and average of the
values in a one-dimensional array
17
Arrays
A group of related variables with the same
data type is referred to as an array
Example: Suppose in CMIN101 module there
are 51 students, and if we need to store the grades
of all of them. Instead of creating 51separate
variables, we can simply create an array

dataType arrayName[arraySize];
int x[6]; int y[10] ={1,2,3,4,5,6,..10}
18
• X={20,10,40,50}
• X[0]=20
• X[1]=10
• X[2]=40
• X[3]=50

• X[2][3]=
• X[3][3]
19

You might also like