0% found this document useful (0 votes)
6 views32 pages

Chapter One Overview of Programming

Chapter 1 provides an overview of programming concepts focusing on data structures and algorithms in C++. It covers essential topics such as functions, user-defined functions, arrays, pointers, and structures, explaining their definitions, usage, and advantages. The chapter also discusses function prototypes, recursion, and library functions, providing a comprehensive foundation for understanding programming in C++.

Uploaded by

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

Chapter One Overview of Programming

Chapter 1 provides an overview of programming concepts focusing on data structures and algorithms in C++. It covers essential topics such as functions, user-defined functions, arrays, pointers, and structures, explaining their definitions, usage, and advantages. The chapter also discusses function prototypes, recursion, and library functions, providing a comprehensive foundation for understanding programming in C++.

Uploaded by

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

Chapter 1: Review of

Programming Concepts

Data Structures and Algorithms


Compiled by:
Ataklti Nguse
Ataklti N. 05/16/2 1
025
Memorize questions?

Q1. Which one is the following valid initialization of


variables?

A. int x (20);
B. float x{10.8};
C. int c=d=s=f=50;
D. int a=(b=5,b+4);
Q2. what are Typedef and enum with examples?
Ataklti N. 05/16/2025 2
Q3. Which one is the following valid initialization of integer
literals ?
A. int a=10;
B. int b=012;
C. int c=0x12;
D. All of the above
Q4. What will be display from the above statements
E. cout<<c<<"\t"<<b<<'\t'<<a<<endl;
F. int f=9;
bool bf=true;
cout<<f%bf;
Ataklti N. 05/16/2025 3
Q3. float c=10e-3; is valid initialization for floating
number and what is the value of c?

Ataklti N. 05/16/2025 4
1.1. Functions
A function is nothing but a group of codes written together and given
a name. or
A function is a block of code that performs a specific task.
And these can be called anytime in the main function without typing
the whole code.
Why Functions?
Functions make the code reusable.
Declare them once and use them multiple times.
Functions make the program easier as each small task is divided into
a function.
Functions
Ataklti N.
increase readability. 05/16/2025 5
Continued…

• Some functions return a numeric value to the calling function.


• These are called return type functions.
• If a function does not return a numeric value, we call it void function.
• A programmer should understand how to declare a function, define a
function and call a function.
• The functions which are declared and defined by a programmer are
called user defined functions or programmer defined functions.
• Besides, C++ Standard Library has a large collection of predefined
functions which can also be used by a programmer.
05/16/2025 Ataklti N. 6
1.2 User Defined Functions
• C++ allows the programmer to define their own function.

• A user-defined function groups code to perform a specific task and that


group of code is given a name (identifier).

• When the function is invoked from any part of the program, it all executes
the codes defined in the body of the function.

• The Component of User defined Functions are:-

• Function declaration

• The definition of a function


Ataklti N. 05/16/2025 7

• Calling functions
1.2.1 Function Prototype
• A function prototype comprises type and name of the function along with
list of types of its parameters enclosed in brackets.
• It ends with a semicolon
• Syntax: type identifier (type-of- parameter1, type-of-parameter2, …
type-of- parameter n );
• The prototype conveys the following information to the compiler.
(i) The type of function, i.e. the type of return value.
(ii) Number of parameters of the function.
(iii) Type of each parameter.
(iv) The order in which the arguments would be placed when the
05/16/2025 Ataklti N. 8
Continued…

• The names of parameters may or may not be given in the prototype,


however the names are necessary in the head of function in the function
definition.
• For calling the function in the main() or in any other function you need
to mention the name of function followed by arguments (values of
parameters) in parentheses and end the line by a semicolon.
• If this rule is not followed the program will give erroneous results.
• A function returns only one value.
• a function is not defined in-side the main() function.
• Functions maybe with empty parameter list.
05/16/2025 Ataklti N. 9
1.2.2 C++ User-defined Function Types

• For better understanding of arguments and return in functions,


user-defined functions can be categorised as:

• Function with no parameters and no return value


• Function with no parameters but return value
• Function with parameters but no return value
• Function with parameters and return value

Ataklti N. 05/16/2025 10
1.2.3 Function Arguments
Sr.
Call Type & Description
No
Call by Value. This method copies the actual value of an argument
into the formal parameter of the function.
1 In this case, changes made to the parameter inside the function have
no effect on the argument.
Call by Pointer. This method copies the address of an argument into
the formal parameter.
2 Inside the function, the address is used to access the actual argument
used in the call.
This means that changes made to the parameter affect the argument.

Call by Reference. This method copies the reference of an argument


into the formal parameter.
3 Inside the function, the reference is used to access the actual
argument used in the call.
Ataklti N. 05/16/2025 11
This means that changes made to the parameter affect the argument.
1.2.4 Scope of Variables in C++
• Variable Scope is a region in a program where a variable is
declared and used.

• An entity declared outside any block has global scope.


• Inside a function or a block which is called local variables,
• In the definition of function parameters which is called formal
parameters.
• The formal parameters behave like other local variables inside
the function
Ataklti N. 05/16/2025 12
1.2.5 C++ Function Overloading
• in C++, two different functions can have the same name if
their parameters are different;

• either because they have a different number of parameters,


or because any of their parameters are of a different type.

• These functions having the same name but different


arguments are called overloaded functions.

Ataklti N. 05/16/2025 13
1.2.6 C++ Programming Default Arguments
(Parameters)
• In C++ programming, we can provide default values for function
parameters.
• In C++, functions can also have optional parameters, for which no
arguments are required in the call, in such a way that,
• for example, a function with three parameters may be called with
only two.
• For this, the function shall include a default value for its last
parameter, which is used by the function when called with fewer
arguments.
• If a function with default arguments is called without passing
arguments, then the default parameters are used.

