0% found this document useful (0 votes)
12 views16 pages

Functioninc

Uploaded by

priyanka Suyal
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)
12 views16 pages

Functioninc

Uploaded by

priyanka Suyal
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/ 16

functions

A function is a group of statements that together


perform a task.
Another advantage of using functions is that it is
possible to reduce the size of program by calling
and using them at different places in program.
Every C++ program has at least one function,
which is main(), The C++ standard library provides
numerous built-in functions that your program can
call.
About function
• A function declaration tells the compiler
about a function's name, return type, and
parameters.
A function definition provides the actual body
of the function.
• Function call
note: proto type and call is always in main()
and definition is always outside the main.
Different parts of a function −

• Return Type − The return_type is the data type of the value the
function returns. A function may return a value. Some functions
perform the desired operations without returning a value. In this
case, the return_type is the keyword void.
• Function Name − This is the actual name of the function. The
function name and the parameter list together constitute the
function signature.
• Parameters − When a function is create, you pass a value to the
parameter. This value is referred to as actual parameter or
argument. The parameter list refers to the type, order, and number
of the parameters of a function. Parameters are optional, that is a
function may contain no parameters.
• Function Body − The function body contains a collection of
statements that define what the function does.
Pre defined string function
• Str cat(); for concatination
• Str cmp(); comparing the string
• Str cpy(); copy one string from one variable
to another
• Str len(); for calculate the length
Str cat();
• Void main()
• {
• Char n1[15];
• Char n2[5];
• Cout<<“enter 1st string”;
• Cin.getline(n1,15);
• Cout<<“enter2nd string”;
• Cin.getline(n2,5);
• Strcat(n1,n2);
• Cout<<“concatination of two string=“<<n1;
• Getch();
• }
Strcpy()
• Void main()
• {
• Char n1[15];
• Char n2[15];
• Cout<<“enter 1st string”;
• Cin>>n1;
• Cout<<“enter2nd string”;
• Cin>>n2;
• Strcpy(n2,n1);
• Cout<<n2;
• Getch();
• }
Strcmp()
• Void main()
• {
• Char n1[15];
• Char n1[15];
• Int r;
• Cout<<“enter 1st string”;
• Cin>>n2;
• r=strcmp(n1,n2);
• If(r>0)
• {
• Cout<<“string 1st is greater”;
• }
• Else if(r<0)
• {
• Cout<<“string 2nd is greater”;
• }
• Getch();
• }
WAP to enter any string from the user and check
whether its is pelindrom or not
• Void main()
• {
• Char n1[15],n2[15]
• Int r;
• Cout<<“enter any string”;
cin>>n1;
Strcpy(n2;n1);
• Str rev(n1)
• r=str cmp(num1,num2);
• If r=0 {
• Cout<<“pelindrom”;
• }
• Else
• Cout<<“not pelindrom”;
• }
Function prototyping
• Function prototyping is one of the major
improvement added to c++ functions.
• The prototype describes the function interface
to the compiler by giving details such as the
number and type of argument and the type of
return values.
• syntax:
• Type function name(argument-list);
• Ex: float volume(int x, int y, int z);
• Float(int x, y) // can’t declare
• Float(int, int) // can daclare
User defined function
• Types of function
• 1)no return type no argument
• 2) with return type with argument
• 3) no return type with argument
no return type no argument

• Void main()
• {
• Void table() //function prototype
• Table(); // function call
• Getch();
• }
• Void table() // function definition
• {
• Int num,t,i;
• Cout<<“enter any no”;
• Cin>>num;
• For(i=1; i<=10; i++)
• {
• T=num*I
• Cout<<t;
• }
• }
no return type with argument

• Main()
• {
• Int n;
• Cout<<“enter any no to print a table”;
• Cin>>n;
• Void tab(int); // fun prototype/declaration , it may be inside or outside the main fun.
• Tab(n); //fun call, actual parameter
• Getch();
• }
• Void tab(int x) // function definition, formal parameter
• {
• Int i,t;
• For (i=1; i<=10;i++)
• {
• T=i*x;
• Cout<<x<<“*”<<i<<“=“<<t<< endl;
• ]
• }
exercise
• WAP to make a function to find greater of two
no.
with return type with argument

• Void main()
• {
• Float avg(float,float,float) // fun prototype
• Float a,b,c,av;
• Cout<<“enter marks of three subjects”;
• Cin>>a>>b>>c;
• Av=avg(a,b,c) // fun call
• Cout<<“average=“<<av;
• getch();
• }
• Float avg(float x,float y,float z)
• {
• Float average;
• Average=x+y+z/3;
• Return average
• }

You might also like