0% found this document useful (0 votes)
27 views

3 Functions

Uploaded by

Krishna Raj
Copyright
© © All Rights Reserved
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

3 Functions

Uploaded by

Krishna Raj
Copyright
© © All Rights Reserved
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Chapter 3

Functions

Prepared By
TCA Gafoor
HSST Computer Application
AKM HSS KOTTOOR

Download these slides


http://www.hsslive.in
Concept of modular programming

Modular programming is the act of designing and writing


programs as functions. A large program is broken down into sub-
programs(Modules). The process of breaking large programs into
smaller sub programs is called modularization. The subprograms
are also called functions.
Functions are classified into two bulit-in or predefined
functions and user defined functions.
Built-in functions are readily available to the users and are stored
in header files. They are a part of C++ Library.
User defined functions are written by the user for specific tasks.
Two methods are used for modular programming top-down and
bottom-up method
Advantages of Functions
a)Reduction in program size.
Modular approach helps to isolate repeated task.Thus program size is reduced.
b)Less chance of error
When size of program is reduced syntax error will be less. Modularity helps to
reduce logical error.
c)Reduce programming complexity
Modularity helps to reduce program complexity by dividing larger programs into
functions.
d)Improve reusability
Once a function is written it can be used later. It helps to reduce development
time.
Dis-advantages of Modular programming
Modular programming requires more time and memory.
Predefined functions

Console functions for character I/O


The console I/O functions are functions
which reads directly from the keyboard
and outputs to the monitor.
These functions require the inclusion of
header file cstdio(stdio.h) in the program.
Functions are getchar(),putchar(),
getchar( ) & Putchar()
getchar( )function: returns a single c
the keyboard.
Example:
char ch=getchar( )
putchar( )function: displays or pr
character.
Example:
char ch=’P’;
putchar(ch); //Displays P on the screen
Stream functions for I/O operations

A stream is a group of bytes. Streams are


available in the form of functions stored in
iostream header file. C++ supports a
number of input/output operations

Input functions : get(),getline()


Output functions :put(),write()
get( ) & getline( )
a) get( )
The get( ) function is accept a single character or stream through
the keyboard.
char ch;
ch=cin.get(ch); //accepts a character and stores in ch
cin.get(ch); //accepts a character and stores in ch
b) getline( )
The getline( ) function is used to accept a string through the keyboard. It
reads a string until a newline character is read. The format of getline
function is
cin.getline(line,size);
Where line is the name of the variable which stores line of text and size
specifies the maximum number of characters which can be stored in
line.
Sample Program using getline
#include<iostream>
using namespace std;
int main( )
{
char str[40];
cout<<"Enter the name";
cin.getline(str,40);
cout<<str;
}
Output functions
The output functions allows flow of bytes(stream) from memory to
output object cout
a)Put( ) function
The put( ) function is used to display a single character.
Example:
char ch='c';
cout.put(ch);
b)write( ) function
The write( ) function is used to displays a string.
Example:
char str[10]="hello";
cout.write(str,10);
String functions

A string is a group of characters. String functions are used


to manipulate a string. The header file cstring (string.h) is
used for these functions.
i) strlen( )
ii) strcpy( )
iii) strcat( )
iv) strcmp( )
v) strcmpi( )
Mathematical functions

• The header file cmath(math.h) is to be


used for mathematical functions
Character functions
Character functions are used to perform various operations
on characters. The header file cctype(ctype.h) supports
these functions. The commonly used character functions in
C++ are,
• i) isupper( )
• ii) islower( ) V) isalnum( )
• iii) isalpha( ) vi) toupper( )
• iv) isdigit( ) vii) tolower( )
User-defined functions
A user defined function is a group of code to perform a specific
task. Every function in C++ function consists of two parts, A
function header and a function body.
The general format of a function is
data_type function_name(argument_list)
{
statements ;
}
The result of a function is called return value. A function can only return a value. A function which is
defined cannot be executed by itself. A set of values passed to a function is called arguments. Body
contains statements of the function. A function is invoked by another function such as main( ).
Depending on type of communication, between calling and called function , functions are of four
types.
1. Function with no argument and no return value
2. Function with argument and no return value
3. Function with argument and with return value
4. Function with no argument and with return value
Function prototype
Function prototype models the actual function. It specifies the compiler the type and number of
arguments a function use.
Actual and Formal parameters
The values which are passed into a function is called arguments or parameters. The arguments or
parameters present in function definition is called formal parameter and those present in function
call is called actual parameter.
For example
int main( )
{
int x,y;
void output(int x,int y);
//Actual parameters are x and y
}
Note:The number and type of Actual and formal parameters must be the same.
Passing arguments to a function

Based on the method of passing arguments,


function
calling methods can be classified into two
1. Call by Value method .
2. Call by Reference method
Call by value (Pass by value) method

This is the default argument passing method in C++. In this method, the value
contained in the actual argument is passed to the formal argument ie, a copy of the
actual argument is passed to the function. Any) changes made to the formal
argument does not effect actual argument.
Example: Swapping two values using call by value
#include<iostream> void swap(int x,int y)
using namespace std; //function definition
void swap(int,int); {
int t;
//function prototype declaration
t=x;
int main( ) x=y;
{ y=t;
int a=10,b=50; cout<<"The values of x and y are :";
swap(a,b);//function call by value cout<<x<<” “<<y;
cout<<"The values of a and b are :"; }
Working of the above program
cout<<a<<” “<<b;
t=10;
} x=50;
y=10;
Call by reference (Pass by
reference) method
In this method a reference of the actual argument is passed to the function. The
actual argument and formal argument shares the same memory location.
Here any changes made to formal argument effects or is reflected back to the
actual arguments. The formal argument is preceded by) ‘&’ Operator.
Example: Swapping two values using call by reference void swap(int &x,int &y)
#include<iostream> //function definition
using namespace std; {
int main( ) int t;
t=x;
{
x=y;
void swap(int &x,int &y); y=t;
//function prototype declaration }
int a=10,b=50; Before Swapping
cout<<"The values of a and b before swapping:"; After Swapping
cout<<a<<" "<<b; t=10;
x=50;
swap(a,b); //function call by value
y=10;
cout<<"\n"<<"The values of a and b after swapping :";
cout<<a<<" "<<b;
Call by value / reference

Comparison
Scope of variables and function

Scope of a variable is the portion of a program where the variable can be


used(accessed).
The variables declared within the body of a block(Function) are called local
variables and can be used within the block. The life of local variables ends
with execution of last instruction of the function.
The variables declared outside any function and which are accessible to all
functions are called global variables. It has life time through out the program
execution. A function which is declared inside the body of another
function is called a local function .
A function declared outside the function body of any other function is called a
global function. It can be used throughout the program. The scope of a global
function is the entire program and that of a local function is only within the
function where it is declared.
Check yourself
1. Define a function to accept a number and return 1 if it is odd, 0 otherwise.
Using this function write a program to display all odd numbers between 10
and 50.
2. Write a program to find the product of three integer numbers using a user
defined function. Invoke the function using call by value and call by reference
methods. V erify the results .
3. Write a program to find the smallest of three or two given numbers using a
function (use the concept of default arguments).
4. With the help of a user-defined function find the sum of digits of a number.
That is if the given number is 345 then the result should be 3+4+5 = 12.
5. Using a function, write a program to find the area of a circle.
6. Write a program to check whether a number is positive,negative or zero.Use
a user defined function for checking.
Thanks for Watching

HSST Computer Application


AKM HSS KOTTOOR

Download these slides


http://www.hsslive.in

You might also like