Functions in Programming

undefined
 
 
Modularity
: break up the problem into sub-
problems.
Top-down design approach
: when attempting to
solve a sub-problem at one level, new sub-
problems arise at a lower level.
Code sharing
: In big projects, the coding is
assigned to different people. The code is then
assembled in a single code. This is impossible to
achieve without the concept of functions.
Create libraries
: in the same way there are stdio.h
and string.h libraries, you may create your own
library.
Dr. Soha S. Zaghloul
   
2
 
In order to deal with functions, we have to know
the following:
Definition: writing the statements of the function
Prototypes: functions declaration
Calling: how to call a function
Dr. Soha S. Zaghloul
   
3
 
Any function has a name.
Any function has a type.
A function may have arguments.
A function has one or more statements.
The following function calculates the area of a
circle:
 
Dr. Soha S. Zaghloul
   
4
double Circle (double radius)
{
   double PI = 3.14;
   double area;
   area =  3.14 * radius * radius;
   return (area);
}
 
The name of a function obeys the same rules as
those of an identifier.
Therefore, the following function names are NOT
accepted:
 
 
 
 
In the previous example, 
Circle
 is the function
name.
 
Dr. Soha S. Zaghloul
   
5
 
A function returns a 
single value
 at most to the
main program.
The type of the function is the same as that of the
returned type.
If the function does not return any value, then the
type is “void”.
 
Dr. Soha S. Zaghloul
   
6
 
 
 
 
 
 
 
In the above example, the function returns the
area
 of the circle.
Since 
area
 is defined in the function as 
double
,
then the function is of type 
double
.
 
Dr. Soha S. Zaghloul
   
7
double
 Circle (double radius)
{
   double PI = 3.14;
   
double
 area;
   area =  3.14 * radius * radius;
   return (
area
);
}
 
 
 
 
 
 
 
The above function should print 
letter 
ten times
on ten lines.
Since it does not return anything, then the type of
the function is 
void.
No 
return
 statement should be associated with a
function of type 
void
.
 
Dr. Soha S. Zaghloul
   
8
void
 PrintLetter (char letter)
{
   int i;
   for (i= 1; i<= 10; i++)
      printf (“/n”, letter);
}
 
The function arguments are the variables through
which values are transferred to the function from
the main program (or from another function) to
work with.
There is no restriction on the number of
arguments.
We may have no arguments.
Each argument has a name and is preceded by its
type.
The order of arguments listing is 
relevant
. This
will be more illustrated when we discuss 
function
calling.
 
Dr. Soha S. Zaghloul
   
9
 
There is NO relation between the type of function
and the number and/or type of arguments. For
example:
A 
void 
function may have arguments;
A 
double
 function may have no arguments;
An 
int 
function may have an argument of type 
char
;
and so on…
The arguments are also sometimes called the
parameter list 
of a function.
 
Dr. Soha S. Zaghloul
   
10
 
 
 
 
 
 
 
In the above example, the parameter list consists
of one variable 
radius
, which is of type 
double
.
Note that 
radius
 is not declared within the
function.
 
Dr. Soha S. Zaghloul
   
11
double Circle (
double radius
)
{
   double PI = 3.14;
   double area;
   area =  3.14 * radius * radius;
   return (area);
}
 
 
 
 
 
 
 
In the above example, the parameter list consists
of two variables 
sum 
(of type 
double
), and 
count
(of type 
int
)
Arguments are separated by 
commas
.
Note that the 
return
 statement may include an
arithmetic operation.
 
Dr. Soha S. Zaghloul
   
12
double Average (
double sum, int count
)
{
  return (sum/count);
}
 
 
 
 
 
 
 
 
The above function contains no arguments, and is
of type 
void
.
It is called to display the shown menu.
 
Dr. Soha S. Zaghloul
   
13
void Menu(void)
{
  printf (“ +: Addition \n”);
  printf (“ -: Subtraction \n”);
  printf (“ *: Multiplicaiton \n”);
  printf (“ /: Division \n”);
  printf (“ %: Modulus \n”);
}
 
The function structure is similar to the main
program, which is actually a function.
Therefore, the structure is as follows:
Declaration part if needed. Refer to slide 13.
Initialization part if needed.
Processing part if needed.
Return statement for non-void functions
 
 
Dr. Soha S. Zaghloul
   
14
 
 
 
 
 
 
The above example could be written as:
 
 
 
 
The above function has only one 
return
statement. There is no declaration, initialization,
nor processing.
 
