0% found this document useful (0 votes)
9 views

Function

This document discusses data structures and functions in programming. It defines what a function is and its general structure. It provides examples of different types of functions, including functions without and with parameters, and without and with return values. It also discusses calling functions by value versus by reference and provides examples. Finally, it provides an exercise and assignment for the reader to write functions to calculate average and net salary based on input parameters.

Uploaded by

Mostafa Mahmoud
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Function

This document discusses data structures and functions in programming. It defines what a function is and its general structure. It provides examples of different types of functions, including functions without and with parameters, and without and with return values. It also discusses calling functions by value versus by reference and provides examples. Finally, it provides an exercise and assignment for the reader to write functions to calculate average and net salary based on input parameters.

Uploaded by

Mostafa Mahmoud
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

DATA STRUCTURE

SECTION 1
FUNCTION
1 Dr. Doaa Hany Elsayed
[email protected]
FUNCTION
 Concept: A program may be broken up into manageable
functions.

2
STRUCTURE OF FUNCTION
 A function is a collection of statements that performs a specific
task.
 A general form of a function looks like this :

<return type> FunctionName (parameter 1, parameter 2, ……)


{
Statement;
Statement;
Statement;
}

3
STRUCTURE OF FUNCTION

4
FUNCTION WITHOUT PARAMETERS AND
WITHOUT RETURN VALUE
 Example:

o Function calling:

o Output:
5
FUNCTION WITH PARAMETERS AND
WITHOUT RETURN VALUE
 Example:

 Function calling:

 Output
6
FUNCTION WITHOUT PARAMETERS AND
WITH RETURN VALUE
 Example:

 Function calling:

 Output
7
FUNCTION WITH PARAMETERS AND WITH
RETURN VALUE
 Example:

 Function calling:

 Output
8
EXAMPLE 1
1. Write a program that consists of:
• A function Cal_Avg that receives 2 numbers and returns their Average
• Main function that reads the 2 numbers from the user, calls Cal_Avg and
displays the average
• Answer:

9
CALL BY VALUE
 When a function with multiple parameters is called, the arguments
are passed to the parameters in order.

 When an argument is passed into a parameter, only a copy of the


argument’s value is passed. Changes to the parameter in function
do not affect the original argument.
10
CALL BY VALUE
My
 Example: number
value
12
12 0

 Show the output:


11
CALL BY REFERENCE
 The call by reference method of passing arguments to a
function copies the address of an argument into the formal
parameter.
 It means the changes made to the parameter in function affect
the passed argument.
 Reference variables are defined like regular variables, except
you place an & in front of the name.
 For example:

12
CALL BY REFERENCE
value refVar
 For example:
8
4

 output
13
EXERCISE
Show the output:
A.

Answer:
14
4 11 16
1 5 7
EXERCISE
 Show the output:
B.

Answer:
15
4 11 16
4 11 16
ASSIGNMENT
1. Write a program that consists of:
• Function calc_NetSalary that Receives the worked
hours and pay rate and return the calculated net
salary :
o GrossSalary= WorkedHours* PayRate;
o NetSalary=GrossSalary-10;
• main function a
• Read from user hours and rate

• Call calc_NetSalary function

• Display the net salary

• Remark the number of employees is 100 employees 16

You might also like