0% found this document useful (0 votes)
46 views24 pages

Programming Language: Functions

The document discusses functions in C++ programming. It defines what a function is, how to create and call functions, how to pass parameters and return values, pass by reference, function overloading, and more. Examples are provided throughout to demonstrate the concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views24 pages

Programming Language: Functions

The document discusses functions in C++ programming. It defines what a function is, how to create and call functions, how to pass parameters and return values, pass by reference, function overloading, and more. Examples are provided throughout to demonstrate the concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Programming Language

Functions
Content
• Functions
• Introduction
• Definition
• Declaration
• Prototype
• Function with return value
• Function with arguments
• Recursion

2
C++ Functions
• 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.

3
Create a Function
• C++ provides some pre-defined functions, such as main(), which is
used to execute code.
• But you can also create your own functions to perform certain
actions.
• To create (often referred to as declare) a function, specify the name
of the function, followed by parentheses ():

4
Create a Function
void myFunction() {
// code to be executed
}
• myFunction() is the name of the function
• void means that the function does not have a return value
• inside the function (the body), add code that defines what the
function should do

5
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 next slide example, myFunction() is used to print a text (the
action), when it is called:

6
Call a Function
• Inside main, call myFunction():
// Create a function
void myFunction() {
cout << "I just got executed!";
}
int main() {
myFunction(); // call the function
return 0;
}
// Outputs "I just got executed!"
7
Call a Function
• A function can be called multiple times:
void myFunction() {
cout << "I just got executed!\n";
}
int main() {
myFunction();
myFunction();
myFunction();
return 0;
}
// I just got executed!
// I just got executed!
// I just got executed!

8
Function Declaration and Definition
• A C++ function consist of two parts:
• Declaration: the function's name, return type, and parameters (if any)
• Definition: the body of the function (code to be executed)

void myFunction() { // declaration


// the body of the function (definition)
}

9
Function Declaration and Definition
Note: If a user-defined function, such as myFunction() is declared after the
main() function, an error will occur. It is because C++ works from top to
bottom; which means that if the function is not declared above main(), the
program is unaware of it:
int main() {
myFunction();
return 0;
}
void myFunction() {
cout << "I just got executed!";
}
// Error

10
Function Declaration and Definition
• It is possible to separate the declaration and the definition of the function - for code optimization.
• You will often see C++ programs that have function declaration above main(), and function
definition below main(). This will make the code better organized and easier to read:
// Function declaration
void myFunction();
// The main method
int main() {
myFunction(); // call the function
return 0;
}
// Function definition
void myFunction() {
cout << "I just got executed!";
}

11
Parameters and Arguments
• 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:
void functionName(parameter1, parameter2, parameter3)
{
// code to be executed
}

12
Parameters and Arguments
• 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:
void myFunction(string fname) {
cout << fname << " Awan\n";
}
int main() { When a parameter is passed to the
myFunction(“Arshad"); function, it is called an argument. So, from
myFunction(“Ibrahim");
myFunction(“Haroon"); the example above: fname is a parameter,
return 0; while Arshad, Ibrahim and Haroon are
} arguments.
// Arshad Awan
// Ibrahim Awan
// Haroon Awan
13
Default Parameter Value
• You can also use a default parameter value, by using the equals sign (=).
• If we call the function without an argument, it uses the default value ("Norway"):
void myFunction(string country = "Norway") {
cout << country << "\n";
}
int main() {
myFunction("Sweden");
myFunction(“Pakistan"); A parameter with a default value, is often
myFunction(); known as an "optional parameter". From
myFunction("USA");
return 0; the example above, country is an optional
} parameter and "Norway" is the default
// Sweden value.
// Pakistan
// Norway
// USA

14
Multiple Parameters
Inside the function, you can add as many parameters as you want:
void myFunction(string fname, int age) {
cout << fname << " Awan. " << age << " years old. \n";
}
int main() { Note that when you are working with
myFunction(“Arshad", 45); multiple parameters, the function call must
myFunction(“Ibrahim", 12);
myFunction(“Haroon", 16); have the same number of arguments as
return 0; there are parameters, and the arguments
} must be passed in the same order.

// Arshad Awan. 45 years old.


// Ibrahim Awan. 12 years old.
// Haroon Awan. 16 years old.

15
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:
int myFunction(int x) {
return 5 + x;
}
int main() {
cout << myFunction(3);
return 0;
}
// Outputs 8 (5 + 3)

16
Return Values
• This example returns the sum of a function with two parameters:
int myFunction(int x, int y) {
return x + y;
}

int main() {
cout << myFunction(5, 3);
return 0;
}

// Outputs 8 (5 + 3)

17
Return Values
• You can also store the result in a variable:
int myFunction(int x, int y) {
return x + y;
}

int main() {
int z = myFunction(5, 3);
cout << z;
return 0;
}
// Outputs 8 (5 + 3)

18
Pass By Reference
• You can also pass a reference to the function.
• This can be useful when you need to change the value of the arguments:
void swapNums(int &x, int &y) {
int z = x;
x = y;
y = z;
}
int main() {
int firstNum = 10;
int secondNum = 20;
cout << "Before swap: " << "\n";
cout << firstNum << secondNum << "\n";
// Call the function, which will change the values of firstNum and secondNum
swapNums(firstNum, secondNum);
cout << "After swap: " << "\n";
cout << firstNum << secondNum << "\n";
return 0;
}

19
Function Overloading
• With function overloading, multiple functions can have the same
name with different parameters:

int myFunction(int x)
float myFunction(float x)
double myFunction(double x, double y)

Note: Multiple functions can have the same name as long as the number
and/or type of parameters are different.

20
Function Overloading
• Consider the following example, which have two functions that add numbers of
different type:
int plusFuncInt(int x, int y) {
return x + y;
}
double plusFuncDouble(double x, double y) {
return x + y;
}
int main() {
int myNum1 = plusFuncInt(8, 5);
double myNum2 = plusFuncDouble(4.3, 6.26);
cout << "Int: " << myNum1 << "\n";
cout << "Double: " << myNum2;
return 0;
}//output: Int: 13 Double: 10.56

21
Function Overloading
• Instead of defining two functions that should do the same thing, it is better to overload
one.
• In the example below, we overload the plusFunc function to work for both int and
double:
int plusFunc(int x, int y) {
return x + y;
}
double plusFunc(double x, double y) {
return x + y;
}
int main() {
int myNum1 = plusFunc(8, 5);
double myNum2 = plusFunc(4.3, 6.26);
cout << "Int: " << myNum1 << "\n";
cout << "Double: " << myNum2;
return 0;
}//output: Int: 13 Double: 10.56

22
Recursion (Factorial example without recursion)

23
Recursion (Factorial example with recursion)

24

You might also like