Docsity

Modular Programming and Functions in C++: Understanding the Basics, Study notes of C programming

Partial preview of the text

Download Modular Programming and Functions in C++: Understanding the Basics and more Study notes C programming in PDF only on Docsity!

Chapter 3

Functions

What is Modular Programming?

โ—

The process of breaking large program into

smaller sub-programs is called as modular

programming or modularization.

โ—

C++ use functions to implement modular

programming

What is a Function?

โ— Function is a named unit of statements in a program to perform a specific task as part of the solution. โ— There are two types of functions in C++: โ— a). Predefined functions or Built in Functions : Functions that are already written , compiled and their definitions are grouped and stored in header files. Eg. sqrt(), toupper() โ— b). User Defined Functions : Functions that are written by the user to carry on some task. Eg. main( )

Predefined functions or Built in

Functions

โ—

Predefined functions or Built in Functions are

classified into:

1)Console functions for character I/O

2)Stream functions for I/O operations

3)String functions

4)Mathematical functions

5)Character functions

What is Stream functions?

โ— Stream Functions is used to perform input/output operations on character and strings. โ— It allow a stream of bytes (data) to flow between memory and objects like Keyboard or Monitor. iostream header file required. i. get() : It can accept a single character or multiple characters (string) through the keyboard. Eg cin.get(str,10) ii.getline() : It accepts a string through the keyboard. Eg cin.getline(str,len); iii.put() : It is used to display a character constant or the content of a character variable given as argument. Eg. cout.put('B'); iv.write() : This function displays the string contained in the argument. Eg. cout.write(str,10);

What is String Function?

โ— (^) String functions allow manipulation of strings. โ— cstring header file required. i. strlen() : used to find the length of a string(Number of characters ). Its return value is an integer. ii.strcpy() : used to copy one string into another. The function will copy string2 to string1. Syntax : strcpy(string1, string2); iii.strcat() : used to append one string to another string. Syntax: strcat(string1, string2); iv.strcmp() : used to compare two strings. In this comparison, the alphabetical order of characters in the strings is considered. Syntax: strcmp(string1, string2); v.strcmpi() :used to compare two strings ignoring cases (both the upper case and lower case letters are treated as same). syntax is same as strcmp.

What is Character Function?

โ— (^) These functions are used to perform various operations on characters. โ— (^) These functions require the inclusion of header file cctype 1)isupper() : This function is used to check whether a character is in upper case(capital letter) or not. The function returns 1 if the given character is in uppercase, and 0 otherwise. 2)islower() : This function is used to check whether a character is in lower case (small letter) or not. The function returns 1 if the given character is lower case, and 0 otherwise. 3)isalpha() : This function is used to check whether the given character is an alphabet or not. The function returns 1 if the given character is an alphabet, and 0 otherwise. 4)isdigit() : This function is used to check whether the given character is a digit or not. The function Output: 1 returns 1 if the given character is a digit, and 0 otherwise. 5)isalnum() : This function is used to check whether a character is alphanumeric or not. The function returns 1 if the given character is alphanumeric, and 0 otherwise. 6)toupper() : This function is used to convert the given character into its uppercase. The function returns the upper case of the given character. If the given character is in upper case, the output will be the same. 7)tolower() : This function is used to convert the given character into its lower case. The function returns the lower case of the given character. If the given character is in lowercase, the output will be the same.

What is user defined function?

โ—

A user defined function is a group of code to

perform a specific task.

โ—

Syntax:

data_type function_name(argument_list)

statements ;

โ—

The result of a function is called return value.

โ—

A set of values passed to a function is called

arguments.

FUNCTION HEADER FUNCTION BODY

โ—

1) What are formal and actual parameters?

โ—

The arguments given at the calling of a function is

called actual (original) arguments or actual

parameters since they are the actual data passed to

the function for processing.

โ—

The arguments used in the function definition are

known as formal parameter.

โ—

2) What is default arguments?

โ—

Default arguments are arguments to which initial

values given at function definition. So a function can

be invoked without specifying all its arguments.

โ—

3) What is mean by scope or life time of a function/
variable?

โ—

Scope of a variable or function is that part of the program
in which it is used. The life of a variable, declared within a
function, ends with the execution of the last instruction of
the function because its memory location of all the
variables declared within it will be freed.

โ—

4) What is a local variable?

โ—

A local variable is one that declared within a function or a
block of statements. It is available only within that
function or block.

Previous Questions

โ— Q1) Name the mathematical function which returns the absolute value of an integer number โ— abs() โ— Q2) Explain any two built-in functions in C++ that are used for string manipulation โ— Q3) Define built-in functions. Give two examples? โ— Q4) The process of breaking large program into smaller sub-programs is called........ โ— Modularization โ— Q5) Define modular programming. โ— Q6) Explain the merits of modular programming

Previous Questions

โ— Q7) Identify the built in C++ functions for the following cases โ— i) to convert -25 to 25 โ— abs() โ— ii) compare โ€˜computerโ€™ and โ€œCOMPUTERโ€ ignoring cases โ— strcmpi() โ— iii) to check the given character is digit or not โ— isdigit() โ— iv) to convert the character โ€˜Bโ€™ to โ€˜bโ€™ โ— tolower() โ— v) to find square root of 64 or a number โ— sqrt()

โ—

Q12) Compare call-by-value and call-by-

reference methods for calling functions