Lecture 7
Lecture 7
LECTURE #7
FUNCTIONS
A function is a subprogram, or module to which any amount of
data can be sent, which return only one value.
Each function has its own name and when the name is
encountered, the function gets called i.e. the control is
transferred to the first statement in the called function.
1. Built-in functions
2. User-defined functions
Void funct(void);
A function that has no parameter can use the reserved
word void in its parameter list.
print();
getch();
}
Void print()
{
Cout<<“Well come to the functions \n”;
return ;
}
CALLING A FUNCTION
Executing the statements of a function to perform a
task is called calling of the function
Local Variables
Global variables
LOCAL VARIABLES
The variables that are declared inside the main function or
inside any user defined function are called local variables.
For example,
auto int a,b,c;
A variable remain available and can be used only during its lifetime.
The function that are declared outside the main function are
called global functions. They can be called by any function.
All local variables are automatic & all global variable are static.
Static numbers are special that are declared inside a function by using the keyword
“static”.
.
Static variables allows the last value of the variable to be
preserved, when its function ends. When the function is called
again, this preserved value can be used again.
The function accesses the value in the newly created variable and the data in
the original variable in the calling function is not changed.
The data can also be passed to a function by reference of a variable
name that contains data. The reference provides the second name for
a variable name. when a variable is passed by reference to a function.
Both variables use the same memory location. Thus any change in
the reference variable also change the value in the original variable.