FUNCTIONS AND STRUCTURED PROGRAMMING CHAPTER 10. Introduction A c program is composed of at least one function definition, that is the main() function.

Slides:



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

Spring Semester 2013 Lecture 5
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Chapter 6: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
Top-Down Design with Functions 4 What do programmer’s (not programs!) use as input to the design of a program? –Documentation Problem definition Requirements.
Chapter 6: User-Defined Functions I
 Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function 
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
Functions Lecture 4 – Section 2: 9/21/05 Section 4: 9/22/05.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
MAHENDRAN CHAPTER 6. Session Objectives Explain Type of Functions Discuss category of Functions Declaration & Prototypes Explain User Defined Functions.
Chapter 6: User-Defined Functions
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
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.
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 6 User-Defined Functions I. Objectives Standard (predefined) functions What are they, and How to use them User-Defined Functions Value returning.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Functions Exercise 5. Functions a group of declarations and statements that is assigned a name  effectively, a named statement block  usually has a.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
Computer programming Outline Functions [chap 8 – Kochan] –Defining a Function –Arguments and Local Variables Automatic Local.
Chapter 6 Functions 6.1 Modular Design A valuable strategy when writing complex programs is to break down the program into several smaller modules. A module.
1 Structure of a C Program (continued) Presentation original from Dr. Turner’s class USF - COP C for Engineers Summer 2008.
1 CSE1301 Computer Programming Lecture 13 Functions (Part 1)
ITERATIVE STATEMENTS. Definition Iterative statements (loops) allow a set of instruction to be executed or performed several until condition are met.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
CHAPTER 6 USER-DEFINED FUNCTIONS I. In this chapter, you will: Learn about standard (predefined) functions and discover how to use them in a program Learn.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Computer Programming Basics Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
Functions Chapter 5. Function A set of instructions that are designed to perform specific task. A complete and independent program. It is executed by.
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
Announcements. Practice questions, with and without solutions will be uploaded by Friday 5 th November, make sure to check them before the weekend \\netstorage\Subjects\ITCA-b\Exam.
Chapter 9: Value-Returning Functions
Chapter 6: User-Defined Functions I
FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.
FIGURE 4-10 Function Return Statements
Programmazione I a.a. 2017/2018.
User-Defined Functions
Tejalal Choudhary “C Programming from Scratch” Function & its types
FIGURE 4-10 Function Return Statements
User Defined Functions
CSI 121 Structured Programming Language Lecture 13 Functions (Part 1)
Subroutines – parameter passing
FUNCTION CSC128.
FIGURE 4-10 Function Return Statements
Chapter 6: User-Defined Functions I
Chapter 9: Value-Returning Functions
In C Programming Language
Introduction to C++ Programming Language
Functions Imran Rashid CTO at ManiWeber Technologies.
Introduction to Computing Lecture 08: Functions (Part I)
FIGURE 4-10 Function Return Statements
Eizan Aziz CSC128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING
Functions, Part 2 of 42 Topics Functions That Return a Value
CPS125.
Presentation transcript:

FUNCTIONS AND STRUCTURED PROGRAMMING CHAPTER 10

Introduction A c program is composed of at least one function definition, that is the main() function. Execution of the program begins with main() and also ends with the main() function. However, a C program can also be composed of other functions aside from the main().

Consider the program below… Write a program that will print the lines Hi! Hello! How are you? using several functions aside from the main function. #include main() { clrscr( ); printf (“Hi”); greet1( ); greet2( ); getch( ); } greet1 ( ) { continue,…. printf ( “Hello”); } greet2 ( ) { printf (“How are you”); } Sample Output: Hi! Hello! How are you?

The c program presented in previous slide is composed of 3 functions: the main function, the function greet1 and the function greet2. Therefore we can say that we can create a program that is composed of other function aside from the main function. Note: The main( ) function should always be present in every C program.

Functions Defined Functions are the building blocks of C in which all program activity occurs. A function is also called a subprogram or subroutine. It is a part of a C program that performs a task, operation or computation then may return to the calling part of the program. Other functions aside from the main( ) can only be executed by the program through a “function call”. Note: Function call is a C statement that is used to call a function to execute C statements found inside the function body.

Functions Defined Going back to the example, greet1( ); is an example of a function call, calling the function greet ( ). main clrscr------printf greet1( ) function call------greet1( ) function greet2 function call----greet2( ) function getch( )

General form of a function Where function_type specifies the type of value that the function will return. function_name is any valid identifier name which will name the function. Parameter list is a comma separated list of variables that receive the values when the function is called. body of the function is composed of valid c statements that the function will execute. function_type function_name (parameters list) { body of the function; }

General form of a function Example: int subtractor (int x, int y) { int z; z = x – y; return (z); } Function type Function name Parameter list Body of the function Local variable declaration Note: Return statement is a c statement that is used to return a value in a c program.

Type of functions Void Functions – which does not return any value when invoked. Function that returns a value once invoked.

Example of Void Functions #include main() { clrscr( ); printf (“Hi”); greet1( ); greet2( ); getch( ); } greet1 ( ) { continue,…. printf ( “Hello”); } greet2 ( ) { printf (“How are you”); } Sample Output: Hi! Hello! How are you?

Example of Functions that returns a value once invoked. Sample Output: int subtractor (int x, int y) { int z; z = x – y; return (z); } #include main() { clrscr( ); int a,b,; b=4; a=3; a=subtractor(a,b); printf(“%d”, a); }

Actual and Formal Parameters Actual Parameters are the variables found in the function call whose values will be passed to the formal parameters of the called function. Formal Parameters are the variables found in the function header that will receive from the actual parameters.

Actual and Formal Parameters #include main() { int area, x,y; x=25, y=10; area= Compute (x,y); printf(“%d”,area); } Compute (int L, int W) { int a; a=L*W; return a; } x and y are the Actual parameters L and W are the Formal parameters Note: The actual parameter and formal parameter must agree in data type. The actual and formal parameter must have a one-to-one correspondence.

Call by value and Pass by value In the method call by value, the values of the actual parameters are passed to the formal parameters.

Call by value In the method call by value, the values of the actual parameters are passed to the formal parameters. Changes that happen to the values of the formal parameters inside the function will not affect the values of the actual parameters.

Call by value Example: #include main() { clrscr(); x=10; y=5; printf(“%d %d\n”, x,y); pass (x,y); printf(“%d %d\n”,x,y); getch(); } pass (int a, int b) {a=a+5; b=b*2; printf(“%d %d \n”,a,b);} Sample Output:

Pass by value or Call by reference The actual parameters also pass their value to the formal parameters. But the changes that happen to the values of the formal parameters inside the function will affect the values of the actual parameters. This is because the actual address of the variables is passed using the address of operator (&) together with the pointer operator (*).

Pass by value or Call by reference Example: #include main() { clrscr(); x=10; y=5; printf(“%d %d\n”, x,y); pass (&x,&y); printf(“%d %d\n”,x,y); getch(); } pass (int *a, int *b) {*a=*a+5; *b=*b*2; printf(“%d %d \n”,*a,*b);} Sample Output:

sqrt(x) fabs(x)- calculates the absolute value of a number ceil(x)- ceil (11.25)=12 floor(x)- floor(11.25)=11 sin(x) cos(x) tan(x) pow(x)