FUNCTIONS
=> is a group of statements that together perform a task.
Syntax:
type function_name (parameter list)
* DEFINING and CALLING A FUNCTION *
Implementing any function in addition to main involves two steps:
1. Defining the Function
2. Calling the Function
take this a look:
#include<iostream>
using namaspace std;
int main()
{
printMessage();
return 0;
}
void printMessage(void)
{
cout<<“HELLO WORLD!”;
}
|
sample program :
create a program with two subfunctions that performs addition and subtraction.
#include<iostream>
using namaspace std;
int main()
{
int num1,num2,sum,diff;
cout<<“Enter first number: \n”;
cin>>num1;
cout<<“Enter second number: \n”;
cin>>num2;
addnum(num1,num2,sum);
subnum(num1,num2,diff);
cout<<sum;
cout<<diff;
retur 0;
}
void addnum(int x,int y,int &z)
{
z=x+y;
}
void subnum(int a,int b,int &c)
{
c=a-b;
}
|
Recent Comments