Dr. Soha S. Zaghloul
   
15
int Sum (int num1, int num2, int num3)
{
   int total;
   total = num1 + num2 + num3;
   return (total);
}
int Sum (int num1, int num2, int num3)
{
   return (num1 + num2 + num3);
}
 
 
 
 
 
 
 
 
Functions are defined after the end of the main
function
 
Dr. Soha S. Zaghloul
   
16
1.
#include <stdio.h>
2.
int main (void)
3.
{
4.
  ------
5.
  ------
6.
  return (0);
7.
} 
// end main
8.
// start define all functions
27.
double CircleArea (double radius)
28.
{
29.
  ------
30.
} 
// end CircleArea
31.
// end of program
 
 
 
 
 
 
 
 
 
 
 
In addition, a prototype of the function should be
written 
before
 the main function.
 
 
Dr. Soha S. Zaghloul
   
17
1.
#include <stdio.h>
2.
// Function prototype
3.
double CircleArea(double radius);
 
4.
int main (void)
5.
{
6.
  ------
7.
  ------
8.
  return (0);
9.
} 
// end main
10.
// start define all functions
11.
double CircleArea (double radius)
12.
{
13.
  ------
14.
} 
// end CircleArea
15.
// end of program
 
Note that the function prototype ends with a
semicolon, while the function header does not.
The program execution starts at the main
function; then each function is executed when
called.
 
 
Dr. Soha S. Zaghloul
   
18
 
Dr. Soha S. Zaghloul
   
19
1.
#include <stdio.h>
2.
// Function prototype
3.
double
 CircleArea(double radius);
 
// FUNCTION PROTOTYPE
 
4.
int main (void)
5.
{
6.
  
double
 circle, r;
7.
  printf (“Enter circle radius> “);
8.
  scanf (“%f”, r);
9.
  
circle
 = CircleArea( r );
  
// FUNCTION CALL
10.
  printf (“Area of circle = %f”, circle);
11.
  return (0);
12.
} 
// end main
 
13.
// start define your functions
14.
double
 CircleArea (double radius)
 
// FUNCTION HEADER AND DEFINITION
15.
{
16.
   
double area
;
17.
   area = 3.14 * radius * radius;
18.
   return (
area
);
   
// RETURN VALUE
19.
}
 
// end CircleArea
20.
// end of program
 
In line 9 of the previous example, we call the function CircleArea:-
The value returned by a function (
area) 
is stored in a
variable 
declared
 in the main function 
(circle)
.
The type of the declared variable should be the same
as that of the function 
(double)
.
The variable used in the 
actual
 argument list 
(r)
replaces the 
formal
  parameter 
(radius). 
The actual
argument
 
should also be declared in the main
function. It should be the same type as the formal
parameter.
In addition, 
r 
should have a value before calling the
function.
Note that the types of the arguments are not written
when calling the function in the main program.
Be sure that the number and types of arguments in the
prototype, the function call, and the function header
are the same.
 
 
Dr. Soha S. Zaghloul
   
20
 
When using multiple-argument functions, you must be
careful:
To include the correct 
number
 of arguments in the function
prototype, function call, and function header.
Also, the 
order
 of the actual arguments used in the function call
must correspond to the order of the formal parameters listed in
the function prototype or heading.
 
Dr. Soha S. Zaghloul
   
21
 
Dr. Soha S. Zaghloul
   
22
1.
#include <stdio.h>
2.
// Function prototype
3.
int
 power(int num1, int num2);
 
// FUNCTION PROTOTYPE
 
4.
int main (void)
5.
{
6.
  
int
 result;
7.
  
result
 = power (2, 5) ;  
// actual parameters 
 num1 = 2, num2 = 5 (ie 2
5 
= 32)
8.
  
result = power (5, 2);
 // actual parameters 
 num1 = 5, num2 = 2 (ie 5
2
 = 25)
9.
return (0);
10.
} 
// end main
 
11.
// start define your functions
12.
int
 power( int num1, int num2) // formal parameters
13.
{
14.
   int i;
15.
   
int product
 = 1;
16.
   for (i= 1; i< num2; i++)
17.
      product *= num1;
18.
   return 
product
;
19.
}
 
// end power
20.
// end of program
 
Dr. Soha S. Zaghloul
   
23
1.
#include <stdio.h>
2.
// Function prototype
3.
char DisplayMenu (void);
  
