Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
8 views43 pages

06 Functions

The document provides an overview of functions in C++, including their definitions, types (user-defined and built-in), and syntax. It explains how to declare, define, and call functions, as well as how to pass parameters by value and by reference. Additionally, it includes examples and assignments for practical understanding of function usage.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views43 pages

06 Functions

The document provides an overview of functions in C++, including their definitions, types (user-defined and built-in), and syntax. It explains how to declare, define, and call functions, as well as how to pass parameters by value and by reference. Additionally, it includes examples and assignments for practical understanding of function usage.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 43

Functions

• Function is a collection of statements that performs


a specific task.
• A program can have many function each function
must have a unique name
• Every function in a C++ program has atleast one
function known as main()
• A program can be divided into small blocks using
functions.
• It is easier to write small function instead of writing
one long program making the code easy and clear
to understand
1
Types of Function
C++ provides two types of function:
• User-defined functions: A type of function
defined by the programmer is called a user-define
function.
• Built-in functions: These functions are predefined
in C++ and are stored in different header files (e.g
clrscr() is a built-in function to clear the screen
and is a part of the header file conio.h)
• sqrt(), pow() etc.
2
User Defined Functions
A user defined consist of the following:
• Function declaration: also known as function
prototyping.
• It provides information to the compiler of the
function to be used in the program.
• It ends with a semicolon.
• It is usually placed just before the main () function
• It consist of: Function name, Function return type
and types of parameters
3
User Defined Functions
Syntax: Return-type Function-name (parameters)
• Return type: it indicates the type of value that will be returned by
the function. (e.g. int is used as return type if the function returns
integer value. If the function return no value, the keyword void is
used)
• Function-name: it indicates the name of function follows similar
rule to that of declaring a variable.
• Parameters: parameters are the values provided to a function
when the function is called
• Parameters are given in two ways:
• int add (int, int)
• int add (int a, int b)
4
Examples

5
Function Definition
• Set of statements that explains what a
function does is called function definition
• The function definition can be written at the
following places:
• Before main () function
• After main function
• In a separate file

6
Function Definition
• No need of function declaration if a function
definition is written before main()
• Function declaration is compulsory if the
function definition is written after main()

7
Defining a Function
• A function is a collection of statements that are
grouped together to perform an operation.
• A function definition consists of its function name,
parameters, return value type, and body.

8
Defining Functions, cont.
• The variables defined in the function header
are known as formal parameters.
• When a function is invoked, you pass a value to
the parameter. This value is referred to as
actual parameter or argument.

9
Defining Functions, cont.
• A Function may return a value.
• The return value type is the data type of the
value the function returns.
• If the function does not return a value, the
return value type is the keyword void.

10
Function body
• The set of statements which are executed inside
the function is known as function body. The
body of functions appears after function
declarator and the statements are written in
curly braces { }
Syntax Function definition
Return- type Function-name (parameters) Function
{ header
statement 1;
statement 2; Function body
} 11
Function Call
The following steps take place when a function is
called:
• The control moves to the function that is called
• All the statements in the function body are
executed
• The control returns back to the calling function
Note: when the control returns back to the calling
function, the remaining statements in the calling
function are executed
12
13
Example (Passing parameter to function by
value)

14
Passing Arguments/parameter by Value
#include <iostream>
using namespace std;
void show(int num);
int main()
{
int n;
cout<<"enter number";
cin>>n;
show(n);
cout<<"end of program";
return 0;
}
void show(int num)
{
cout<<"the entered value is"<<num;
} 15
Parameters passed by Reference
• The address of actual parameter is passed
• The ampersand sign (&) is used with formal
parameters to pass parameters by reference
• The formal parameter and actual parameter
has same memory location, but the memory
location is accessed with two names
• Therefore, if the value is changed in formal
parameter the actual parameter value will also
change
16
Example

Shared memory location 17