Ataklti N. 05/16/2025 14
1.2.7 C++ Recursion
• Recursively is the property that functions have to be called by
themselves.

• It is useful for some tasks, such as sorting elements, or


calculating the factorial of numbers.

Ataklti N. 05/16/2025 15
Advantages of C++ Recursion
• It makes the code shorter and cleaner.
• Recursion is required in problems concerning data structures and
advanced algorithms, such as sorting, Graph and Tree
Traversal.

• Disadvantages of C++ Recursion


• It takes a lot of stack space compared to an iterative program.
• It can be more difficult to debug compared to an equivalent
iterative
Ataklti N.
program. 05/16/2025 16
1.2.8 C++ Library Functions
 Library functions are the built-in functions in C++ programming.
 Programmers can use library functions by invoking the functions
directly; they don't need to write the functions themselves.
 Some common library functions in C++ are sqrt(), abs(),
isdigit(),rand() etc.
 In order to use library functions, we usually need to include the
header file in which these library functions are defined.
 For instance, in order to use mathematical functions such as sqrt() and
abs(), we need to include the header file math.h.
Ataklti N. 05/16/2025 17
1.3 Arrays In C++
 An array can be defined as a collection of variables of the same data
type and has contiguous memory locations.
 In C++, an array is a variable that can store multiple values of the
same type.
 Array declaration in C++:
 To declare an array in C++, the programmer specifies the type of the
elements and the number of elements required by an array.
• There are various ways in which we can declare an array.
• It can be done by specifying its type and size, by initializing it or
both.
• Array declaration by specifying size
typeAtaklti
arr[arraysize];
N. 05/16/2025 18

int arraysize = 10;


Advantages of an Array in C++:
1.Random access of elements using array index.
2.Use of fewer line of code as it creates a single array of multiple elements.
3.Easy access to all the elements.
4.Traversal through the array becomes easy using a single loop.
5.Sorting becomes easy as it can be accomplished by writing fewer line of
code.
• Disadvantages of an Array in C/C++:
1.Allows a fixed number of elements to be entered which is decided at the
time of declaration.
-Unlike a linked list.
2. Insertion and deletion of elements can be costly since the elements are
Ataklti N. 05/16/2025 19
needed to be managed in accordance with the new memory allocation.
C++ Arrays
• Access Elements in C++ Array
• In C++, each element in an array is associated with a number.
• The number is known as an array index.
• We can access elements of an array by using those indices.

Ataklti N. 05/16/2025 20
C++ Array Initialization

• In C++, it's possible to initialize an array during declaration.


• C++ Multidimensional Arrays
• In C++, we can create an array of an array, known as a
multidimensional array.

• Note: It is not mandatory to specify the number of rows in the


array.

• However, the number of columns should always be specified.


Ataklti N. 05/16/2025 21
Q6. What is the output to following fragment of user defined
function C++ program.

Ataklti N. 05/16/2025 22
Passing Array to a Function in C++ Programming

• In C++, we can pass arrays as an argument to a function.


• And, also we can return arrays from a function.

Ataklti N. 05/16/2025 23
C++ Strings
• String is a collection of characters.
• String is nothing but a sequence of character in which the last
character is the null character ‘\0’.

• Built-in function to Strings Manipulation


• Copying string:-Strcpy()
• Concatenating strings:-strcat()
• Comparing strings:-strcmp()
• Length of String:-strlen()
Ataklti N. 05/16/2025 24
1.4 Pointer in C++

 Pointer is a variable which holds (or whose value is) the


memory address of another variable.

 The address of a variable is the address (byte number) of the


first byte of the memory block allocated to the variable. Thus,
the variable gets an address where its value is stored.
Syntax:

• data_type variable_name; // declares a pointer to


data_type

• data_type *pointer_name = &variable_name;


Ataklti N. 05/16/2025 25
Applications of Pointers

• To pass arguments by reference


• For accessing array elements
• To create Dynamic memory allocation
• To implement data structures such as Linked list, stack,
queue..
• To obtain memory from the system
• To access value of variable indirectly
• To save space of computers memory
NULL pointer and Void Pointer

• Null Pointer: is a pointer constant that points to nothing.


Any pointer can be made null pointer by initializing with
keyword NULL.

But a null pointer should not be dereferenced.


• Example:
• float * ptr; = NULL;

• cout << *ptr; // gives a run-time error message.


Continued…
 Do not confuse null pointers with void pointers.
 Void pointer means points to any type of pointer assigned to a
variable.

Ataklti N. 05/16/2025 28
1.5 Structure in C++
• structure is another user defined data type which allows you to
combine data items of different kinds.
• Structures are used to represent a record
• A Structure is a collection of simple variables.
• The Variables in a structure can be of different types.
• Some can be int, some can be float, and so on.
• The data items in a structure are called the members of the
structure.
• Declaration of structure
• To define a structure, you must use the struct statement.
• The struct statement defines a new data type, with more than one
Ataklti N. 05/16/2025 29
member
Continued…
• struct [structure tag]
• {
member definition;
member definition;
...
member definition;
} [one or more structure variables];

Ataklti N. 05/16/2025 30
Initialization of structure and
Accessing of structure
• To access any member of a structure, we use the member
access operator (.).

• The member access operator is coded as a period between


the structure variable name and the structure member
that we wish to access.

Ataklti N. 05/16/2025 31
Pointers and structures
• To access the members of a structure using a
pointer to that structure, you must use the
(Arrow)-> operator as follows:

Ataklti N. 05/16/2025 32

You might also like