Functions Extra Examples.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

C Language.
Spring Semester 2013 Lecture 5
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions.
Chapter Five Functions
Modular Programming With Functions
By Senem Kumova Metin 1 DATA TYPES. by Senem Kumova Metin 2 DATA TYPE? …… x; // DECLARATION OF VARIABLE X printf(“Do you want to go on? \n”) printf(“Please.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Functions Definition: Instruction block called by name Good design: Each function should perform one task and do it well Functions are the basic building.
The switch Statement.  Occasionally, an algorithm will contain a series of decisions in which a variable or expression is tested separately for each.
Nested LOOPS.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
The switch Statement.  Occasionally, an algorithm will contain a series of decisions in which a variable or expression is tested separately for each.
Modular C Programming & Function #include printNewLine() { printf(“\n”); } main() { printf(“C programming is easy”); printNewLine(); printf(“But, you still.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions Lecture 12.
USER DEFINED FUNCTIONS Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Sudeshna Sarkar, IIT Kharagpur 1 Functions Lecture
Modularity using Functions Chapter 4. Modularity In programming blocks of code often can be "called up" and reused whenever necessary, for example code.
FUNCTIONS. Midterm questions (1-10) review 1. Every line in a C program should end with a semicolon. 2. In C language lowercase letters are significant.
Functions Chapter 5. Function A set of instructions that are designed to perform specific task. A complete and independent program. It is executed by.
EXAMPLE. Dr. Soha S. Zaghloul2 Write a complete program that searches for all the elements that are multiple of 7 in array X of type int and size 100.
Week 4 – Functions Coding Functions. Purpose of Coding Functions A function is written to perform a well-defined task; rather than having all logic in.
Repetition statements
ARRAYS.
Decision making If.. else statement.
BASIC ELEMENTS OF A COMPUTER PROGRAM
Functions, Part 2 of 2 Topics Functions That Return a Value
Functions Separate Compilation
REPETITION STATEMENTS
FUNCTIONS EXAMPLES.
Functions in C Mrs. Chitra M. Gaikwad.
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
OUTPUT STATEMENTS GC 201.
FUNCTIONS WITH ARGUMENTS
FIGURE 4-10 Function Return Statements
Local Variables variables which are declared within a
2011/11/10: Lecture 21 CMSC 104, Section 4 Richard Chang
User Defined Functions
INPUT & OUTPUT scanf & printf.
Functions I Creating a programming with small logical units of code.
Chapter 5 Function Basics
Functions.
CS150 Introduction to Computer Science 1
1) C program development 2) Selection structure
Lec8.
Introduction to C Topics Compilation Using the gcc Compiler
A function with one argument
Functions with arrays.
Decision making If statement.
Strings Dr. Soha S. Zaghloul updated by Rasha ALEidan
FIGURE 4-10 Function Return Statements
Conversion Check your class notes and given examples at class.
REPETITION STATEMENTS
Introduction to C Topics Compilation Using the gcc Compiler
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Introduction to Programming
Arrays.
Predefined Functions Revisited
FIGURE 4-10 Function Return Statements
Introduction to Programming
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Functions I Creating a programming with small logical units of code.
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Functions, Part 2 of 3 Topics Functions That Return a Value
CPS125.
Presentation transcript:

functions Extra Examples

6. Function type – example (1) 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. double Circle (double radius) { double PI = 3.14; double area; area = 3.14 * radius * radius; return (area); } Dr. Soha S. Zaghloul 2

7. Function type – example (2) 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. void PrintLetter (char letter) { int i; for (i= 1; i<= 10; i++) printf (“/n”, letter); } Dr. Soha S. Zaghloul 3

9. Function arguments – example (1) 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. double Circle (double radius) { double PI = 3.14; double area; area = 3.14 * radius * radius; return (area); } Dr. Soha S. Zaghloul 4

10. Function arguments – example (2) 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. double Average (double sum, int count) { return (sum/count); } Dr. Soha S. Zaghloul 5

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

13. Function statements – example (1) The above example could be written as: The above function has only one return statement. There is no declaration, initialization, nor processing. 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); } Dr. Soha S. Zaghloul 7

Functions are defined after the end of the main function 14. Functions defintion #include <stdio.h> int main (void) { ------ } // end main // start define all functions double CircleArea (double radius) } // end CircleArea // end of program Functions are defined after the end of the main function Dr. Soha S. Zaghloul 8

15. Functions prototypes #include <stdio.h> // Function prototype double CircleArea(double radius); int main (void) { ------ } // end main // start define all functions double CircleArea (double radius) } // end CircleArea // end of program In addition, a prototype of the function should be written before the main function. Dr. Soha S. Zaghloul 9

16. Function call – EXAMPLE (1) #include <stdio.h> // Function prototype double CircleArea(double radius); // FUNCTION PROTOTYPE int main (void) { double circle, r; printf (“Enter circle radius> “); scanf (“%f”, r); circle = CircleArea( r ); // FUNCTION CALL printf (“Area of circle = %f”, circle); } // end main // start define your functions double CircleArea (double radius) // FUNCTION HEADER AND DEFINITION double area; area = 3.14 * radius * radius; return (area); // RETURN VALUE } // end CircleArea // end of program Dr. Soha S. Zaghloul 10

19. multi-arguments Functions – EXAMPLE (1) #include <stdio.h> // Function prototype int power(int num1, int num2); // FUNCTION PROTOTYPE int main (void) { int result; result = power (2, 5) ; // actual parameters  num1 = 2, num2 = 5 (ie 25 = 32) result = power (5, 2); // actual parameters  num1 = 5, num2 = 2 (ie 52 = 25) } // end main // start define your functions int power( int num1, int num2) // formal parameters int i; int product = 1; for (i= 1; i< num2; i++) product *= num1; return product; } // end power // end of program Dr. Soha S. Zaghloul 11

20. zer0-argument Functions – EXAMPLE (1) #include <stdio.h> // Function prototype char DisplayMenu (void); // FUNCTION PROTOTYPE int main (void) { char option; option = DisplayMenu() ; // FUNCTION CALL } // end main // start define your functions char DisplayMenu (void) // FUNCTION HEADER char choice; printf (“C: Area of a Circle \n”); printf (“T: Area of a Triangle \n”); printf (“S: Area of a Square \n”); printf (“ Enter your choice> “); scanf (“%c”, choice); return (choice); // returned value } // end DisplayMenu // end of program Dr. Soha S. Zaghloul 12

21. zero-argument Functions – EXAMPLE (1) #include <stdio.h> // Function prototype char DisplayMenu (void); // FUNCTION PROTOTYPE int main (void) { char option; option = DisplayMenu() ; // FUNCTION CALL } // end main // start define your functions char DisplayMenu (void) // FUNCTION HEADER char choice; printf (“C: Area of a Circle \n”); printf (“T: Area of a Triangle \n”); printf (“S: Area of a Square \n”); printf (“ Enter your choice> “); scanf (“%c”, choice); return (choice); // returned value } // end DisplayMenu // end of program Dr. Soha S. Zaghloul 13

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 14