



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
The concepts of functions, arguments (actual and formal), local and global variables, and recursion in C++ programming. It includes examples and exercises to help understand these concepts.
What you will learn
Typology: Schemes and Mind Maps
1 / 6
This page cannot be seen from the preview
Don't miss anything!
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:
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
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 ==
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