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

C++ OOP Lecture04 Functions

This document discusses functions in C++. It covers parameters in functions, default function parameters, and function overloading. Parameters are passed by value by default, but object parameters are passed by reference. Memory for class functions is allocated only once when the functions are declared. Function overloading allows multiple functions with the same name but different parameters. The parameters must differ in number, type, or sequence, but not return type.

Uploaded by

Nabil Abdullah
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

C++ OOP Lecture04 Functions

This document discusses functions in C++. It covers parameters in functions, default function parameters, and function overloading. Parameters are passed by value by default, but object parameters are passed by reference. Memory for class functions is allocated only once when the functions are declared. Function overloading allows multiple functions with the same name but different parameters. The parameters must differ in number, type, or sequence, but not return type.

Uploaded by

Nabil Abdullah
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

1

Introduction to Programming [C++]


Lecture 04:
Functions in C++
Learning Objectives
2

To know about:
Functions
Parameters in C++ functions
Default function parameter
Function overloading
Functions
3

#include<iostream> int sum2(){


using namespace std; int a=5;
int main(){ int b=4;
int sum2(); int l;
sum2(); l=a+b;
} cout<<l;
}
Parameters in Functions
4
#include<iostream>
int sum2(int a,int
using namespace std;
b){
int main(){ int l;
int sum2(int x, int y);
l=a+b;
int x=2; cout<<l;
int y=3;

sum2(x,y); }
}
Default Arguments
5

C++ allows calling of functions without


specifying all its arguments.
Only the trailing arguments can have default
values.
No intermediate gap between the default
values is allowed.
Default Arguments
6

#include<iostream>
using namespace std;
int sum2(int a,float b=13.5, double c=14.55){
cout<<a<<","<<b<<","<<c<<endl;
}
int main(){
sum2(2,14.33);
}
Function Overloading
7

 Function overloading means to have more than


one function with the same name but with
different parameters.
 Overloaded functions are differentiated by
checking
1. Number of arguments.
2. Type & sequence of arguments but not
by return type of the function.
Function Overloading
8
#include<iostream> int sum2(int a,int b){
using namespace std; int l;
int main(){ l=a+b;
int sum2(int x, int y); cout<<l;
int sum2(int x,int y, int z); }
int x=2; int sum2(int a,int b,int c){
int y=3;
int l;
int z=4;
l=a+b+c;
sum2(x,y);
cout<<l;
sum2(x,y,z);
}
}
Objects as Function arguments
9

Parameters are by default pass by values.


But the invoking object is pass by reference.
Objects as Function arguments
10
class time { int main()
int hr; {
int min;
time t1;
public:
void setdata(int h=0,int m=0) t1.setdata(3,10);
{ time t2;
hr=h,min=m; t2.setdata(4,10);
} time t3;
void putdata() t3.sumtime(t1,t2);
{
cout<<hr<<min<<endl;
t1.putdata();
} t2.putdata();
void sumtime(time ta,time tb) t3.putdata();
{ return 0;
min=(ta.min+tb.min)%60; }
hr=ta.hr+tb.hr+
(ta.min+tb.min)/60;
}
};
Memory Allocation for Function
11

For all the functions of a class, memory is allocated


only once.
Memory is created for functions when functions
are declared.
12

THANK YOU

You might also like