Plus Two Computer Application Notes Chapter 3 Functions

Kerala Plus Two Computer Application Notes Chapter 3 Functions

String handling using arrays: A string is a combination of characters hence char data type is used to store string. A string should be enclosed in double quotes. In C++ a variable is to be declared before it is used.Eg. “BVM HSS KALPARAMBU”.

Memory allocation for strings: To store “BVM” an array of char type is used. We have to specify the size. Remember each and every string is end with a null (\0) character. So we can store only size-1 characters in a variable. Please note that \0 is treated as a single character. \0 is also called as the delimiter, char school_name[4]; By this we can store a maximum of three characters.

Plus Two Computer Application Notes Chapter 3 Functions 1

Consider the following declarations
char my_name[10]=”Andrea”;
char my_name2[]=”Andrea”;
char str[ ]=”Hello World”

In the first declaration 10 Bytes will be allocated but it will use only 6+1 (one for ‘\0’) = 7 Bytes the remaining 3 Bytes will be unused. But in the second declaration the size of the array is not mentioned so only 7 Bytes will be allocated and used hence no wastage of memory. Similarly in the third declaration the size of the array is also not mentioned so only 12( one Byte for space and one Byte for‘\0’) Bytes will be allocated and used hence no wastage of memory.

Input / output operations on strings

Consider the following code

# include<iostream>
using namespace std;
int main()
{
charname[20];
cout<<“Enter your name:”; cin>>name;
cout<<“Hello “<<name;
}

If you run the program you will get the prompt as follows:
Enter your name: Alvis Emerin
The output will be displayed as follows and the “Emerin” will be truncated.
Hello Alvis

This is because of cin statement that will take upto space. Here space is the delimiter. To resolve this gets() function can be used. To use gets() and puts() function the header file stdio.h must be included. gets() function is used to get a string from the keyboard including spaces.
puts() function is used to print a string on the screen. Consider the following code snippet that will take the input including the space.

# include<iostream>
#include<cstdio>
using namespace std;
int main()
{
charname[20];
cout<<“Enter your name:”;
gets(name);
cout<<“Hello”<<name;
}

More console functions
Input functions

Plus Two Computer Application Notes Chapter 3 Functions 2

Output functions

Plus Two Computer Application Notes Chapter 3 Functions 3

Stream functions for I/O operations: Some functions that are available in the header file iostream.h to perform I/O operations on character and strings(stream of characters). It transfers streams of bytes between memory and objects. Keyboard and monitor are considered as the objects in C++.

Input functions: The input functions like get( ) (to read a character from the keyboard) and getline() (to read a line of characters from the keyboard) is used with cin and dot(.) operator.

Plus Two Computer Application Notes Chapter 3 Functions 4

Eg.

# include<iostream>
using namespace std;
int main()
{
char str[80], ch= 'z';
cout<<“enter a string that end with z:”;
cin.getline(str, 80, ch);
cout<<str;
}

If you run the program you will get the prompt as follows:
Enter a string that end with z: Hi I am Jobi. I am a teacherz. My school is BVM HSS
The output will be displayed as follows and the string after ‘z’ will be truncated.
Hi I am Jobi. I am a teacher

Output function: The output functions like put() (to print a character on the screen) and write() (to print a line of characters on the screen) is used with cout and dot(.) operator.

Plus Two Computer Application Notes Chapter 3 Functions 5

Complex programs are divided into smaller subprograms. These subprograms are called functions.
Eg. main(), clrscr(), sqrt(), strlen(),…

Concept of modular programming: The process of converting big and complex programs into smaller programs is known as modularisation. These small programs are called modules or subprograms or functions. C++ supports modularity in programming called functions.

Merits of modular programming

  • It reduces the size of the program
  • Less chance of error occurrence
  • Reduces programming complexity
  • Improves reusability

Demerits of modular programming
While dividing the program into smaller ones extra care should be taken otherwise the ultimate result will not be right.

Functions in C++
Some functions that are already available in C++ are called pre defined or built in functions.
In C++, we can create our own functions for a specific job or task, such functions are called user-defined functions.
A C++ program must contain a mainO function. A C++ program may contain many lines of statements(including so many functions) but the execution of the program starts and ends with main() function.

Predefined functions
To invoke a function that requires some data for performing the task, such data is called parameter or argument. Some functions return some value back to the called function.

String functions
To manipulate string in C++ a header file called string.h must be included.
a) strlen() – to find the number of characters in a string(i.e. string length).
Syntax: strlen(string);
Eg.
cout<<strlen(“Computer”); It prints 8.

b) strcpy() – It is used to copy the second string into the first string.
Syntax: strcpy(string1, string2);
Eg.
strcpy(str, “BVM HSS”);
cout<<str; It prints BVM HSS.

c) strcat() – It is used to concatenate the second string into first one.
Syntax: strcat(string1, string2)
Eg.
strcpy(str1, “Hello”);
strcpy(str2, “World”);
strcat(str1, str2);
cout<<str1; It displays the concatenated string “Hello World”

d) strcmp() – it is used to compare two strings and returns an integer.
Syntax: strcmp(string1, string2)

  • if it is 0 both strings are equal.
  • if it is greater than 0(i.e. +ve) stringl is greater than string2
  • if it is less than 0(i.e. -ve) string2 is greater than string1

