0% found this document useful (0 votes)
193 views5 pages

C++ Functions

Functions are predefined or user-defined blocks of code that perform specific tasks. Predefined functions are declared in language headers while user-defined functions must be declared and defined outside of the main program. Functions are called by their name from within the main program and can accept parameters to operate on. Parameters passed during a function call are called actual parameters, while parameters specified in the function definition are called formal parameters. Functions can return values to the calling program. Several examples are provided demonstrating how to declare function prototypes, define functions, call functions, and pass parameters.

Uploaded by

Anurag Goel
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
0% found this document useful (0 votes)
193 views5 pages

C++ Functions

Functions are predefined or user-defined blocks of code that perform specific tasks. Predefined functions are declared in language headers while user-defined functions must be declared and defined outside of the main program. Functions are called by their name from within the main program and can accept parameters to operate on. Parameters passed during a function call are called actual parameters, while parameters specified in the function definition are called formal parameters. Functions can return values to the calling program. Several examples are provided demonstrating how to declare function prototypes, define functions, call functions, and pass parameters.

Uploaded by

Anurag Goel
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1/ 5

Function - It is a predefined unit of code to achieve a desired goal.

These are of two types:

a) Predefined - Already declared and defined in the language compiler we are going
to use, we need to include the associated header file for its use in a program

b) User Defined - These need to be declared outside a program in which we are going
to use and then in the main program we need to call the name of the function along
with the values to use it.

#include<iostream.h>
void main()
{
int a=10,b=20,c=0;
c=a+b;
cout<<"Result is="<<c;
}

#include<iostream.h>
void main()
{
int add(int,int); Function prototype
int a=10,b=20,c=0;
c=add(a,b); Function Call, a and b are actual parameters or arguments
cout<<"Result is="<<c;
}

int add(int a,int b) Function Definition, a and b are formal parameters or


arguments
{
int c=0;
c=a+b;
return(c);
}

****************************************

#include<iostream.h>
void main()
{
int add(int,int); Function prototype
int a=10,b=20;
add(a,b); Function Call, a and b are actual parameters or arguments
}

int add(int a,int b) Function Definition, a and b are formal parameters or


arguments
{
int c=0;
c=a+b;
cout<<"Result is="<<c;
return 0;
}

****************************************
#include<iostream.h>
void main()
{
void add(int,int); Function prototype
int a=10,b=20;
add(a,b); Function Call, a and b are actual parameters or arguments
}

void add(int a,int b) Function Definition, a and b are formal parameters or


arguments
{
int c=0;
c=a+b;
cout<<"Result is="<<c;
}
****************************************

#include<iostream.h>
void main()
{
int add(); Function prototype
add(); Function Call
}

int add() Function Definition


{
int a=10,b=20,c=0;
c=a+b;
cout<<"Result is="<<c;
return 0;
}
****************************************

#include<iostream.h>
void main()
{
int add(); Function prototype
int c=0;
c=add(); Function Call
cout<<"Result is="<<c;
}

int add() Function Definition


{
int a=10,b=20,c=0;
c=a+b;
return(c);
}
****************************************

#include<iostream.h>
void main()
{
int addi(int,int); Function prototype
int subt(int,int); Function prototype
int mult(int,int); Function prototype
int divi(int,int); Function prototype
int a,b,res=0;
cout<<"Enter a and b";
cin>>a>>b;
res=addi(a,b); Function Call, a and b are actual parameters or
arguments
cout<<"Addition is="<<res;
res=subt(a,b); Function Call, a and b are actual parameters or
arguments
cout<<"Substraction is="<<res;
res=mult(a,b); Function Call, a and b are actual parameters or
arguments
cout<<"Multiplication is="<<res;
res=divi(a,b); Function Call, a and b are actual parameters or
arguments
cout<<"Division is="<<res;
}

int addi(int a,int b) Function Definition, a and b are formal parameters or


arguments
{
int c=0;
c=a+b;
return(c);
}

int subt(int a,int b) Function Definition, a and b are formal parameters or


arguments
{
int c=0;
c=a-b;
return(c);
}

int mult(int a,int b) Function Definition, a and b are formal parameters or


arguments
{
int c=0;
c=a*b;
return(c);
}

int divi(int a,int b) Function Definition, a and b are formal parameters or


arguments
{
int c=0;
c=a/b;
return(c);
}

********************************************
#include<iostream.h>
void main()
{
void big(int,int); Function prototype
int a,b;
cout<<"Enter a and b";
cin>>a>>b;
big(a,b); Function Call, a and b are actual parameters or arguments
}

void big(int a,int b) Function Definition, a and b are formal parameters or


arguments
{
if(a>b)
{
cout<<a;
}
else
{
cout<<b;
}
}

****************************************
#include<iostream.h>
void main()
{
int big(int,int); Function prototype
int a,b,c=0;
cout<<"Enter a and b";
cin>>a>>b;
c=big(a,b); Function Call, a and b are actual parameters or arguments
cout<<"Bigger is="<<c;
}

int big(int a,int b) Function Definition, a and b are formal parameters or


arguments
{
if(a>b)
{
return(a);
}
else
{
return(b);
}
}

*****************************************

#include<iostream.h>
void main()
{
int big(); Function prototype
c=big(); Function Call
cout<<"Bigger is="<<c;
}

int big() Function Definition, a and b are formal parameters or arguments


{
int a,b,c=0;
cout<<"Enter a and b";
cin>>a>>b;
if(a>b)
{
c=a;
}
else
{
c=b;
}
return(c);
}
*****************************************
#include<iostream.h>
void main()
{
void table(int); Function prototype
int n;
cout<<"Enter n";
cin>>n;
table(n); Function Call, a and b are actual parameters or arguments
}

void table(int n) Function Definition, a and b are formal parameters or arguments


{
int i;
for(i=1;i<=10;i++)
{
cout<<n*i<<endl;
}
}

You might also like