0% found this document useful (0 votes)
43 views6 pages

Actual and Formal Arguments

Uploaded by

desinanagarjuna3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views6 pages

Actual and Formal Arguments

Uploaded by

desinanagarjuna3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Actual and Formal Arguments:

The arguments may be classified under two groups, actual and formal arguments:

(a) Actual arguments:-


An actual argument is a variable or an expression contained in a functioncall that
replaces the formal parameter which is a part of the function declaration. Sometimes,
a function may be called by a portion of a program with some parameters and these
parameters are known as the actual arguments.
(b) Formal arguments:-
are the parameters present in a function definition which may also be called as
dummy arguments or the parametric variables. When the function is invoked, the
formal parameters are replaced by the actual parameters. Formal arguments may be
declared by the same name or by different names in calling a portion of the program
or in a called function but the data types should be the same in both blocks.

For example:

# include <iostream.h>
Void main()
{
Int x,y;
Void output (int x, int y); // function declaration
__
__
Output (x,y); // x and y are actual arguments
}
Void output (int a, int b) // forma or dummy arguments
{
// body of function
}

Local and Global variables:

The variables in general bay be classified as local or global variables.

(a) Local variables:-


Identifiers, variables and functions in a block are said to belong to a particular
block or function and these identifiers are known as the local parameters or
variables. Local variables are defined inside a function block or a compound
statement.
example

Void func (int I, int j)


{
Int k,m; // local variables
…… // bodyofthe function
}
Local variables are referred only the particular part of a block or a function. Same variable name
may be given to different parts of a function or a block and each variable will be treated as a
different entity.

(b) Global variables:-


these are variables defined outside the main function block. These variables are referred
by the same data type and by the same name through out the program in both the calling
portion of the program and in the function block.

Example :- A program to find the sum of the given two numbers using the global variables?

#include <iostream.h>
Int x;
Int y=5;
void main( )
{
X=10;
Void sum(void);
Sum();
}
Void sum(void)
{
Int sum;
Sum=x+y;
Cout<<”x=”<<x<<endl;
Cout<<”y=”<<y<<endl;
Cout<<”sum=”<<sum<<endl;
}
4. Recursive Functions:-

A function which calls itself directly or indirectly again and again is known as the
recursive function. Recursive functions are very useful while constructing the data
structures like linked lists, double linked lists, and trees. There is a distinct difference
between normal and recursive functions. A normal function will be invoked by the main
function whenever the function name is used, where as the recursive function will be
invoked by itself directly or indirectly as long as the given condition is satisfied.
Forexample,

# include <iostrem.h>
Void main(void)
{
Void func1(); //function declaration
_____
_____
Func1(); //function calling
}
Void func1() //function definition
{
____
____
Func1(); //function calls recursively
}
Example:- A program to find the sum of the given non negative integer numbers using a
recursive function ? sum=1+2+3+4+…+n

#include <iostream.h>
Void main(void)
{
Int sum(int);
Int n,temp;
Cout<<”Enter any integer number”<<endl;
Cin>>n;
Temp=sum(n);
Cout<<”value=”<<n<<”and its sum=”<<temp;
}
Int sum(int n) //recursive function
{
Int sum(int); //local function declaration
Int value=0;
If (n==0)
Return (value);
Else
Value=n+sum(n-1);
Return (value);
}
Example:- A program to find the factorial (n!) of the given number using the recursive
function.Its the product of all integers from 1 to n ?
(n is non negative) (so n!=1 if n=0 and n!=n(n-1) if n>0)
#include <iostream.h>
main()
{
int fact (int);
int x,n;
cout<<”Enter any integer number”<<endl;
cin>>n;
x=fact(n);
cout<<”value=”<<n<<”and its factorial=”;
cout<<x<<endl;
}
int fact (int n) //recursive function
{
int fact( int); //local function declaration
int value =1;
If (n==1)
Return(value)
Else
{
value=n*fact(n-1);
Return(value);
}
}
WORK SHEET of Functions

Q1: Write a C++ program, using function, to counts uppercase letter in a 20 letters
entered by the user in the main program?

Q2: Write a C++ program, using function, that reads two integers (feet and inches)
representing distance, then converts this distance to meter?
Note: 1 foot = 12 inch 1 inch = 2.54 Cm

Q3: Write a C++ program, using function, which reads an integer value (T) representing
time in seconds, and converts it to equivalent hours (hr), minutes (mn), and seconds
(sec), in the following form: hr : mn : sec?
i.e.: Input: 4000 Output: 1 : 6 : 40

Q4: Write a C++ program, using function, to see if a number is an integer (odd or even)
or not an integer?

Q5: Write a C++ program, using function, to represent the permutation of n?

Q6: Write a C++ program, using function, to inputs a student’s average and returns 4 if
student’s average is 90-100, 3 if the average is 80-89, 2 if the average is 70-79, 1 if
the average is 60-69, and 0 if the average is lower than 60?

Q7: The Fibonacci Series is: 0, 1, 1, 2, 3, 5, 8, 13, 21, … It begins with the terms 0 and
1and has the property that each succeeding term is the sum of the two preceding
terms. Write a C++ program, using function, to calculate the nth Fibonacci number?

Q8: Write a C++ program, using function, to calculate the factorial of an integer entered
by the user at the main program?

Q9: Write a C++ program, using function, to evaluate the following equation:

Q10: Write a C++ program, using function, to test the year if it’s a leap or not?
Note: use y % 4 == 0 && y % 100 != 0 :: y % 400 ==0

Q11: Write a C++ program, using function, to find xy ?


Note: use pow instruction with math.h library.

Q12: Write C++ program, using function, to inverse an integer number?


For example: 765432 234567
Q13: Write C++ program, using function, to find the summation of student’s marks, and
it’s average, assume the student have 8 marks?

Q14: Write C++ program, using function, to convert any char. From capital to small or
from small to capital?

Q15: Write C++ program using recursive function to find the power of n numbers?

You might also like