#include<conio.h> void printline(char ch) { int i; cout<<“\n”;
for (i=1; i<=52; i++) Formal Arguments cout<< ch;
cout<<“\n”;
Note: When the UDFs are defined before the main ( ) function, they need not be declared in the main ( ) function.
The actual and formal arguments should match in number, data type and order. The values of AA are assigned to FA on a one-to-one basis.
If the AA are more than FA, the extra AA are ignored. However if the FA are more than AA, the unmatched FA are initialized to junk values. Any mismatch in data type also results in passing of junk values.
The FA should be valid variable names, where as AA could be variable names or constants. The variables used in AA must be assigned values before calling the function. When the function is called only a copy of the values of variables of AA is passed to the called function. Any manipulation within the function has no effect on the variables used in AA.
}void value(float p, float r, int n) { int year =1; flaot sum = p; while (year <=n) { sum *= (1 + (r/100)); year++; } cout<<“\nPrincipal amount :”<<p; cout<<“\nRate of Interest : ”<< r; cout<<“\nPeriod : ”<<n; cout<<“\nSum = ”<<sum; } void main() {
float principal, inrate; int period;
clrscr ( );
cout<<“Enter principal amount, interest rate, and period\n”; cin>> principal>> inrate>> period;
printline(‘-‘);
value(principal, inrate, period); Actual Arguements printline(‘=’);
getch(); }
● Functions with arguments and return value
52.User defined function with arguments and return values
#include<iostream.h> #include<conio.h> void main()
{
void printline(char, int); Data type of arguments float value(float, float, int), principal, inrate, amount; int period; Data type of return value
clrscr ( );
cout<<“Enter the principal amount, interest rate and period\t”; cin>> principal>> inrate>> period;
printline(‘-‘,52);
amount = value(principal, inrate, period); Variable to store the return value cout<<“\nPrincipal amount : ”<<principal;
cout<<“\nRate of Interest : ”<< inrate; cout<<“\nPeriod : ”<<period;
cout<<“\nSum =”<<amount; printline(‘=’,52);
getch(); }
void printline(char ch ,int len) {
int i;
cout<<“\n”; for (i=1; i<=len; i++)
cout<< ch; cout<<“\n”; }
float value(float p, float r, int n) {
int year =1; float sum = p; while (year <= n) {
sum *= (1 + (r/100)); year ++;
}
return (sum); }
● Functions with no arguments but returns a value: Example given later
Nested Functions: C permits nesting of functions where main ( ) calls function1, which
calls function2, which calls function3 and so on. There is no limit as to how deeply functions can be nested.
53.Accept three values a, b and c and display the ratio a/(b-c) using nested functions
#include<iostream.h> #include<conio.h>
int difference (int p, int q) { if(p!=q) return (1); else return (0); }
float ratio (int x, int y, int z) { if (difference (y,z)) return (x/(y-z)); else return (0); } void main ( ) { int a,b,c; clrscr ( );
cout<<“Enter values for a, b and c:”; cin>>a>>b>>c;
cout<<“\nRatio = ”<<ratio(a,b,c); getch ( );
}
Note: main ( ) calls ratio ( ), which calls difference ( ). difference ( ) will return the value to ratio ( ) and ratio will return the value to main ( ). if (difference (y,z)) means if difference ( ) returns a non-zero value.
Recursion: When a called function in turn calls another function, a process of nesting
occurs. Recursion is a special case of this process where a called function calls itself. When we write a recursive function, there must be at least one if ( ) statement to force the function to return without the recursive call being executed.
54.Accept a number and display its factorial using recursive function
#include<iostream.h> #include<conio.h> long int factorial (int n) {
long int fact; if(n==1) return(1); else if(n<1) return(0); else fact = n * factorial (n-1); return(fact); } void main() { int num; long int f; clrscr ( ); cout<<“Enter a number\t”; cin>>num; f =fact(num); cout<<“\nFactorial of = ”<< num<< f; getch(); } Note: main ( ) f=factorial (3) factorial (3) fact = 3 * factorial (3-1) factorial (2) return (fact) = 3* 2* 1 fact = 2 * factorial (2-1)
return (1) return (fact) = 2* 1
factorial (1)
55.Assignment-Accept 5 nos from the user, store them in an array and display the largest number using UDF
#include<iostream.h> #include<conio.h>
int largest(int a[]) When an array is formal argument, the subscript is not
specified.
{
int i;
int max = a[0]; for (i=1; i<5; i++);
if (max < a[i]) max = a[i]; return(max); } void main() { int num[5], ctr; clrscr ( ); cout<<“Enter 5 numbers\n”;
While passing an array as an actual argument, only the array name is used without any brackets.
for (ctr=0; ctr<5; ctr++) cin>>num[ctr]; cout<<“\nMaximum value = ”<< largest(num); getch();
}
SESSION-12
Storage Class for Variables
i. Automatic Variables: They are declared in a function or block in which they are to
be utilized. They are created when the function or block starts executing and automatically destroyed when the function or block ends. Automatic variables are also referred to as local or internal variables. A variable declared without any storage class specification is by default an automatic variable. Automatic variables may also be explicitly declared using the keyword auto.
48.
56.Automatic Variables in Blocks
#include<iostream.h> #include<conio.h> void main()
auto int n =5; clrscr ( ); { auto int n =2; { auto int n = 1; cout<<“\n”<<n; }
Note:The storage class declaration auto is optional.
cout<<“\n”<<n; } cout<<“\n”<<n;
getch(); }
57.Automatic Variables (Using user defined functions)
#include<iostream.h> #include<conio.h> void function1() { int m =10; cout<<“\n’<<m; } void function2() { int m=100; function1(); cout<<“\n”<<m; } void main() { int m=50; clrscr ( ); function2(); cout<<“\n”<<m; getch(); }
ii. Global Variable: Variables that are available throughout the program are known as
global variables. They are also known as External variables. Unlike local variables, global variables can be accessed by any function in the program. In case if a local and global variable have the same name, the local variable will have precedence over the global variable in the function where it is declared.
Once a global variable has been declared, any function can access it and change its value. The subsequent functions can access only the changed value. A global variable is available from the point of declaration to the end of the program.
To access a global variable declared later in the program, it can be declared with the storage class extern in the current function.
58.Global Variable (Also an example of UDF without arguments and with return value) #include<iostream.h> #include<conio.h> int f1( ) { extern int x;
x+=10; Changing value of global v. retrun(x);
}
int x; Declaring global variable
int f2( ) {
int x=1; Local variable return(x);
}
Referring to global v. declared later in the program
int f3( ) {
x+=10; Changing value of global v. return(x);
} main( ) {
clrscr ( );
x=10; Initializing global variable cout<<“\n x = ”<<x; cout<<“\n x = ”<<f1(); cout<<“\n x = ”<<f2(); cout<<“\n x = ”<<f3(); getch(); }
iii. Static Variables: Unlike automatic variables, which retain their value till the end of
the block or function, static variables retain their value till the end of the program.
59.Static Variables
{ static int x =0; x++; cout<<“\n x = ”<<x; } void main() { int i; for (i=1;i<=5;i++) stat(); }
Note: A static variable is initialized only once and the subsequent function calls can access the value of the previous execution.
If the declaration static is removed, x will be initialized 5 times.
SESSION-13