programming in c++ lecture 6- functions
TRANSCRIPT
Programming in C++ Lecture 6- Functions
1
T.K.Malwatta Senior Lecturer
Department of Software Technology Faculty of IT
UoVT
2
Functions A Function is a sub-program that acts on data and often returns a value. Large
programs are generally avoiding because it is difficult to manage a single list of
instructions. Thus, a large program is broken down into smaller units known as
functions. A functions is a named unit of a group of program statements. This unit
can be invoked from other parts of the program.
Why to use Functions ?
The most important reason to use functions is to make program handling easier as
only a small part of the program is dealt with at a time, thereby avoiding ambiguity.
Another reason to use functions is to reduce program size. Functions make a
program more readable and understandable to a programmer thereby making
program management much easier
All C++ programs must contain the function main( ). The execution of the program starts
from the function main( ). A C++ program can contain any number of functions
according to the needs. A function is a set of statement grouped together and given a
valid name .That is executed when it is called from some point of the program.
Pre define functions(built in function)
User defined functions
Standard Library Functions
Mathematical Functions
Some of the important mathematical function in header file <math.h> are as follows
sin(x),cos(x),tan(x),asin(x),exp(x),sqrt(x),pow(x,y)abs(x),fabs(x)
Character functions
All the character functions require <ctype.h> header file below
isalpha(c),isdigit(c) ,isalnum(c), islower(c), isupper(c), toupper(c),tolower(c)
isalpha(c) – It returns True if C is an alphabetical character and false if not
toupper(c) – It convers c to uppercase letter
3
The general form of the user defines function is: -
return_type function_name(parameter list)
{
body of the function
}
Function definition Interface(Prototype) Body -Function name - Using a function involves ‘calling’, it -Function Parameter - Calling function consists of function name followed by arguments -Function return type
y=x(15,3) type name ( parameter1, parameter2, ...) { statement }
int addition (int a,int b)
4
main() {
Statements; func1 ( );
Statements; func2( )
}
Statement;
-- func3();
-- return
func1 ( );
-- -- --
return
func2( )
-- -- --
return
func3( )
How function works
5
Definition of simple function int add(int p, int q) //function interface { int r; //local variable r=p+q; // result return (r); // return value of the function }
How function declare: Function Prototype & body or declaration of function main Function main function function prototype & body
6
Example 1
Write a program to print the sum of two numbers entered by user by defining your own function.
# include<iostream>
using namespace std;
int add(int a, int b)
{
int r;
r=a+b;
return (r);
}
int main()
{
int z;
z=add(5,6);
cout<<"The sum="<<z;
return 0;
}
7
Example 2
#include<iostream>
using namespace std;
int add(int a, int b);
int main()
{
int z;
z=add(5,6);
cout<<"The sum="<<z;
return 0;
}
int add(int a, int b)
{
int r;
r=a+b;
return (r);
}
8
Example 2
C++ program to add any two integers. Make a function add() to
add integer values and display sum in main() function.
# include<iostream>
using namespace std;
int add(int a, int b);
int main()
{
int z,x,y;
cout<<"Enter x,y";
cin>>x>>y;
z=add(x,y);
cout<<"The sum="<<z;
return 0;
}
int add(int a, int b)
{
int r;
r=a+b;
return (r);
}
Scope of variables #include<iostream.h> int x; char character; Global Variable unsigned int num; main() { short age; Local Variable float number; cout<<“enter your age”; cin>>age; }
9
Local variable that is restricted to use within a function of a program.
Function with no argument and no return value
It will not send or receive any parameters and it will not return any values. The
word void appears as the return type and the parameters.
Example 1
#include<iostream>
using namespace std;
void message(void);
int main()
{
message();
return 0;
}
void message(void)
{
cout<<"Functions we can structure our programs in a modular way ";
}
10
#include<iostream>
using namespace std;
void asterisks(void); //function prototype
int main(void)
{
cout<<"Hello World!\n";
asterisks( ); //function call
cout<<“Functions in C++\n";
asterisks( ); //function call
return 0;
}
//function definition
void asterisks(void)
{
int i; // declaring LOCAL variable
for(i = 1; i<=20; i++)
cout<<"*";
cout<<endl;
return; //return value is VOID, no return }
11
Hello World! ******************** Functions in C++ ********************
One of the important features of functions is their ability to be reused. Notice
how the function asterisks() was called twice from main(). The programmer
did not have to duplicate the code.
This function will take arguments (parameters) but will not return a value. The
argument list in the parentheses specifies the types and number of arguments that
are passed to the function.
void sum(int x, int y, int z); //function prototype
Using variable names in the prototype does not actually create the
variables. It merely states the "type" of variables that will be used.
Within the program, the function call sends actual values to the function to be
processed by the function.
sum(75, 95, 83); //function call
void Function name(Parameters) (Functions that take arguments)
Invalid function header
void sum(int x, y, z)
valid function header
void add(int x, int y);
void add(int, int);
12
Default values in Parameters
function declaration we can specify a default value for each parameter. This value
will be used if the corresponding argument is left blank when calling to the function.
If a value for that parameter is not passed when the function is called, the default
value is used, but if a value is specified this default value is ignored and the passed
value is used instead. For example
#include<iostream>
using namespace std;
int divide(int a, int b=2)
{
int r;
r=a/b;
return (r);
}
13
int main()
{
int x,y;
x=divide(12);
cout<<“x=”<<x<<endl;
y=divide(20,4);
cout<<“y”<<y;
return 0; }
Example
#include<iostream> using namespace std;
int divide(int a, int b=2) { int r; r=a/b; return (r); }
int main() { int x,y; x=divide(12);
cout<<“x=”<<x<<endl;
y=divide(20,4);
cout<<“y”<<y; return 0; } •
14
#include <iostream> using namespace std; void display(char = '*', int = 1); int main() { cout<<"No argument passed:\n"; display(); cout<<"\n\nFirst argument passed:\n"; display('#'); cout<<"\n\nBoth argument passed:\n"; display('$', 5); return 0; } void display(char c, int n){ for(int i = 1; i <=n; ++i) { cout<<c; } cout<<endl; }
Example 2
15
Function Overloading
c++ overloading mean using some thing for more than one purpose (>>= insertion
operator =right shift operator)
Two function with the same name , but different parameter types or number .That means
you can give the same name to more then one function if they have either a different
number of parameters or different types in their parameters.
16
Example 1 # include<iostream> using namespace std; int operate(int a, int b) { return(a*b); } float operate (float a,float b) { return (a/b); } int main() { int x=5, y=2; float n=8.0 , m=3.0; cout<<operate(x,y)<<endl; cout<<operate(n,m)<<endl; return 0; }
17
#include <iostream> using namespace std; void test(int); void test(float); void test(int, float); int main() { int a = 5; float b = 5.5; test(a); test(b); test(a, b); return 0; } void test(int var) { cout<<"Integer number: "<<var<<endl; } void test(float var){ cout<<"Float number: "<<var<<endl; } void test(int var1, float var2) { cout<<"Integer number: "<<var1; cout<<" And float number:"<<var2; }
Example 2
Calling overloaded function test() with different functions
18