// FUNCTION PROTOTYPE
 
4.
int main (void)
5.
{
6.
  char option;
7.
  option = DisplayMenu() ; 
  
// FUNCTION CALL
8.
  
return (0);
9.
} 
// end main
 
10.
// start define your functions
11.
char DisplayMenu (void) 
  
// FUNCTION HEADER
12.
{
13.
   char choice;
14.
   printf (“C: Area of a Circle \n”);
15.
   
printf (“T: Area of a Triangle \n”);
16.
   printf (“S: Area of a Square \n”);
17.
   printf (“ Enter your choice> “);
18.
   scanf (“%c”, choice);
19.
   return (choice);
  
 
// returned value
20.
} 
// end DisplayMenu
21.
// end of program
 
Dr. Soha S. Zaghloul
   
24
1.
#include <stdio.h>
2.
// Function prototype
3.
char
 DisplayMenu (void);
  
// FUNCTION PROTOTYPE
 
4.
int main (void)
5.
{
6.
  
char
 option;
7.
  
option
 = DisplayMenu() ; 
  
// FUNCTION CALL
8.
  
return (0);
9.
} 
// end main
 
10.
// start define your functions
11.
char
 DisplayMenu (void) 
  
// FUNCTION HEADER
12.
{
13.
   
char   choice
;
14.
   printf (“C: Area of a Circle \n”);
15.
   
printf (“T: Area of a Triangle \n”);
16.
   printf (“S: Area of a Square \n”);
17.
   printf (“ Enter your choice> “);
18.
   scanf (“%c”, choice);
19.
   return (
choice
);
  
 
// returned value
20.
} 
// end DisplayMenu
21.
// end of program
 
Dr. Soha S. Zaghloul
   
25
Write a complete modular program that displays a menu to perform the
four mathematical operations (+, -, *, /). Each operation should be then
computed in a separate function.
Slide Note
Embed
Share

Functions play a crucial role in programming by enabling modularity, code sharing, and top-down design approach. This content covers the definition, manipulation, naming conventions, types, and examples of functions in programming, along with insights from Dr. Soha S. Zaghloul.

  • Functions
  • Programming
  • Modularity
  • Top-down Design
  • Dr. Soha Zaghloul

