0% found this document useful (0 votes)
28 views20 pages

L4 2

Uploaded by

Shrushti Bhange
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)
28 views20 pages

L4 2

Uploaded by

Shrushti Bhange
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/ 20

Function Defination

A function is a subprogram that acts on data and often


returns a value.
Why We use Functions??
• Divide the program into subtasks
• Reduce the number of lines
• Easy to read
• Reduce development cost and time
Functions
Function

Built in User defined


Built-in functions
• Also known as library functions
• We can directly call them
✔ pow(x,y), built-in function which is x to the power y.
✔ This function is declared in cmath header file.
User-defined functions
• The functions that we declare and write in our programs.
• It groups code to perform a specific task and that group of code is given a
name(identifier).
Functions
Function

Function call Function definition Function declaration


1 #include<iostream> comment
2 using namespace std;
3
4 int addition (int, int); Function declaration
5
6 int main()
7 {
8 int x = 10, y = 20;
9 int res = addition(x,y); Function call
10 cout << “result = “ << res;
11 return 0;
12 }
13
14 int addition (int a, int b) Function definition
15 {
16 int sum = a + b;
17 return sum;
18 }
19
20
21
22
1 #include<iostream> comment
2 using namespace std;
3
4 int addition (int, int); Function declaration
5
6 int main() • It tells the compiler about a
7 { function name and how to call
8 int x = 10, y = 20; the function.
9 int res = addition(x,y);
10 cout << “result = “ << res; • The actual body of the
11 return 0; function can be defined
12 } separately.
13
14 int addition (int a, int b)
15 {
16 int sum = a + b;
17 return sum;
18 }
19
20
21
22
1 #include<iostream> comment
2 using namespace std;
3
4 int addition (int, int); Function call
5
6 int main() • Pass the required parameters
7 { along with the function name,
8 int x = 10, y = 20; and if the function returns a
9 int res = addition(x,y); value, then you can store the
10 cout << “result = “ << res; returned value.
11 return 0;
12 } • x & y are actual parameters
13
14 int addition (int a, int b) • a & b are formal parameters
15 {
16 int sum = a + b;
17 return sum;
18 }
19
20
21
22
1 #include<iostream> comment
2 using namespace std;
3
4 int addition (int, int); Function definition
5
6 int main() • The function body contains a
7 { collection of statements that
8 int x = 10, y = 20; define what the function does.
9 int res = addition(x,y);
10 cout << “result = “ << res;
11 return 0;
12 }
13
14 int addition (int a, int b)
15 {
16 int sum = a + b;
17 return sum;
18 }
19
20
21
22
Types of user-defined functions
• No argument and no return value// int function(){ }
• No argument but return value // int function(){ return 1;}
• With argument but no return value // int function(int x, int y, int z){ }
• With argument and return value// int function(int x, float y){ return x*y; }
1 #include<iostream> comment
2 using namespace std;
3
4 void greatNum(); With no arguments and no return
5 value
6 int main()
7 {
8 greatNum();
9 return 0;
10 }
11
12 void greatNum()
13 {
14 int x, y;
15 cin >> x >> y;
16 if(x > y)
17 cout <<“The greater number is”<< x;
18 else
19 cout <<“The greater number is”<< y;
20 }
21
22
1 #include<iostream> comment
2 using namespace std;
3
4 int greatNum(); With no arguments and a return
5 value
6 int main()
7 {
8 int a = greatNum();
9 cout <<“The greater number is”<< a;
10 return 0;
11 }
12
13 int greatNum()
14 {
15 int x, y, greaterNum;
16 cin >> x >> y;
17 if(x > y)
18 greaterNum = x;
19 else
20 greaterNum = y;
21 return greaterNum;
22 }
1 #include<iostream> comment
2 using namespace std;
3
4 void greatNum(int, int); With arguments and no return
5 value
6 int main()
7 {
8 int x, y;
9 cin >> x >> y;
10 greatNum(x, y);
11 return 0;
12 }
13
14 void greatNum(int x, int y)
15 {
16 if(x > y)
17 cout <<“The greater number is”<< x;
18 else
19 cout <<“The greater number is”<< y;
20 }
21
22
1 #include<iostream> comment
2 using namespace std;
3
4 int greatNum(int , int); With arguments and a return
5 value
6 int main()
7 {
8 int x, y;
9 cin >> x >> y;
10 int a = greatNum(x, y);
11 cout <<“The greater number is”<< a;
12 return 0;
13 }
14
15 int greatNum(int x, int y)
16 {
17 if(x > y)
18 return x;
19 else
20 return y;
21 }
22
Swapping two Numbers
1 // Code to swap two numbers
2 #include<iostream>
3 using namespace std; n1 n2
4 35 45
5 void swap(int a, int b)
6 {
7 int temp;
8 temp = a; a b
9 a = b; 35 45
10 b = temp;
11 } temp
12 int main()
13 { 35
14 int n1 = 35, n2 = 45;
15 cout <<"Before : “<< n1 << “,” << n2;
16 swap(n1,n2); Output:
17 cout <<"After : “<< n1 “,” << n2;
18 return 0; Before : 35, 45
19 } After : 35, 45
20
21
22
1 // Code to swap two numbers
2 #include<iostream>
3 using namespace std; n1 n2
4 void swap(int *a, int *b) 35 45
5 {
6 int temp; 1000 2000
7 temp = *a;
8 *a = *b; a b
9 *b = temp; 1000 2000
10 }
11 int main() temp
12 {
13 int n1 = 35, n2 = 45; 35
14 cout <<"Before : “<< n1 << “,” << n2;
15 swap(&n1,&n2);
16 cout <<"After : “<< n1 “,” << n2; Output:
17 return 0;
18 } Before : 35, 45
19 After : 45, 35
20
21
22
THANK YOU

You might also like