Plus One Computer Science Notes Chapter 10 Functions

Kerala Plus One Computer Science Notes Chapter 10 Functions

Concept of modular programming:
The process of converting big and complex programs into smaller programs is known as modularisation. This small programs are called modules or sub programs 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 main() 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.

Pre-defined 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.
1. strlen():
to find the number of characters in a string(i.e. string length).
Syntax: strlen(string);
eg:
cout<<strien(“Computer”); It prints 8.

2. strcpy():
It is used to copy second string into first string.
Syntax: strcpy(string1, string2);
eg:
strcpy(str,”BVM HSS”);
cout<<str; It prints BVM HSS.

3. strcat():
It is used to concatenate 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”

4. 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 isgreaterthan 0(i.e. +ve) stringl is greater than string2
  • if it is less than 0(i.e. -ve) string2 is greater than stringl

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.

5. strcmpi():
It is same as strcmpO 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 same.

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

2. sqrt():
To find the square root of a number.
eg: cout<<sqrt(49); prints 7.

3. 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.

4. sin():
To find the sine value of an angle and the angle must be in radian. To convert an angle into radian multiply by 3.14(“) and divide by 180.
float x = 60 × 3.14/180;
cout<<sin(x); prints 0.86576.

5. cos():
To find the cosine value of an angle and the angle must be in radian. To convert an angle into radian multiply by 3.14(“) and divide by 180.
float x = 60 × 3.14/180;
cout<<cos(x); prints 0.50046.

Character functions:
To manipulate character in C++ a header file called ctype.h must be included.
1. 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(charch);

2. 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);

3. 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);

4. 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(charch);

5. 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);

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

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

Conversion functions:
Some occasions we have to convert a data type into another for this conversion functions used. The header file stdlib.h must be included.
1. itoa():
It is used to convert an integer value to string type.
Syntax: itoa(int v, char str, int size); This function has 3 arguments, first one is the integer to be converted, second is the string variable to store and third is the size of the string.
eg: itoa(“123”,str,4);
cout<<str;

2. atoi():
It Is the opposite of itoa( ). That is it converts a string into integer.
Syntax: atoi(str);

I/O Manipulating function:
It is used to manipulate I /O operations in C++. The header file iomanip.h must be included,
(a) setw(): It is used to set the width for the subsequent string.
Syntax: setw(size);

User defined functions:
Syntax: Return type Function_name(parameterlist)
{
Body of the function
}

  • Return type: It is the data type of the value returned by the function to the called function;
  • Function name: A name given by the user.

Different types of User defined functions.

  • A function with arguments and return type.
  • A function with arguments and no return type.
  • A function with no arguments and with return type.
  • 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)
{
return(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 of the function is defined after the main function. To resolve this a prototype should be declared inside the main function as follows.

Method 3:
#include<iostream>
using namespace std;
int main()
{
int n1,n2;
int sum(int.int);
cout<<“Enter 2 numbers:”;
cin>>n1>>n2;
cout<<“The sum is “<<sum(n1,n2);
}
int sum(int n1,int n2)
{
retum(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 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 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:
1. Local scope:
A variable declared inside a block can be used only in the block. It cannot be used 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 local scope;

2. Global scope:
A variable declared outside of all blocks can be used any where 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 out side of all functions and we can use variable s any where in the program

Recursive functions:
A function calls itself is called recursive function.

Plus One Computer Science Notes