11th Computer Science 12th Computer Science BCA CBSE CBSE JAC JAC

Functions in C++ Programming Language

Functions in C++ Programming Language
Written by AIPS

The number of statements grouped in a logical units to perform some specific task , are called functions.

Function makes program easier to understand and maintain .

when a program is made using function that can be easily modified and error can be detected.

A large program is divided into several functions to handle in easy way.

There are two types of function.

  1. Library function / Built in function
  2. User defined function.

Library Function

The predefined function is called library function.

we can use library function using their respective header files.

If we use library functions and respective header file is not included then we get Prototype error.

we can’t understand the internal working of the library function.

example: sqrt( ), pow( ), strlen( ) , etc…

User defined function

The function defined by the user is called user defined function.

we can understand the structure of the user defined function.

we can modify user defined function when needed .

It can be used in four ways.

  1. No Argument, No Return value
  2. With Argument, But not Return value
  3. With Argument , with Return value
  4. No Argument, but return value

Parts of Functions:

There are following part of function.

  1. Function prototyping:- It determines the function name,number of arguments and their types, return type etc..
  2. Function call:- When we want to execute the statements of function then we need function calling. function is invoked or called with parameters or arguments.
  3. Function definition :- It contains the statement, which to be performed after calling.
  4. Actual argument:- Argument inside the function call is known as actual argument.
  5. Formal Argument:- Argument inside the function definition is called formal argument.

No Argument , No Return Value

example

void sum(); // prototyping part
void main()
{
......
......
.......
sum(); // calling part
getch();
}
void sum()
{
.....
......//Definitioin part

}

Q.Write a program to find sum of two numbers using No argument , No return value.(compile time)

#include<iostream.h>
#include<conio.h>
void sum();
void main()
{
clrscr();
sum();
getch();
}
void sum()
{
float a,b,c;
a=10;
b=20;
c=a+b;
cout<<”Sum=”<<c;
}

Output

Sum=30

Q.Write a program to find sum of two numbers using No argument , No return value.(Run time)

#include<iostream.h>
#include<conio.h>
void sum();
void main()
{
clrscr();
sum();
getch();
}
void sum()
{
float a,b,c;
cout<<”Enter value of a and b=”;
cin>>a>>b;
c=a+b;
cout<<”Sum=”<<c;
}

output

Enter value of a and b=10

20

sum=30

With Argument , But not Return Value

Example

void sum(float ,float); // prototyping part
void main()
{
float a,b;
a=10;
b=20;
......
.......
sum(a,b); // calling part, Actual argument
getch();
}
void sum(float x,float y)  // formal argument
{
float z;
z=x+y;
cout<<"sum="<<z;
......//Definitioin part

}

Q.Write a program to find sum of two numbers using with argument , but not return value.(compile time)

#include<iostream.h>
#include<conio.h>
void sum(float,float);
void main()
{
clrscr();
float a,b;
a=10;
b=20;
sum(a,b);
getch();
}
void sum(float x, float y)
{
float z;
z=x+y;
cout<<”Sum=”<<z;
}

Output

sum=30

Q.Write a program to find sum of two numbers using with argument , but not return value.(Run time)

#include<iostream.h>
#include<conio.h>
void sum(float,float);
void main()
{
clrscr();
float a,b;
cout<<”Enter value of a and b=”;
cin>>a>>b;
sum(a,b);
getch();
}
void sum(float x, float y)
{
float z;
z=x+y;
cout<<”Sum=”<<z;
}

Output

Enter value of a and b=10

20

sum=30

With Argument, With Return value

Example

float sum(float ,float); // prototyping part
void main()
{
float a,b,c;
a=10;
b=20;
......
.......
c=sum(a,b); // calling part, Actual argument
cout<<"Sum="<<c;
getch();
}
float sum(float x,float y)  // formal argument
{
float z;
z=x+y;
return z;
......//Definitioin part

}

Q.Write a program to find sum of two numbers using with argument , with return value.(compile time)

#include<iostream.h>
#include<conio.h>
float sum(float,float);
void main()
{
clrscr();
float a,b,c;
a=10;
b=20;
c=sum(a,b);
cout<<”Sum=”<<c;
getch();
}
float sum(float x, float y)
{
float z;
z=x+y;
return z;
}

Output

sum=30

Q.Write a program to find sum of two numbers using with argument , with return value.(Run time)

#include<iostream.h>
#include<conio.h>
float sum(float,float);
void main()
{
clrscr();
float a,b,c;
cout<<”Enter value of a and b=”;
cin>>a>>b;
c=sum(a,b);
cout<<”Sum=”<<c;
getch();
}
float sum(float x, float y)
{
float z;
z=x+y;
return z;
}

Output

Enter value of a and b=10

20

sum=30

to be continued…..check Regular for more…..update

About the author

AIPS

Leave a Comment