-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctions.cpp
More file actions
182 lines (139 loc) · 4.33 KB
/
Copy pathFunctions.cpp
File metadata and controls
182 lines (139 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
Introduction of Functions in C++
A function is a block of code which only runs when it is called.
You can pass data, known as parameters, into a function.
Functions are used to perform certain actions, and they are important for reusing code: Define the code once, and use it many times.
It promote DRY principle(Which means do not repeat yourself)
Call a Function
Declared functions are not executed immediately. They are "saved for later use", and will be executed later, when they are called.
To call a function, write the function's name followed by two parentheses () and a semicolon ;
In the following example, myFunction() is used to print a text (the action), when it is called:
Inside main, call myFunction():
Function Declaration and Definition
A C++ function consist of two parts:
Declaration: the return type, the name of the function, and parameters (if any)
Definition: the body of the function (code to be executed)
#include <bits/stdc++.h>
using namespace std;
void sayhello(){ Declaration of function
cout<<"Hello\n"; Definition of function
}
int main()
{
sayhello(); Calling of Function
return 0;
}
Four types of Function based on argument and return type
Function - no argument and no return value
Function - no argument but return value
Function - argument but no return value
Function - argument and return value
*/
// Program to demonstrate Function
/*
#include <bits/stdc++.h>
using namespace std;
void hello()
{
cout << "Hello buddy!\n";
}
int main()
{
hello();
return 0;
}
*/
// Program to demonstrate types of Function based on argument and return type
/*
#include <bits/stdc++.h>
using namespace std;
// no argument and no return value function
void goodmorning()
{
cout << "Good Morning\n";
}
// no argument and no return value function
string goodafternoon()
{
return "Good Afternoon";
}
// argument but no return value function
void Number_upto_n(int n)
{
for (int i = 0; i < n; i++)
{
cout << i + 1 << " ";
}
cout << endl;
}
// argument with return value function
int sum(int num1, int num2)
{
return num1 + num2;
}
int main()
{
cout << "\n Calling Function with no argument and no return type\n";
goodmorning();
cout << "\nCalling function with no argument but return value...\n";
cout << goodafternoon() << endl;
cout << "\nCalling function with argument but no return value...\n";
Number_upto_n(10);
cout << "\nCalling function with argument and return value...\n";
cout << sum(3, 78) << endl;
return 0;
}
*/
// Function Overloading
/*
In this tutorial, we will learn about the function overloading in C++ with examples.
In C++, two functions can have the same name if the number and/or type of arguments passed is different.
These functions having the same name but different arguments are known as overloaded functions. For example:
*/
// Notice all functions have same name but their parameters are different this is called function Overloading
// Program to demonstrate function Overloading
/*
#include <bits/stdc++.h>
using namespace std;
int sum(int a, int b)
{
return a + b;
}
int sum(int a, int b, int c)
{
return a + b + c;
}
int sum(int a, int b, int c, int d)
{
return a + b + c + d;
}
int main()
{
cout << "The sum of given Numbers is : " << sum(7, 6) << endl;
cout << "The sum of given Numbers is : " << sum(7, 6, 9) << endl;
cout << "The sum of given Numbers is : " << sum(7, 6, 8, 9) << endl;
return 0;
}
*/
/*
Function with default arguments:-
In this tutorial, we will learn C++ default arguments and their working with the help of examples.
In C++ programming, we can provide default values for function parameters.
If a function with default arguments is called without passing arguments, then the default parameters are used.
However, if arguments are passed while calling the function, the default arguments are ignored.
*/
// Program to demonstrate function with default arguments
#include <bits/stdc++.h>
using namespace std;
void sum(int a, int b = 10)
{
cout << "The sum of given Numbers is : " << a + b << endl;
}
int main()
{
int a = 10, b = 20;
// If we pass both arguments it is also correct the function sum(a,b) b value will be overrided;
sum(100, 200);
sum(10); // This is also correct as sum(a,b) use a value as 10 and b value as 20 as it is defined by default
return 0;
}