PF Week 8
PF Week 8
WEEK 08
Aqsa Ijaz
Functions in C++
• Declaration: the return type, the name of the function, and parameters (if
any)
• Definition: the body of the function (code to be executed)
void myFunction()
{ // declaration
// the body of the function (definition)
}
Function Declaration
Example
int main() {
myFunction();
return 0;
}
void myFunction() {
cout << "I just got executed!";
}
// Error
However, 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:
Example
// Function declaration
void myFunction();
// Function definition
void myFunction() {
cout << "I just got executed!";
}
Example:
// C++ Program to show function that takes
// two integers as parameters and returns
// an integer
int max(int, int);
Example Explained
• myFunction() is the name of the function
• void means that the function does not have a return value. You will learn
more about return values later in the next chapter
• inside the function (the body), add code that defines what the function
should do
Calling a Function
• Declared functions are not executed immediately. They are "saved for later
use", and will be executed later, when they are called.
• While creating a C++ function, you give a definition of what the function has
to do. To use a function, you will have to call or invoke that function.
• When a program calls a function, program control is transferred to the
called function. A called function performs defined task and when it’s return
statement is executed or when its function-ending closing brace is reached, it
returns program control back to the main program.
• To call a function, you simply need to pass the required parameters along
with function name, and if function returns a value, then you can store
returned value.
Calling a Function
In the following example, myFunction() is used to print a text (the action), when it is called:
Example
//Inside main, call myFunction():
// Create a function
void myFunction() {
cout << "I just got executed!";
}
int main() {
myFunction(); // call the function
return 0;
}
Example
void myFunction() {
cout << "I just got executed!\n";
}
int main() {
myFunction();
myFunction();
myFunction();
return 0;
}
#include <iostream>
using namespace std;
int main() {
favoriteHobby("reading"); // Example call
return 0;
}
Types of Functions
1. Built-in Functions
2. User Defined Functions
1. Built-in Functions:
These are pre-defined functions provided by C++ libraries. They are
part of the Standard Template Library (STL) or other included
libraries.
Types are:
1. Local Functions
2. Global Functions
Local Scope:
A function that is accessible only within the block in which it is
declared. Functions defined inside classes, or as inline functions,
typically have a local scope.
Example:
void main(){
void show(void);
show();
}
Global Scope:
A function defined outside of all classes, namespaces, or other
functions. It can be accessed from anywhere in the file after its
declaration.
Example: