GO TO INDEX
Computer Science For Class X
Unit 04: Control Structure
Question Answers
By Mrs. Ayesha Arif
Vice Principal
(Jauhar Progressive School)
DETAILED NOTES
Q.No.1: Define functions and its categories or types.Ans: FUNCTIONS:
A set of statements written to perform a specific task and having a unique name is called a function.
Sub-Programs:
In structured programming, the complicated and large program coding is broken down into smaller modules which are called subprograms. In C++, subprograms are called functions.
Every program has at least one main( ) function in C++. When the program starts, the main( ) function is called for execution.
CATEGORIES OR TYPES OF FUNCTIONS:
There are two categories or types of functions:
- Pre-defined Functions
- User-defined Functions
1. Pre-define Functions:
- The pre-define functions are the part of every high-level programming language.
- These functions are already defined in the system libraries. Therefore Predefined functions are also known as System-defined or library functions.
- It can be used for different purposes.
- These functions do not need to be declared and defined.
- Pre-define functions are declared in header file.
- All predefined functions can be used simply by calling the function like sqrt(), strcpy(), touppero, pow() etc.
- Many pre-define functions need proper header file by using #include pre-processor directive.
- The definitions of many common functions are found in the cmath and cstdlib libraries.
2. User-defined-function:
- Programmer can also write their own functions to perform specific task. They are called user-define-functions.
- These functions need declaration and definition.
- When user defined function is called from any part of of the program , it will execute the code defined inside the body of function.
- multiply(), sum(), average() etc are examples of User-define functions.
-
User defined function is based on two parts:
(i) Function declaration or prototype
(ii) Function definition
(i) Function Declaration:
- A function without its definition (code block) is known as function declaration or function prototype.
- It is declared before the main ( ) function.
-
It tells the compiler about the function’s name, return data types, and arguments/parameter data types and it ends with statement terminator (;).
where
int = return data type
my fun = function name
float a, float b, char c = parameters
; = statement terminators- Basic Syntax:
i) Return Data Type:
It shows the data type of value returned by function.
It may be int, float, double and char data type.
If no value is returned by the function in that case keyword “void” is used.
ii) Function Name:
It specifies the name of function.
It is recommended that meaningful and understandable names are given to the function.
It should not be a reserved word.
iii) Parameters:
It define the list of data types of function parameters that are to be passed to the function. or Information can be passed as parameters.
Parameters are separated by commas.
Parameters are also known as arguments.
If there is no parameter in function, programmer uses keyword "void".
They act as variables inside the function.
Variable names are optional in prototype parameters/ arguments.
iv) Statement Terminator:
In function declaration, statement terminator are used to end the statement.
(ii) Function Definition:
- Function definition is function itself. It provides the actual body of the function.
- A function must be defined before it is used anywhere in the program.
- It has a function header and a body or code block.
- Header has three parts:
- a) return value data type
- b) function name
- c) list of arugments with data types in parenthesis.
- Body of the function includes statements in braces.
- Function definition may be defined before or after the main function.
-
Q.No.2: What is function call?
Ans: FUNCTION CALL:
Functions are written by the programmer to define specific tasks. To use the function code the function has to be called or invoked with its name. These functions are called function call.
- When function is called for execution, control will transfer to the function definition and all statements of function definition will execute and after executing the statements the control will transfer back to the calling function (statement after function call).
-
If the function is without return value and has no arguments then it is called by its name. In this case the function braces will remain empty.
Syntax: function_name ( );
Example: add( ); - If the function returns a value, then we can store return value to a variable in calling function.
Example: x=add (y, z);
Q.No.3: What do you understand by function passing argument or parameters?
Ans: FUNCTION PASSING ARGUMENT OR PARAMETERS:
Passing information from calling function to the called function is known as passing argument or parameters. Or An argument or parameter is a part of data passed to the function.
By using passing argument, we can share information from one scope to another in C++ programming language.ACTUAL PARAMETERS:
When function is called for execution, the actual values which act as parameters are also given with function call statement. Passing actual values to function as arguments with function call statement are known as actual parameters.
- Actual parameters may be variables or constants.
- They are placed in parenthesis after the function name.
- These values are received in variables of the header of function definition.
- These receiving variables are called the formal parameters of the function.
- It acts as a local variable inside the function in which they are used.
Q.No.4: What is returning value from function?
Ans: RETURNING VALUE FROM FUNCTION:
- With value-returning functions, the actions result in the return of a value, and that value can be used in an expression.
- In C++, the return keyword allows a function to return a value.
- When a function completes its execution, it can return a single value to calling function.
- Return data type must be specified with the function header in the function definition as well as function declaration.
- It is written before function name.
- Syntax: int function name( );
Q.No.5: Differentiate between function definition and function call?
Ans: DIFFERENCE BETWEEN FUNCTION DEFINITION AND FUNCTION CALL:
S.NO. | FUNCTION DEFINITION | FUNCTION CALL |
---|---|---|
1. | A function definition provides the actual body of the function. Or The function definition is function itself. | Function call is to invoke the code of function by its name. |
2. | A function must be defined before it is used anywhere in the program. | The calling function asks the called function to perform a task. |
3. | It has a function header and a body or code block. Body of the function includes statements in braces. | If the function is without return value and has no arguments then it is called by its name . In this case the function braces will remain empty. |
4. | Function definition may appear before or after the main() function. | When the task is done the called function reports back to the calling function. |
5. | Syntax: data_ type function_ name (parameters list) { statements; } Example: int sum(int p, int q) {int z; z = p + q; return z; } | Syntax: variable _name = Function _ name (parameter list); Example: a= sum (x,y) |
Q.No.6: What are the different ways of using the user define functions?
Ans: Different Ways To Use User Define function (Based On argument / parameters and Retuen type):
There are four different methods to use user-define functions in C++, based on passing parameters to the function and return values from the functions. They are as follows:
-
No return value and No passing arguments/parameters.void function name(void);
- Return value but No passing arguments/parametersint/float/char function name(void);
- No return value but Passing arguments/parametersvoid function name(int, float, char);
- Return value and Passing arguments/parametersint/float/char function name (int, float, char);
Q.No.7: Differentiate between pre- defined and user- define functions.
Ans: Difference Between Pre-defined And User-Define Functions
S.NO. | User-defined Functions | Pre -define Functions |
---|---|---|
1. | These functions are not predefined in the Compiler. | These functions are predefined in the compiler of C language. |
2. | These function are created by user as per their own requirement. | These functions are not created by user as their own. |
3. | User-defined functions are not stored in library file. | Library Functions are stored in special library file. |
4. | Their declaration and definition are needed in program. | No need for function definition as that is part of C++ |
5. | Execution of the program begins from the user-define function. | Execution of the program does not begin from the library function. |
6. | Example: sum(), fact(),…etc. | Example: gets(), putchar(), getch(),…etc. |
Q.No.8: Explain the types of variables used in structured programming.
Ans: Types Of Variable Used In Structured Programming:
In structured programming, generally two types of variables are used.
- Local Variables
- Global Variables
1. Local Variables:
They are declared inside any function. A local variable is only accessible within a specific part of a program.
2. Global Variable:
Global variables are declared outside of the main function. It is also known as external variables. It holds the value of variable during the entire execution of the program. The value of global variables can be shared with different functions.
Here,
- ‘a’ is global variable and
- ‘x’ and ‘b’ are local variables.
- Receiving arguments are also local variable as 'c' variable in this program.
B. RESPOND THE FOLLOWING:
1. Differentiate between function definition and function declaration?
Ans: Difference Between Function Definition and Function Declaration
S.NO. | FUNCTION DEFINITION | FUNCTION DECLARATION |
---|---|---|
1. | Function definition is a function itself. It has a function header, a body and a code block. | A function without its definition (code block) is known as function declaration or function prototype. |
2. | It may be defined before or after the function. | It is declared before the main() function. |
3. | Header has three parts: return value data type, function name and list of arguments with data types in parenthesis. | It tells the compiler about the function's name, return data types and arguments /parameters data types |
4. | No statement terminators ( ); is required. | It ends with statement terminators ( ); |
5. | Determines the value stored in variable, function or class. | Specifies the name and type of variable, function, class, etc. |
6. | The function definition reserves some amount of memory | Function declaration doesn’t involve memory allocation. |
7. | Statements cannot be defined again if once it is already defined. | Re declaration can be easily possible. |
8. | Duration is determined. | Visibility is specified. |
2. What is the purpose of keyword void in function?
Ans: Purpose of "void":
In computer programming there are different uses of “void”. They are as follows:
- When void is used as a function return type, it indicates that the function does not return a value.
- When used in a function's parameter list, void indicates that the function takes no parameters.
- When void appears in a pointer declaration, it specifies that the pointer is universal.
Position of Void:
If that function will not return a value, such as when the purpose of a function is to display some outputs on the screen, then "void" is to be placed at the leftmost part of the function header.
When a return value is expected after the function execution, the data type of the return value is placed instead of "void".
3. Why do we use header file?
Ans: Purpose Or Use Of Header File:
When we want to use any function in our C++ program then first we need to import their definition from C++ library. For importing their declaration and definition we need to include header file in program by using #include. Header file include at the top of any C++ program. The primary purpose of a header file is to propagate declarations to code files. Header files allow us to put declarations in one location and then import them wherever we need them. This can save a lot of typing in multi-file programs.
4. Differentiate between passing argument and return the value from function.
Ans: Difference Between Passing Argument And Return The Value From Function:
S.NO. | PASSING ARGUMENT | RETURN VALUE OF FUNCTION |
---|---|---|
1. | Passing information from calling function to the called function is known as argument passing. | In C++, the return keyword allows a function to return a value. |
2. | By using argument passing, we can share information from one scope to another in C++ programming language. | With value-returning functions, the actions result in the return of a value, and that value can be used in an expression. |
3. | When function is called for execution, the actual values which act as parameters are also given with function call statement. They are called actual parameters. | When a function completes its execution, it can return a single value to calling function. |
4. | These values are received in variables of the header of function definition. | Return data type must be specified with the function header in the function definition as well as function declaration. |
5. | They are placed in parenthesis after the function name. | It is written before function name. |
5. What is the difference between external variables and function local variables?
Ans: Difference Between External Variables And Function Local Variables
S.NO. | Local Variables | Global or External Variables |
---|---|---|
1. | It is declared inside a function. | It is declared outside the function. |
2. | If it is not initialized, a garbage value is stored. | If it is not initialized zero is stored as default. |
3. | It is created when the function starts execution and lost when the functions terminate. | It is created before the program’s global execution starts and lost when the program terminates. |
4. | Data sharing is not possible as data of the local variable can be accessed by only one function. | Data sharing is possible as multiple functions can access the same global variable. |
5. | Parameters passing is required for local variables to access the value in other function. | Parameters passing is not necessary for a global variable as it is visible throughout the program. |
6. | When the value of the local variable is modified in one function, the changes are not visible in another function. | When the value of the global variable is modified in one function changes are visible in the rest of the program. |
7. | Local variables can be accessed with the help of statements, inside a function in which they are declared. | We can access global variables by any statement in the program. |
8. | It is stored on the stack unless specified. | It is stored on a fixed location decided by the compiler. |
6: List the five standard built-In functions with examples.
Ans: The C++ Standard Library provides a rich collection of functions for performing common mathematical calculations, string manipulations, character manipulations, input/output, error checking and many other useful operations. This makes the programmer's job easier, because these functions provide many of the capabilities programmers need. The C++ Standard Library functions are provided as part of the C++ programming environment.
C++ Standard Library | Explanation |
---|---|
<iostream> | Contains function prototypes for the C++ standard input and standard output functions. This header file replaces header file <iostream.h>. |
<iomanip> | Contains function prototypes for stream manipulators that format streams of data. This header file replaces header file <iomanip.h>. |
sin(x) | Sine of an angle x (measured in radians) |
cos(x) | Cosine of an angle x (measured in radians) |
isalpha(c) | It returns True if C is an uppercase letter and False if c is lowercase. |
isdigit(c) | It returns True if c is a digit (0 through 9) otherwise False. |
7. Write down the advantages of user define function in C++.
Ans: Advantages Of User Define Function In C++:
User defined functions in C programming has following advantages:
-
Reduction in Program Size:
Since any sequence of statements which are repeatedly used in a program can be combined together to form a user defined functions. And this functions can be called as many times as required. This avoids writing of same code again and again reducing program size. -
Reducing Complexity of Program:
Complex program can be decomposed into small sub-programs or user defined functions. -
Easy to Debug and Maintain:
During debugging it is very easy to locate and isolate faulty functions. It is also easy to maintain program that uses user defined functions. -
Readability of Program:
Since while using user defined function, a complex problem is divided in to different sub-programs with clear objective and interface which makes easy to understand the logic behind the program. -
Code Reusability:
Once user defined function is implemented it can be called or used as many times as required which reduces code repeatability and increases code reusability.
8. Get the output and highlight the errors from the following program.
Ans: Compilation Failed After Running Codes
After Correcting Error Compilation Result:
Hint:- (Line 11: Add using namespace std;)
- (Line 23: Replace "void" by "int")
Source: Special Thanks To Sir Syed Arif Ali
GOOD JOB
ReplyDeleteJAZAKALLAH
Delete