Eg.

#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char str1[10], str2[10];
strcpy(str1, “Kiran”);
strcpy(str2, “Jobi”);
cout<<strcmp(str1, str2);
}

It returns a +ve integer.

e) strcmpi() – It is same as strcmp() but it is not case sensitive. That means uppercase and lowercase are treated as same.
Eg. “ANDREA” and “Andrea” and “andrea” these are same.

#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char str1[10], str2[10];
strcpy(str1, "Kiran”);
strcpy(str2, "KIRAN”);
cout<<strcmpi(str1, str2);
}

It returns 0. That is both are the same.

Mathematical functions.
To use mathematical functions a header file called math.h must be included
a) abs() – To find the absolute value of an integer.
Eg. cout<<abs(-25); prints 25.
cout<<abs(+25); prints 25.

b) sqrt() – To find the square root of a number.
Eg. cout<<sqrt(49); prints 7.

c) pow() – To find the power of a number.
Syntax. pow(number1, number2)
Eg. cout<<pow(2, 10); It is equivalent to 210. It prints 1024.

Character functions
To manipulate the character in C++ a header file called ctype.h must be included.
a) isupper() – To check whether a character is in uppercase or not. If the character is in uppercase it returns a value 1 otherwise it returns 0.
Syntax: isupper(char ch);

b) islower() – To check whether a character is in lowercase or not. If the character is in lowercase it returns a value 1 otherwise it returns 0.
Syntax: islower(char ch);

c) isalpha() – To check whether a character is an alphabet or not. If the character is an alphabet it returns a value 1 otherwise it returns 0.
Syntax: isalpha(char ch);

d) isdigit() – To check whether a character is a digit or not. If the character is a digit it returns a value 1 otherwise it returns 0.
Syntax: isdigit(char ch);

e) isalnum() – To check whether a character is an alphanumeric or not. If the character is an alphanumeric it returns a value 1 otherwise it returns 0.
Syntax: isalnum(char ch);

f) toupper() – It is used to convert the given character into uppercase.
Syntax: toupper(char ch);

g) tolower() – It is used to convert the given character into lowercase.
Syntax: tolower(char ch);

User defined functions

Syntax:

Return type Function_name(parameter list)
{
Body of the function
}
  1. Return type: It is the data type of the value returned by the function to the called function;
  2. Function name: A name given by the user.

Different types of User-defined functions.

  1. A function with arguments and return type.
  2. A function with arguments and no return type.
  3. A function with no arguments and with the return type.
  4. A function with no arguments and no return type.

Prototype of functions

Consider the following codes

Method 1

# include<iostream>
using namespace std;
int sum(int n1, int n2)
{
return(n1+n2);
}
int main()
{
int n1, n2;
cout<<“Enter 2 numbers:”; cin>>n1>>n2;
cout<<“The sum is “<<sum(n1, n2);
}

Method 2

#include<iostream>
using namespace std;
int main()
{
int n1, n2;
cout<<“Enter 2 numbers:”; cin>>n1>>n2;
cout<<“The sum is “<<sum(n1, n2);
}
int sum(int n1, int n2)
{
retum(n1+n2);
}

In method 1 the function is defined before the main function. So there is no error.
In method 2 the function is defined after the main function and there is an error called “function sum should have a prototype”. This is because the function is defined after the main function. To resolve this a prototype should be declared inside the main function as follows.

Method 2

# include<iostream>
using namespace std;
int main()
{
int n1, n2;
int sum(int, int);
cout<<“Errter 2 numbers:"; cin>>n1>>n2;
cout<<“The sum is “<<sum(n1, n2);
}
int sum(int n1, int n2)
{
return(n1+n2);
}

Functions with default arguments
We can give default values as arguments while declaring a function. While calling a function the user doesn’t give a value as arguments the default value will be taken. That is we can call a function with or without giving values to the default arguments.

Methods of calling functions: Two types call by value and call by reference.
1. Call by value: In the call by value method, the copy of the original value is passed to the function, if the function makes any change will not affect the original value.
2. Call by reference: In the call by reference method, the address of the original value is passed to the function, if the function makes any change will affect the original value.

Scope and life of variables and functions
a) Local scope – A variable declared inside a block can be used only in the block. It cannot be used in any other block.
Eg.

#include<iostream>
using namespace std;
int sum(int n1, int n2)
{
int s;
s=n1+n2;
return(s);
}
int main()
{
int n1, n2;
cout<<“Enter 2 numbers:"; cin>>n1>>n2;
cout<<“The sum is “<<sum(n1, n2);
}

Here the variable s is declared inside the function sum and has a local scope;

b) Global scope – A variable declared outside of all blocks can be used anywhere in the program.

# include<iostream>
using namespace std;
int s;
int sum(int n1, int n2)
{
s=n1+n2;
return(s);
}
int main()
{
int n1, n2;
cout<<“Enter 2 numbers:"; cin>>n1>>n2;
cout<<“The sum is “<<sum(n1, n2);
}

Here the variable s is declared outside of all functions and we can use variable s anywhere in the program.

Plus Two Computer Application Notes