COMPUTER SCIENCE 10TH – DETAILED QUESTION ANSWERS
Chapter # 05: FUNCTIONS
Q.1: What do you know about functions?
Ans:
INTRODUCTION TO FUNCTIONS
A function is a block of code that performs a particular task. It is also called a method or a sub-routine or a procedure, etc. There are some situations when we need to write a particular block of code more than once in our program. This may lead to bugs and irritation for the programmer. C++ language provides an approach in which you need to declare and define a group of statements once and that can be called and used whenever required. This saves both time and space. Every C++ program has at least one function, which is main()
, and all the programs can define additional functions.
Q.2: Write down the advantages of functions.
Ans:
ADVANTAGES OF FUNCTIONS
There are some advantages of using functions.
- The complexity of the entire program can be divided into simple subtasks, and function subprograms can be written for each subtask.
- Functions help us to avoid unnecessary repetition of codes. It helps in code reusability.
- Functions can have inputs and outputs and can process information.
- The functions are short, easier to write.
- The function are understandable and can be debugged.
Q.3: Define the types of functions.
Ans:
TYPES OF FUNCTIONS
There are two types of functions in C++ programming:
- Predefined / Built-in functions
- User-defined Functions
Q.4: What is predefined or built-in function?
Ans:
PREDEFINED/BUILT-IN FUNCTIONS
The built-in functions are standard library functions to handle tasks such as mathematical computations, I/O processing, string handling, etc. These functions are defined in the header file and don't need to declare and define. The definitions of most common functions are found in the cmath and cstdlib libraries.
Q.5: Describe user-defined functions.
Ans:
USER-DEFINED FUNCTIONS
C++ allows programmers to define functions to do a task relevant to their programs. Such functions created by the user are called user-defined functions. These functions need declaration and definition. A user can create as many user-defined functions as needed.
Q.6: Write down the major elements of user-defined functions.
Ans:
The user-defined function consists of two parts:
- Function declaration (Function prototype)
- Function definition
Q.7: Define function declaration.
Ans:
FUNCTION DECLARATION/PROTOTYPE
The declaration of a function is called its prototype. Using a function in programs requires that we have to declare the function first. It is declared before the main() function.
The general structure of the function prototype is:
return_datatype function_name(arguments)
Example:
- return type: Specifies the type of value the function will return.
- function name: The name of the function.
- parameters (arguments): The inputs to the function (if any).
- semicolon: The prototype ends with a semicolon because it is a statement.
It has four main components. These are:
- Return data type
- Name of the function
- Arguments (parameters)
- Statement terminator
RETURN DATA TYPE
The return data type of the function is the type of return value. If the function does not return a value, the type is defined as void.
FUNCTION NAME
The function name is any identifier followed by parentheses without any spaces in between.
ARGUMENTS/PARAMETERS
The arguments or parameters come inside the parentheses, preceded by their types and separated by commas. If the function does not use any arguments, the word void
is used inside the parentheses.
STATEMENT TERMINATOR
Semicolon (;) is used as a statement terminator at the end of function declaration or prototype.
FUNCTION DEFINITION
A function definition is the function itself. Function definition consists of a function header, a function body, and a code block. The definition begins with a header which is exactly the same as the function prototype except it must not be terminated with a semicolon (;). It has three main components: return type of the function, name of the function, and arguments/parameters. The body of the function includes statements enclosed in braces and is defined before or after the main function.
HEADERint heading(void)
- return type
- function name
- parameters (arguments)
- No semicolon (;) after header
BODY
Q.8: Define function calling.
Ans:
FUNCTION CALL
A user-defined function is called from the main program simply by using its name, including the parentheses which follow the name. The parentheses are necessary so that the compiler knows you are referring to a function.
GENERAL SYNTAX
Q.9: Define function arguments.
Ans:
FUNCTION PASSING ARGUMENTS OR PARAMETERS
Sending data to a function is called passing arguments. It is basically sending variables, constants, or expressions whose value are needed by the function. Actual values that are passed to the function as arguments with the function call statement are known as actual arguments. These values are received in variables of the header of the function definition. These receiving variables or arguments are called formal arguments.
Q.10: Define function return value.
Ans:
RETURNING VALUE FROM FUNCTIONS
In C++, when a function completes its execution, it can return a value to the calling function using the return
keyword. The return type must be defined with the function header in the declaration and definition.
Q.11: Write down the differences between function definition and function call.
Ans:
DIFFERENCES BETWEEN FUNCTION DEFINITION AND FUNCTION CALL
FUNCTION DEFINITION | FUNCTION CALL |
---|---|
The function definition is the part of the function where it is actually defined. | Invoke the code of the function; this is called a function call. |
A user-defined function may define before or after the main function. | A user-defined function is called from the main program simply by using its name. |
Syntax: data_type function_name(arguments) { statements; } | Syntax: variable_name = function_name(arguments); |
Example:
Example (Function Call):
Q.12: Define function types based on arguments and return value.
Ans:
DIFFERENT WAYS TO USE USER-DEFINED FUNCTION BASED ON ARGUMENTS AND RETURN TYPE
Functions can be used in four variations in C++ based on arguments and return value from the functions:
No return value and no passing arguments
Return value but no passing arguments
No return value but passing arguments
Return value and passing arguments
Q.13: Write a program using predefined functions.
Ans:
EXAMPLE OF PREDEFINED FUNCTIONS
Q.14: Write a program using user-defined functions.
Ans:
EXAMPLE OF USER-DEFINED FUNCTIONS