Uploaded on Sep 27, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. FUNCTIONS

  2. 1. WHY FUNCTIONS? Modularity: break up the problem into sub- problems. Top-down design approach: when attempting to solve a sub-problem at one level, new sub- problems arise at a lower level. Code sharing: In big projects, the coding is assigned to different people. The code is then assembled in a single code. This is impossible to achieve without the concept of functions. Create libraries: in the same way there are stdio.h and string.h libraries, you may create your own library. Dr. Soha S. Zaghloul Dr. Soha S. Zaghloul 2 2

  3. 2. FUNCTIONS MANIPULATION In order to deal with functions, we have to know the following: Definition: writing the statements of the function Prototypes: functions declaration Calling: how to call a function Dr. Soha S. Zaghloul Dr. Soha S. Zaghloul 3 3

  4. 3. FUNCTIONS DEFINITION Any function has a name. Any function has a type. A function may have arguments. A function has one or more statements. The following function calculates the area of a circle: double Circle (double radius) { double PI = 3.14; double area; area = 3.14 * radius * radius; return (area); } Dr. Soha S. Zaghloul Dr. Soha S. Zaghloul 4 4

  5. 4. FUNCTION NAME The name of a function obeys the same rules as those of an identifier. Therefore, the following function names are NOT accepted: Function name Circle-Area 1Area continue Reason Hyphen is not allowed Should start with a letter Reserved word In the previous example, Circle is the function name. Dr. Soha S. Zaghloul Dr. Soha S. Zaghloul 5 5

  6. 5. FUNCTION TYPE A function returns a single value at most to the main program. The type of the function is the same as that of the returned type. If the function does not return any value, then the type is void . Dr. Soha S. Zaghloul Dr. Soha S. Zaghloul 6 6

  7. 6. FUNCTION TYPE EXAMPLE (1) double Circle (double radius) { double PI = 3.14; double area; area = 3.14 * radius * radius; return (area); } In the above example, the function returns the area of the circle. Since area is defined in the function as double, then the function is of type double. Dr. Soha S. Zaghloul Dr. Soha S. Zaghloul 7 7

  8. 7. FUNCTION TYPE EXAMPLE (2) void PrintLetter (char letter) { int i; for (i= 1; i<= 10; i++) printf ( /n , letter); } The above function should print letter ten times on ten lines. Since it does not return anything, then the type of the function is void. No return statement should be associated with a function of type void. Dr. Soha S. Zaghloul Dr. Soha S. Zaghloul 8 8

  9. 8. FUNCTION ARGUMENTS The function arguments are the variables through which values are transferred to the function from the main program (or from another function) to work with. There is no restriction on the number of arguments. We may have no arguments. Each argument has a name and is preceded by its type. The order of arguments listing is relevant. This will be more illustrated when we discuss function calling. Dr. Soha S. Zaghloul Dr. Soha S. Zaghloul 9 9

  10. 8. FUNCTION ARGUMENTS (CONTD) There is NO relation between the type of function and the number and/or type of arguments. For example: A void function may have arguments; A double function may have no arguments; An int function may have an argument of type char; and so on The arguments are also sometimes called the parameter list of a function. Dr. Soha S. Zaghloul Dr. Soha S. Zaghloul 10 10

  11. 9. FUNCTION ARGUMENTS EXAMPLE (1) double Circle (double radius) { double PI = 3.14; double area; area = 3.14 * radius * radius; return (area); } In the above example, the parameter list consists of one variable radius, which is of type double. Note that radius is not declared within the function. Dr. Soha S. Zaghloul Dr. Soha S. Zaghloul 11 11

  12. 10. FUNCTION ARGUMENTS EXAMPLE (2) double Average (double sum, int count) { return (sum/count); } In the above example, the parameter list consists of two variables sum (of type double), and count (of type int) Arguments are separated by commas. Note that the return statement may include an arithmetic operation. Dr. Soha S. Zaghloul Dr. Soha S. Zaghloul 12 12

  13. 11. FUNCTION ARGUMENTS EXAMPLE (3) void Menu(void) { printf ( +: Addition \n ); printf ( -: Subtraction \n ); printf ( *: Multiplicaiton \n ); printf ( /: Division \n ); printf ( %: Modulus \n ); } The above function contains no arguments, and is of type void. It is called to display the shown menu. Dr. Soha S. Zaghloul Dr. Soha S. Zaghloul 13 13

  14. 12. FUNCTION STATEMENTS The function structure is similar to the main program, which is actually a function. Therefore, the structure is as follows: Declaration part if needed. Refer to slide 13. Initialization part if needed. Processing part if needed. Return statement for non-void functions Dr. Soha S. Zaghloul Dr. Soha S. Zaghloul 14 14

  15. 13. FUNCTION STATEMENTS EXAMPLE (1) int Sum (int num1, int num2, int num3) { int total; total = num1 + num2 + num3; return (total); } The above example could be written as: int Sum (int num1, int num2, int num3) { return (num1 + num2 + num3); } The above function has only one return statement. There is no declaration, initialization, nor processing. Dr. Soha S. Zaghloul Dr. Soha S. Zaghloul 15 15

  16. 14. FUNCTIONS DEFINTION 1. 2. 3. 4. 5. 6. 7. 8. 27. double CircleArea (double radius) 28. { 29. ------ 30. } // end CircleArea 31. // end of program #include <stdio.h> int main (void) { ------ ------ return (0); } // end main // start define all functions Functions are defined after the end of the main function Dr. Soha S. Zaghloul Dr. Soha S. Zaghloul 16 16

  17. 15. FUNCTIONS PROTOTYPES 1. 2. 3. #include <stdio.h> // Function prototype double CircleArea(double radius); 4. 5. 6. 7. 8. 9. 10. // start define all functions 11. double CircleArea (double radius) 12. { 13. ------ 14. } // end CircleArea 15. // end of program int main (void) { ------ ------ return (0); } // end main In addition, a prototype of the function should be written before the main function. Dr. Soha S. Zaghloul Dr. Soha S. Zaghloul 17 17

  18. 15. FUNCTIONS PROTOTYPES (CONTD) Note that the function prototype ends with a semicolon, while the function header does not. The program execution starts at the main function; then each function is executed when called. Dr. Soha S. Zaghloul Dr. Soha S. Zaghloul 18 18

  19. 16. FUNCTION CALL EXAMPLE (1) 1. 2. 3. #include <stdio.h> // Function prototype double CircleArea(double radius); // FUNCTION PROTOTYPE 4. 5. 6. 7. 8. 9. 10. printf ( Area of circle = %f , circle); 11. return (0); 12. } // end main int main (void) { double circle, r; printf ( Enter circle radius> ); scanf ( %f , r); circle = CircleArea( r ); // FUNCTION CALL 13. // start define your functions 14. double CircleArea (double radius) 15. { 16. double area; 17. area = 3.14 * radius * radius; 18. return (area); 19. } // end CircleArea 20. // end of program // FUNCTION HEADER AND DEFINITION // RETURN VALUE Dr. Soha S. Zaghloul Dr. Soha S. Zaghloul 19 19

  20. 17. FUNCTION CALL In line 9 of the previous example, we call the function CircleArea:- The value returned by a function (area) is stored in a variable declared in the main function (circle). The type of the declared variable should be the same as that of the function (double). The variable used in the actual argument list (r) replaces the formal parameter (radius). The actual argumentshould also be declared in the main function. It should be the same type as the formal parameter. In addition, r should have a value before calling the function. Note that the types of the arguments are not written when calling the function in the main program. Be sure that the number and types of arguments in the prototype, the function call, and the function header are the same. Dr. Soha S. Zaghloul Dr. Soha S. Zaghloul 20 20

  21. 18. MULTI-ARGUMENT FUNCTIONS When using multiple-argument functions, you must be careful: To include the correct number of arguments in the function prototype, function call, and function header. Also, the order of the actual arguments used in the function call must correspond to the order of the formal parameters listed in the function prototype or heading. Dr. Soha S. Zaghloul Dr. Soha S. Zaghloul 21 21

  22. 19. MULTI-ARGUMENTS FUNCTIONS EXAMPLE (1) 1. 2. 3. #include <stdio.h> // Function prototype int power(int num1, int num2); // FUNCTION PROTOTYPE 4. 5. 6. 7. 8. 9. 10. } // end main int main (void) { int result; result = power (2, 5) ; // actual parameters result = power (5, 2); // actual parameters return (0); num1 = 2, num2 = 5 (ie 25 = 32) num1 = 5, num2 = 2 (ie 52 = 25) 11. // start define your functions 12. int power( int num1, int num2) // formal parameters 13. { 14. int i; 15. int product = 1; 16. for (i= 1; i< num2; i++) 17. product *= num1; 18. return product; 19. } // end power 20. // end of program Dr. Soha S. Zaghloul Dr. Soha S. Zaghloul 22 22

  23. 20. ZER0-ARGUMENT FUNCTIONS EXAMPLE (1) 1. 2. 3. #include <stdio.h> // Function prototype char DisplayMenu (void); // FUNCTION PROTOTYPE 4. 5. 6. 7. 8. 9. int main (void) { char option; option = DisplayMenu() ; return (0); } // end main // FUNCTION CALL 10. // start define your functions 11. char DisplayMenu (void) 12. { 13. char choice; 14. printf ( C: Area of a Circle \n ); 15. printf ( T: Area of a Triangle \n ); 16. printf ( S: Area of a Square \n ); 17. printf ( Enter your choice> ); 18. scanf ( %c , choice); 19. return (choice); 20. } // end DisplayMenu 21. // end of program // FUNCTION HEADER // returned value Dr. Soha S. Zaghloul Dr. Soha S. Zaghloul 23 23

  24. 21. ZERO-ARGUMENT FUNCTIONS EXAMPLE (1) 1. 2. 3. #include <stdio.h> // Function prototype char DisplayMenu (void); // FUNCTION PROTOTYPE 4. 5. 6. 7. 8. 9. int main (void) { char option; option = DisplayMenu() ; return (0); } // end main // FUNCTION CALL 10. // start define your functions 11. char DisplayMenu (void) 12. { 13. char choice; 14. printf ( C: Area of a Circle \n ); 15. printf ( T: Area of a Triangle \n ); 16. printf ( S: Area of a Square \n ); 17. printf ( Enter your choice> ); 18. scanf ( %c , choice); 19. return (choice); 20. } // end DisplayMenu 21. // end of program // FUNCTION HEADER // returned value Dr. Soha S. Zaghloul Dr. Soha S. Zaghloul 24 24

  25. 22. SELF-CHECK EXERCISE Write a complete modular program that displays a menu to perform the four mathematical operations (+, -, *, /). Each operation should be then computed in a separate function. Dr. Soha S. Zaghloul Dr. Soha S. Zaghloul 25 25

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#