Notes of Important programming Fundamental Questions | Arrays and Functions

Functions

What is a function?

A function is a named block of code that performs some action. The statement written in a function are executed when is called by the name. Each function has a unique name. Functions are the building blocks of C++ programs. Functions are easier to write, modify and maintain. Functions can be reused and reduces programming time.

What is function header?

The first line of function definition is known as function header. The function header consists of return type, function name and parameter.

What is function body?

The set of statements which are executed inside the function is known as function body. The body of function appears after function header. The statements are written in curly braces {}. The variable declaration and program logic are implemented in function body.

What is function declaration or function prototype?

Function declaration is a model of a function. It is also known function prototype. It provides information to compiler about the structure of the function to be used in program. It consists of function name, function return type and number and type of parameters. It is terminated with semicolon.

What is a function called?

The statements that activate s a function is known as function call. A function is called its name. Function name is followed by necessary parameters in parenthesess. If there are many parameters, these are separated by commas.

What is local variable?

A variable declared inside a function is known as local variable. Local variable can be used only in the function in which it is declared. The lifetime of local variable starts when control enters the function in which it is declared. Local variable is automatically destroyed when control exist function.

What is global variable?

A variable declared outside any function is known as global variable. Global variable can be used by all functions in the program. Global variable exist in the memory as long as the program is running. These variables are destroyed from the memory when the program terminates.

What is scope of local variable?

The area where a variable can be accessed is known as scope of variable. Local variable can be used only in the function in which it is declared. If a statement accesses a local variable that is not in scope, the compiler generates a syntax error.

What is scope of global variable?

Global variable can be used by all functions in the program. It means that these variables are globally accessed from any part of the program. Normally, global variables are declared before main function.

What is lifetime of local variable?

The time period for which a variable exist in the memory is known as the lifetime of variable. Different types of variable have different lifetime. The lifetime of local variable starts when the control enters the function in which it is declared. Local variable is automatically destroyed when the control exists from the function and its lifetime ends. When the lifetime of a local variable ends, the value stored in this variable also becomes inaccessible.

What is lifetime of global variable?

Global variables exist in the memory as long as the program is running. These variables are destroyed from the memory when the program terminates. These variables occupy memory longer than local variables. So, global variables should be used only when these are very necessary.

Arrays

Define an array?

An array is a group of consecutive memory locations with same name and type. All these Memory locations have one collective name and type. The memory locations in the array are known as elements of array. The total number of elements in the array is called its length.

How an element is accessed in array?

Each element in the array is accessed with reference to its position of location in the array. The position is called index or subscript. The index of first element is 0 and index of last element is length -1. The value of the index is written in brackets along with the name of array.

How subscript variables are written?

A subscript variable is written in brackets [] with array name.

Distinguish between 1 -D array and 2 -D array?

1-D array consists of only single row or single column. Each element of this array can be accessed using one index value. 2-D array consists of multiple row and columns. Each element of this array is accessed using two index value.

Explain array manipulation?

The process of performing different operations on array is called array manipulation. Different operations can be performed using array such as finding an element, comparing two arrays, finding the minimum or maximum value in array and sorting an array etc.

Share To:

Adeel Rasheed

Full Stack Website Developer

Post A Comment:

0 comments so far,add yours