0% found this document useful (0 votes)
21 views15 pages

Lecture10 24august CAP202

Uploaded by

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

Lecture10 24august CAP202

Uploaded by

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

Functions and input/output

streams
Outline
• Creating User Defined Functions
• Functions With Default Arguments
• Inline Functions
What is function????
• Function is a self contained block of
statements that perform a coherent task of
some kind.
• Every C++ program can be a thought of the
collection of functions.
• main( ) is also a function.
Types of Functions.

• Library functions
– These are the in- -built functions of ‘C++ ’library.
– These are already defined in header files.
• User defined functions.
– Programmer can create their own function in C++
to perform specific task
Why use function?

• Writing functions avoids rewriting of the same


code again and again in the program.
• Using function large programs can be reduced
to smaller ones. It is easy to debug and find
out the errors in it.
• Using a function it becomes easier to write
program to keep track of what they are doing.
//Function Declaration
retn_type func_name(data_type 1,data_type par2);

//Function Defination
rent_type func_name(data_type par1,data_type par2)
{
// body of the function
}

//Function Call
func_name(par1,par2);
Function prototype
• A prototype statement helps the compiler to
check the return type and arguments type of
the function.
• A prototype function consist of the functions
return type, name and argument list.
• Example
– int sum( int x, int y);
Example
#include<iostream>

using namespace std;

void sum(int, int); // function declaration

int main()
{
int a, b;
cout << "Enter the two numbers: ";
cin >> a >> b;
sum(a, b); // function calling
cin.get(); // or use cin.get() to pause the program
return 0;
}

void sum(int x, int y) // function definition


{
int c = x + y;
cout << "Sum is: " << c << endl;
}
#include<iostream.h>
int sum(int, int);
void main()
{
int a=10,b=20;
int c=sum(a,b); /*actual arguments
cout<<“sum is” << c;
getch();
}
int sum(int x, int y) /*formal arguments
{
int s;
s=x+y;
return(s); /*return value
}
Categories of functions
A function with no parameter and no return value
A function with parameter and no return value
A function with parameter and return value
A function without parameter and return value
A function with no parameter and no return value
#include<iostream.h>
void print();
void main()
{
cout<<“no parameter and no return value”;
print(); /*func calling
getch();
}
void print() /*func defination
{
For(int i=1;i<=30;i++)
{
cout<<“*”;
}
Cout<<“\n”;
}
A function with no parameter and no return value

• There is no data transfer between calling and


called function
• The function is only executed and nothing is
obtained
• Such functions may be used to print some
messages, draw stars etc
A function with parameter and no return value

#include<iostream.h>
void mul(int,int);
void main()
{
clrscr();
int a=10,b=20;
mul(a,b); /*actual arguments
getch();
}
void mul(int x, int y) /*formal arguments
{
int s;
s=x*y;
Cout<<“mul is” << s;
}
A function with parameter and return value
#include<iostream.h>
int max(int,int);
void main()
{
clrscr();
int a=10,b=20,c;
c=max(a,b);
cout<<“greatest no is” <<c;
getch();
}
int max(int x, int y)
{
if(x>y)
return(x);
else
{
return(y);
}
}
A function without parameter and return value

#include<iostream.h>
int sum();
void main()
{
clrscr();
int a=10,b=20;

int c=sum(); /*actual arguments


Cout<<“sum is”<< c;
getch();
}
int sum() /*formal arguments
{
int x=10,y=20;
return(x+y); /*return value
}

You might also like