Example
#include <iostream>
using namespace std;
void show(int&);
int main()
{
int n;
cout<<"enter number";
cin>>n;
show(n);
cout<<"end of program";
return 0;
}
void show(int &num)
{
cout<<"the entered value is"<<num; 18
#include <iostream>
Example
using namespace std;
// function declaration }
void swap(int &x, int &y); void swap (int &x, int &y)
int main() {
{ int t;
int a,b; t =x;
cout<<"enter an integer:"; x = y;
cin>>a; y = t;
cout<<"enter an integer:"; }
cin>>b;
cout<<"values before
swapping"<<endl;
cout<<"a:"<<a;
cout<<" and b:"<<b<<endl;
cout<<"swapping the
values…"<<endl;
swap(a, b);
cout<<"values after
swapping"<<endl;
cout<<"a:"<<a<<endl;
cout<<"b:"<<b;
return 0; 19
Returning a value from function
i is now 5

20
Returning a value from function
j is now 2

21
Returning a value from function
invoke max(i, j)

22
Returning a value from function
invoke max(i, j)
Pass the value of i to num1
Pass the value of j to num2

23
Returning a value from function
declare variable result

24
Returning a value from function
(num1 > num2) is true since num1
is 5 and num2 is 2

25
Returning a value from function
result is now 5

26
Returning a value from function
return result, which is 5

27
Returning a value from function
return max(i, j) and assign the
return value to k

28
Returning a value from function
Execute the print statement

29
Using returned value in Assignment
statement

int

30
Using returned value in arithmetic
expression

int

31
Using returned value in output statement

int

32
Example
• Write a program that inputs marks in main
function and passes these marks to a function.
The function finds grade of student on the
basis of the following criteria
• Grade A 80 or above marks
• Grade B 60 to 79 marks
• Grade C 40 to 59 marks
• Grade D below 40 marks

33
Example
#include <iostream> else if (m>60)
using namespace std; return 'B';
// function declaration else if (m>40)
return 'C';
char grade(int m);
else
int main() return 'F';
{ }
int marks;
char g;
cout<<"enter marks";
cin>>marks;
g = grade(marks);
cout<<"your grade is"<<g;
}
char grade(int m)
{
if (m>80)
return 'A';
34
Assignment q#1
• Write a program that inputs two numbers in
main () function, passes these numbers to a
function. The function displays the maximum
number

35
Solution
#include <iostream>
using namespace std;
void max(int a, int b);
int main()
{
int x, y;
cout<<"enter value of a and b numbers:";
cin>>x>>y;
max(x,y);
}
void max(int a, int b)
{
if (a>b)
cout<<"maximum number is"<<a;
else
cout<<"maximum number is b";
} 36
Assignment q#2
• Write a program that inputs a number in main
function and passes the number to a function.
The function displays table of that number

37
Solution
#include <iostream>
using namespace std;
void table(int n);
int main()
{
int num;
cout<<"enter a numbers";
cin>>num;
table(num);
}
void table(int n)
{
int c;
for (c=1; c<=10;c++)
{
cout<<n<<"*"<<c<<"="<<n*c<<endl;
}
} 38
Assignment q#3
• Write a program that inputs a number and
displays its precessor and successor numbers
using function

39
Solution
#include <iostream>
using namespace std;
void value(int c);
int main()
{
int x;
cout<<"enter a number: ";
cin>>x;
value(x);
return 0;
}
void value(int c)
{
int p,n;
p = c - 1;
n = c + 1;
cout<<"The number before "<<c<<" is: "<<p<<endl;
cout<<"The number after "<<c<<" is: "<<n<<endl;
} 40
Assignment q#4
• Write a program that inputs a number and a
character. It passes the input to a function that
displays n number of columns and rows of the
given character

41
#include <iostream>
Solution
using namespace std;
void shape(int n, char c);
int main()
{
int num;
char ch;
cout<<"enter a number";
cin>>num;
cout<<"enter a character";
cin>>ch;
shape(num, ch);
}
void shape(int n, char c)
{
int i,j;
for (i=1;i<=n; i++)
{
cout<<endl; 42
Solution
#include <iostream>
using namespace std;
void chk_number (int n)
{
int c=0, i;
for (i=2; i<n; i++)
{
if (n%i==0)
c=1;
}
if (n%2==0 && c==0)
cout<<n<<"is a prime even";
else if (n%2!=0 && c==0)
cout<<n<<"is an odd number";
else if (n%2!=0 && c!=0)
cout<<n<<"is only an even number, not prime";
else if (n%2!=0)
cout<<n<<"is only an odd number, not prime";
Else
cout<<n<<"is not a prime number";
}
int main ()
{
int n;
cout<<"enter a number";
cin>>n;
cout<<"nature of number";
chk_number(n);
}

43

You might also like