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

FY_CPP_Functions

HSC 12TH Functions CS
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

FY_CPP_Functions

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

PAPER 1

Chapter 3
Introduction to C++
Functions
FUNCTION
• Sub program
– A self contained block of statements that performs a
particular task.
• Reusability
• Easy to read, write, debug.
• Maintenance & modification is easier.
• Reduces length of the program.

14/12/2022 Kaushik Panchal 2


#include <iostream.h>
void main()
{
cout << “task 1” << endl ;
cout << “task 2” << endl ;
cout << “task 3” << endl ;
}

14/12/2022 Kaushik Panchal 3


#include <iostream.h> void t1()
void main() {
{ cout << “task 1” << endl ;
t1(); }
void t2()
t2();
{
t3();
cout << “task 2” << endl ;
}
}
void t3()
{
cout << “task 3” << endl ;
}

14/12/2022 Kaushik Panchal 4


FUNCTION
• Standard Library Function
– Predefined function
– Example :
• pow(x,y) and sqrt(x) in math.h
• getch() and clrscr() in conio.h
• User Defined Function

14/12/2022 Kaushik Panchal 5


#include <iostream.h>
#include <conio.h> Enter a number : 3
#include <math.h>
void main()
1 1 1
{ 2 8 1.41
clrscr(); 3 27 1.73
int n;
cout<<“Enter a number:“ ;
cin >> n;
for (int i = 1 ; i <= n ; i++)
{
cout<< i << “\t” << pow(i,3) << “\t”
<< sqrt(i)<< endl;
}
getch();
}

14/12/2022 Kaushik Panchal 6


General form of Function
Function
return_type function_name(argument list)
header
{
. . .
// Function body Function
Body
. . .
}

14/12/2022 Kaushik Panchal 7


FUNCTION
• Function Definition : FH + FB
• Function Declaration : FH + ;
• Function Call

• FH – Function Header, FB – Function Body

14/12/2022 Kaushik Panchal 8


#include <iostream.h>
#include <conio.h>
void add()
{
int a,b,c;
cout <<“Enter two numbers :“ ;
cin >> a >> b;
Function Definition =
Function header + Function body
c = a + b;
cout <<“Sum = “ << c ;

}
void main()
{
clrscr();
add(); Function Call
getch();
}
14/12/2022 Kaushik Panchal 9
#include <iostream.h>
#include <conio.h>
void add(); Function Declaration
void main()
{
clrscr();
add(); Function Call
getch();
}
void add()
{
int a,b,c;
cout <<“Enter two numbers :“ ;
cin >> a >> b; Function Definition =
c = a + b; Function header + Function body
cout <<“Sum = “ << c ;
}

14/12/2022 Kaushik Panchal 10


#include <iostream.h> #include <iostream.h>
#include <conio.h> #include <conio.h>
void add(int,int); void add(int,int);
void main() void main()
{ {
clrscr(); clrscr();
add(10,20); int a,b;
getch(); cout <<“Enter two numbers :“ ;
} cin >> a >> b;
void add(int a, int b) add(a,b);
{ getch();
int c; }
c = a + b; void add(int a, int b)
cout <<“Sum = “ << c ; {
} int c;
c = a + b;
cout <<“Sum = “ << c ;
}
14/12/2022 Kaushik Panchal 11
int add(int,int);
void main()
{
clrscr();
int x,y,z;
cout <<“Enter two numbers :“ ;
cin >> x >> y; z x y
z = add(x,y); 30 10 20 Local to main
cout <<“Sum :“ << Z ;
getch();
}
int add(int a, int b)
{
int c;
c = a + b; 30 10 20 Local to add
return c;
} c a b

14/12/2022 Kaushik Panchal 12


int add();
void main()
{
clrscr();
z
int z;
z = add(); 30 Local to main
cout <<“Sum :“ << Z ;
getch();
}
int add()
{
int a,b,c;
cout <<“Enter two numbers :“ ;
cin >> a >> b;
c = a + b; Local to add
30 10 20
return c;
} c a b

14/12/2022 Kaushik Panchal 13


FUNCTION
• Function
– Returning a value
• With argument [ int add(int,int) ]
• Without argument [ int add() ]
– Not Returning a value(void function)
• With argument [ void add(int,int) ]
• Without argument [ void add() ]

14/12/2022 Kaushik Panchal 14


The return statement
• Function terminates execution and returns to the caller.
• Can return only one value
– return (expression);
• It can be empty for void functions
– return ;

14/12/2022 Kaushik Panchal 15


Write a Function in C++ …
1. To find area of circle. [ float area(float) ]
2. to find greatest of three numbers. [ int max(int,int,int)]
3. to find greatest of four numbers. [ int max(int,int,int,int)]
4. to find square and cube of a number.
[ int sqr(int) and int cube(int)]
5. to find factorial of a number. [ int fact(int) ]
6. to print Fibonacci sequence. [ void fib(int) ]
7. to find g.c.d. of two integers. [ int gcd(int,int) ]

14/12/2022 Kaushik Panchal 16


Write a function in C++ …
8. to read set of numbers and find sum of all numbers.
[ int sum_of_set(int) ]
9. to find sum of the digits of a number. [ int sum_of_digit(int) ]
(e.g. : 125 = 1 + 2+ 5 = 8 )
10. to find reverse the digits of a number. [ int reverse(int) ]
(e.g. : 125 => 521 )
11. to check whether a number is palindrome or not. (e.g. : 121)
12. to check whether a number is prime or not.
[ int IsPrime(int) ]
14/12/2022 Kaushik Panchal 17
Storage Class
• Local variable (internal)
• Global variable (external)
• Static variable
• Register variable

14/12/2022 Kaushik Panchal 18


Storage Class
int a,b,c;
void add()
{
c = a + b;
}
void main()
{ Scope of a,b,c is global…
clrscr();
cout <<“Enter two numbers :“ ;
cin >> a >> b;
add();
cout <<“Sum = “ << c ;
getch();
}

14/12/2022 Kaushik Panchal 19


Storage Class (Local and Global)
int a=5;
void f()
{
int b = 10;
cout << a << b << endl;
cout << c ; // error – not in a scope
}
void main()
{
int c = 15;
f();
cout << a ; // global
cout << b ; // error – not in a scope
cout << c ; // local
}

14/12/2022 Kaushik Panchal 20


Storage Class (Local and Global)
int a=5;
a = 10
void main()
{
int a = 10;
cout << “a = ” << a ;
}

int a = 5; local a = 10
void main() global a = 5
{
int a = 10;
cout << “local a = ” << a ; // local
cout << “global a = ” << :: a ; // global
}

14/12/2022 Kaushik Panchal 21


Storage Class (static)
void f()
{ Output :
int a = 0; 1
a++; 1
cout << a << endl; 1
}
void main()
{
f();
f();
f();
}

14/12/2022 Kaushik Panchal 22


Storage Class (static)
void f()
{ Output :
static int a = 0; 1
a++; 2
cout << a << endl; 3
}
void main()
{
f();
f();
f();
}

14/12/2022 Kaushik Panchal 23


Storage Class

Scope life Default value


Local Within the block Within the block Garbage
Global Entire program Entire program 0
Static Within the block Once created, 0
entire program

14/12/2022 Kaushik Panchal 24

You might also like