Professional Documents
Culture Documents
Fundamentals
Function
Programming Fundamentals
Zainab Ishfaq
1
Global vs Local Variables
• In C++, variables can be declared, at any point of time, before they are used in the
instructions.
• Global variables are declared outside any function, and they can be accessed (used) on
any function in the program
• Local variables are declared inside a function, and can be used only inside that function.
2
Global vs Local Variables
3
Function
4
Function
5
Function
• There are two types of function:
1. Library Function
2. User-defined Function
6
Library Function
• Library functions are the built-in function in C++ programming.
For example cin, cout,sqrt
• Programmer can use library function by invoking function directly; they don't need to write it themselves.
Library
function
7
User Defined Function
8
Components of Functions
1. Function Prototype/Declaration
2. Function Definition
3. Function calling.
9
Components of Functions
1. Function Prototype/Declaration C++ program to add two integers. Make
Function declaration informs the compiler about the a function add() to add integers and
display sum in main() function.
1. Function’s name
2. Type and number of parameters
3. Type of value it returns.
For example:
10
int sum(int a, int b);
Components of Functions C++ program to add two integers. Make
a function add() to add integers and
display sum in main() function.
2. Function definition
• Function definition is the body of the function .
• The function body contains a collection of statements that
define what the function does.
Syntax:
For example:
11
Components of Functions C++ program to add two integers. Make
a function add() to add integers and
display sum in main() function.
2. Function definition
Syntax:
Return Type − A function may return a value. The return_type is the data type
of the value the function returns. Some functions perform the desired
operations without returning a value. In this case, the return_type is the
keyword void.
Function Name − This is the actual name of the function. The function name
and the parameter list together constitute the function signature.
12
Components of Functions C++ program to add two integers. Make
a function add() to add integers and
display sum in main() function.
2. Function definition
Syntax:
13
Components of Functions C++ program to add two integers. Make
a function add() to add integers and
display sum in main() function.
3. Function call
• Function call statement calls the function by matching its name and
arguments.
• A function call can be made by using function name and providing the
required parameters.
• While calling a function, there are two ways that arguments can be
passed to a function Arguments
• Call by value
• Call by reference
• Call by pointer
Parameters
• The call by value method of passing arguments to a function copies
the actual value of an argument into the formal parameter of the
function. In this case, changes made to the parameter inside the
function have no effect on the argument.
Example:
Syntax:
function_name(); 14
How User Defined Function Works in c++
Programming
• Function prototype is a declaration of function without its body to give compiler information about user-
defined function.
NOTE: If a user-defined function is defined after main() function, compiler will show error. It is because
compiler is unaware of user-defined function, types of argument passed to function and return type.
• When the user-defined function is invoked from any part of program, it all executes the codes defined in the
body of function.
a 100
Code starts executing from
main() function b 200
ret
ret 200
Successfully compiled
num1 100
num2 200
result
100>200
Condition is False
result 200
16
Example: Function with argument and no return value
Output:
17
Example: Function with argument and no return value
Error!
18
Class Activity
• Write a function to find factorial of the number.
19
Home Work
Write a function in c++ program to check whether a number is prime
or not?
20
Default Values for Parameters
• When you define a function, you Output
can specify a default value for each
of the parameters.
• This value will be used if the
corresponding argument is left
blank when calling to the function.
• This is done by using the
assignment operator and assigning
values for the arguments in the
function definition.
• If a value for that parameter is not
passed when the function is called,
the default given value is used, but
if a value is specified, this default
value is ignored and the passed
value is used instead.
21
Default Values for Parameters
a 100
b 200
result
a 100
b 200
22
Default Values for Parameters
a 100
b 200
× result
result 100+200=300
300
23
Default Values for Parameters
a 100
b 200
result 300
300
a 100
24
Default Values for Parameters
a 100
b 20
result
result 100+20=120
120
25
Default Values for Parameters
a 100
120 b 200
result 300
26
Default Values for Parameters
a 100
120 b 200
result 300
27
Default Values for Parameters
a 100
120 b 200
result 120
Successfully compiled
28