FUNCTIONS
SYNTAX:
Returntype function name(parameters)
Statement(s);
Return type: data type of value returned by the function
Function name: name of function
Parameters: values given to function when a function is called.
e.g.
1. int add(int a, int b)
2. void line(char c)
3. int subtract(int a, float b, int c)
Example:
Write a program that shows “Programming is easy” on screen using functions.
#include <iostream>
using namespace std;
void show()
cout <<"programming is easy”;
int main()
show();
return 0;
}
Parameter Passing
Parameters are the values that are provided to a function when the function is called. Parameters
are given in the parentheses. If there are many parameters, these are separated by commas. If
there is no parameter, empty parentheses are used. Both variables and constants can be passed to
a function as parameters.
The sequence and types of parameters in function call must be similar to the sequence and types
of parameters in function declaration.
Parameters in function call are called actual parameters.
Parameters in function declaration are called formal parameters.
When a function call is executed, the values of actual parameters are copied to formal parameters.
9.4.1 Pass by Value
A parameter passing mechanism in which the value of actual parameter is copied to formal
parameters of called function is known as pass by value. If the function makes any change in
formal parameter, it does not affect the values of actual parameter. It is the default mechanism for
passing parameters to functions.
#include <iostream>
using namespace std;
void show(int num)
cout << "The number is " << num;
int main()
int n;
cout << "Enter number: ";
cin >> n;
show(n);
cout << "End of Program";
return 0;