Professional Documents
Culture Documents
Parameters
Lecture Link:
https://www.youtube.com/watch?v=RPQEHjZRRoU&list=PLVEVLI2v6thVDz7UxU
PnURUKaqWFK7Z7v&index=30
C++ Function Parameters
int main() {
myFunction(“Adeel"); //arguments
myFunction(“Ahmed");
myFunction(“Afnan");
return 0;
}
C++ Default Parameters
You can also use a default parameter value,
by using the equals sign (=).
int main() {
myFunction("Sweden");
myFunction(“Pakistan");
myFunction();
myFunction("USA");
return 0;
}
C++ Multiple Parameters
int main() {
myFunction(“Adeel", 30);
myFunction(“Ahmed", 14);
myFunction(“Afnan", 4);
return 0;
}
Functions with
return values
Lecture Link:
https://www.youtube.com/watch?v=NK94dq-
RS2Y&list=PLVEVLI2v6thVDz7UxUPnURUKaqWFK7Z7v&index=31
Function with Return Values
Declaration
int myFunction(int x) {
return 5 + x;
}
int main() {
cout << myFunction(3);
}
#include<iostream>
using namespace std;
void displayvalue(int);
int main()
{
cout<<"I am passing value 5 to the function";
displayvalue(5); // calling function
}
void displayvalue (int num)
{
cout<<"\n"<<"the value in the num is :"<<num;
}
This example returns the sum of a function with two
parameters:
#include <iostream>
using namespace std;
int main() {
cout << myFunction(5, 3);
}
continued..
int main ( )
{
double x ;
int p ;
cout << " Please enter the number" ;
cin >> x ;
cout << "Please enter the integer power that you want this number raised to" ;
cin >> p ;
cout << x << "raise to power" << p << "is equal to" << raiseToPow ( x , p ) ;
}
Arguments vs Parameters
The values that are passed into a function call
are called arguments,
Variables that receive those values are called
parameters.
There are several variations of these terms in
use as well. Some call the arguments actual
parameters and call the parameters formal
parameters.
Example: Pass an Array to a function
#include <iostream>
using namespace std;