You are on page 1of 28

Programming

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

• A function is a group of statements that together perform a task.


• Functions Divide and conquer program.
• Construct a program from smaller pieces or components
• Each piece more manageable than the original program
• Functions are used to divide a large code into modules, due to which we can easily
debug and maintain the code.
• For example if we write a calculator program at that time we can write the every
logic in a separate function (For addition sum(), for subtraction sub()).
• Any function can be called many times in main().
• Every C++ program has at least one function, which is main().

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.

cmath is a header file. The function sqrt()


is present in the cmath header file.

Library
function

7
User Defined Function

• These functions are created


by programmer according
to their requirements.

• For example suppose you want


to create a function for add two
numbers then you create a
function with name add() this
type of function is called user
defined function.

8
Components of Functions

A function usually has three components.

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:

Parameters − A parameter is like a placeholder. When a function is invoked,


you pass a value to the parameter. This value is referred to as actual
parameter or argument. The parameter list refers to the type, order, and Parameters
number of the parameters of a function. Parameters are optional; that is, a
function may contain no parameters.

Function Body − The function body contains a collection of statements that


define what the function does.

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.

Consider the figure.

1. When a program begins running, the system calls the


main() function, that is, the system starts executing codes
from main() function.

2. When control of the program reaches to function_name()


inside main(), it moves to void function_name() and all
codes inside void function_name() is executed.

3. Then, control of the program moves back to the main


function where the code after the call to the
function_name() is executed as shown in figure above. 15
Example: Function with argument and return value
Function declaration informs the compiler about the Function’s
name, Type and number of parameters and Type of value it returns.

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

Total value is 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

Total value is 300


Total value is 120

Successfully compiled

28

You might also like