You are on page 1of 20

Functions with

Parameters

Lecture Link:
https://www.youtube.com/watch?v=RPQEHjZRRoU&list=PLVEVLI2v6thVDz7UxU
PnURUKaqWFK7Z7v&index=30
C++ Function Parameters

 Information can be passed to functions as a


parameter. Parameters act as variables inside
the function.
 Parameters are specified after the function
name, inside the parentheses. You can add
as many parameters as you want, just
separate them with a comma.
Syntax

 void functionName(parameter1, param


eter2, parameter3) {
// code to be executed
}
Example
 The following example has a function that
takes a string called fname as parameter.
When the function is called, we pass along
a first name, which is used inside the
function to print the full name.
 When a parameter is passed to the
function, it is called an argument. So, in
the example : fname is a parameter, while
Liam, Jenny and Anja are arguments.
Code
 #include <iostream>
 #include <string>
 using namespace std;

 void myFunction(string fname) { //parameter


 cout << fname << " \n";
 }

 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 (=).

 A parameter with a default value, is often


known as an "optional parameter". From the
example given, country is an optional
parameter and "Norway" is the default value.

 If we call the function without an argument,


it uses the default value .
Example
 #include <iostream>
 #include <string>
 using namespace std;

 void myFunction(string country = "Norway") {


 cout << country << "\n";
 }

 int main() {
 myFunction("Sweden");
 myFunction(“Pakistan");
 myFunction();
 myFunction("USA");
 return 0;
 }
C++ Multiple Parameters

 Inside the function, you can add as many


parameters as you want:
 Note that when you are working with
multiple parameters, the function call must
have the same number of arguments as
there are parameters, and the arguments
must be passed in the same order.
Example
 #include <iostream>
 #include <string>
 using namespace std;

 void myFunction(string fname, int age) {


 cout << fname << age << " years old. \n";
 }

 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

 The void keyword, used in the previous


examples, indicates that the function should
not return a value. If you want the function
to return a value, you can use a data type
(such as int, string, etc.) instead of void,
and use the return keyword inside the
function.
Return Type of Function

Declaration

int square ( int ) ;


Definition

int square ( int i )


{
return ( i * i ) ;
}
Example
 #include <iostream>
 using namespace std;

 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

cout<<"\n"<<"the control is back in main function


again";

}
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 myFunction(int x, int y) {


 int sum;
 sum= x+y;
 return sum;
 }

 int main() {
 cout << myFunction(5, 3);
 }
continued..

 The code segments discussed in previous slide is a


function named myFunction. This function accepts two
arguments i.e. x and y of the type int. The return_value
is int, thus this function will return an integer value.
 So, when this function is called in the program, it will
perform its task which is to calculate the sum of any
two numbers and return the result of the
summation.
 Note that if the function is returning a value, it needs to
use the keyword ‘return’.
Example: Function to calculate
integer power ( Xn )
#include<iostream>
using namespace std;
double raiseToPow ( double x , int power )
{
double result ;
int i ;
result = 1.0 ;
for ( i = 1 ; i <= power ; i ++ ) // braces first
{
result *= x; // result = result *x
}
return ( result ) ;
}

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;

void display(int m[5]);


int main()
{
int marks[5] = {88, 76, 90, 61, 69};
display(marks);
return 0;
}
void display(int m[5])
{
cout << "Displaying marks: "<< endl;

for (int i = 0; i < 5; ++i)


{
cout << "Student "<< i + 1 <<": "<< m[i] << endl;
}
}
 When an array is passed as an argument to
a function, only the name of an array is
used as argument.
 display(marks);
 Also notice the difference while passing
array as an argument rather than a variable.
 void display(int m[5]); The argument marks
in the above code represents the memory
address of first element of array marks[5].

You might also like