Functions #include int sum(int i, int j); main() { int a, b, res; a = 1; b = 2; res = sum(a, b); printf("%d + %d = %d\n", a, b, res); } int sum(int i,

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

1 Review of Class on Oct Outline of Chapter 4  How to write a function?  Function Prototypes  Function Invocation  Function Definition  The.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 3: Functions.
1 Modularity In “C”. 2 General Syntax data_type function_Name (parameter list) { … return expression; // return output } Body of the function is a compound.
1 Functions and Structured Programming. 2 Structured Programming Structured programming is a problem-solving strategy and a programming methodology. –The.
1 Agenda - Loops while for for & while Nested Loops do-while Misc. & Questions.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters 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.
CECS 121 EXAM 1. /* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
CECS 121 EXAM 2.  Function Prototype Syntax return-type function_name ( arg_type arg1,..., arg_type argN);  Function Prototypes tell you the data type.
Real World Applications: Statistical Measures Problem (page 95-98) Read a series of floating-point numbers from the standard input, and print a statistical.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 2.
CSCI 130 Chapter 5 Functions. Functions are named uniquely Performs a specific task Is independent –should not interfere with other parts of program May.
Real World Applications: Generating Prime Numbers  Problem Write a program that prints all positive prime integers less than or equal to n. A positive.
Opening and Closing Files First define a file pointer, which can "pointing" to one of the opened file. #include... FILE *fp; Use fopen() to open a file,
C Programming Lecture 10 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University.
A First C Program /* Print a Message */ #include main() { printf("This is a test!\n"); } The program is compiled and run as cc pg32.c  a.out  This is.
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
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.
CSCI 171 Presentation 8 Built-in Functions, Preprocessor Directives, and Macros.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
ECE 103 Engineering Programming Chapter 18 Iteration Herbert G. Mayer, PSU CS Status 7/19/2015 Initial content copied verbatim from ECE 103 material developed.
Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all.
UniMAP SemI-09/10EKT120: Computer Programming1 Week 5 – Functions (1)
CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;
Function. Outline Intro. Functions Examples of Functions Prototypes of a Functions Local and Global Variables.
Lecture 05 Functions II, Storage Class, Scope, rand() METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet.
Computer programming Outline Functions [chap 8 – Kochan] –Defining a Function –Arguments and Local Variables Automatic Local.
FUNCTION Dong-Chul Kim BioMeCIS UTA 12/7/
FUNCTIONS. Funtions  The heart of effective problem solving is problem decomposition.  breaking a problem into small, manageable pieces  In C, the.
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.
/* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Arrays as Function Arguments Array can be used as a function argument. E.g., #include int sum(int b[], int n) { int i, res; res = 0; for(i = 0; i < n;
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
1 Unstructured Programming The Need for Procedures/Functions Procedural Programming Function Declaration/Prototypes/Invocation Example Functions Function.
Functions. Motivation What is a function? A function is a self-contained unit of program code designed to accomplish a particular task. We already used.
Chapter 5 Modular Design and Function C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
Pointers and Arrays An array's name is a constant whose value is the address of the array's first element. For this reason, the value of an array's name.
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.
The Cast Operator The cast operator converts explicitly from one data type of an expression to another. For example, if x is of type int, the value of.
Senem Kumova Metin // CS115 // FUNCTIONS CHAPTER 5.
Functions Chapter 5. Function A set of instructions that are designed to perform specific task. A complete and independent program. It is executed by.
Functions Dr. Sajib Datta Functions A function is a self-contained unit of program code designed to accomplish a particular task. Some functions.
NOTE: C programs consist of functions one of which must be main. C programs consist of functions one of which must be main. Every C program begins executing.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Modulus Operator #include main() { int a, b, q, r; printf(" a b a/b a%b\n"); while (scanf("%d%d", &a, &b) != EOF) { q = a / b; /* quotient */ r = a % b;
Program Development and Design Using C++, Third Edition
EKT120: Computer Programming
Topic 6 Recursion.
Suppose we want to print out the word MISSISSIPPI in big letters.
Functions and Structured Programming
Functions Dr. Sajib Datta
Command-Line Arguments
Formatted and Unformatted Input/Output Functions
A function with one argument
EECE.2160 ECE Application Programming
Introduction to C Programming
ECE 103 Engineering Programming Chapter 18 Iteration
Arrays.
CPS125.
Presentation transcript:

Functions #include int sum(int i, int j); main() { int a, b, res; a = 1; b = 2; res = sum(a, b); printf("%d + %d = %d\n", a, b, res); } int sum(int i, int j) { int k; k = i + j; return k; } Prototype declaration Function definition Function parameters Function return value Call/invoke a function Arguments

Function Header The sum function takes two ints and returns an int. int sum(int i, int j) {... } The function echo_line has no parameters and returns a value of type int. int echo_line(void) {... }

Function Header The function print_stars has one parameter vol of type int and returns no value. void print_stars(int vol) {... } The function print_prompt has no parameters and returns no value. void print_prompt(void) {... }

Function Prototype Declaration The function print_stars has one parameter vol of type int and returns no value. Prototype: (prototype ends with a semicolon) void print_stars(int vol); Function definition: void print_stars(int vol) { while(vol-- > 0) putchar('*'); }

Prototype Declaration Function prototype declaration should be made BEFORE the function is used. If the declaration is outside any function, the declaration makes it known to all the functions after the declaration in the file. If the declaration is inside a function A, the declared function B is known only to this function A.

Prototype Declaration Outside a function main() {... } int sum(int a, int b); void prt(int vol) { sum(... );... } float prod(float x, int y) {... } The main() function does not know sum(). It cannot used sum (correctly). The functions following the sum prototype declaration can use sum() function.

Prototype Declaration Inside a Function main() {... } void prt(int vol) { int sum(int a, int b); sum(... );... } float prod(float x, int y) {... } The main() function does not know sum(). It cannot used sum (correctly). Only the prt() function can use sum() function. The sum() function is also not known in prod().

Define Functions You can define your function anywhere in a file. Before main() or after main(), both OK. int sum(int a, int b) { … } main() {... }

Program Structure #include int f1(int a,...); int f2(void); int f3(...); main() {... } int f1(int a,... ) { } main() function call other functions. A function can call any other functions, including itself. Prototypes

Return Statement Return statement causes a termination of the current function and execution goes back to the calling function. Return; return without a value. Return exprn; return the value exprn to the calling function.

How the program is executed when there are functions? #include int sum(int i, int j); main() { int a, b, res; a = 1; b = 2; res = sum(a, b); printf("%d + %d = %d\n", a, b, res); } int sum(int i, int j) { int k; k = i + j; return k; } a b res a, b, res have no defined values just after declaration. a b res 12 a b res i j 1212 res 3 res is set to 3 after the function call returns. Copies are made to new memory locations for the parameter i and j in sum() function. k i j 312 Value 3 is sent back as the value of the expression sum(a,b) in calling function.

Example, Printing a Bar Graph Problem –Print a bar graph that shows the volume of large lakes of the world. The input data are the names of the lakes and the volumes of the lakes in hundreds of cubic miles. Solution –We first print the name of the lake simply by echoing the input to the output a character at a time. The bar graph is created by printing a number of stars equal to the volume of each lake in hundreds of cubic miles.

Sample Input/Output Input data Baikal 58 Superior 54 Tanganyika 45 Nyasa 38 Michigan 26 Huron 21 Output data Baikal ********************************************************** Superior ****************************************************** Tanganyika ********************************************* Nyasa ************************************** Michigan ************************** Huron ********************* Input data consists of a line of name followed by a number.

C implementation without the use of functions #include main() { char c; int i, vol; while( scanf(" %c", &c) != EOF) { do { printf("%c", c); scanf("%c", &c); } while( c != '\n'); printf("\n"); scanf("%d", &vol); for(i = 1; i <= vol; i++) printf("*"); printf("\n\n"); }

Functions and Program Design Use functions to break a big problem into small pieces. Each small problem is solved by a function. For the bar graph problem we can –read and print the lake name by echoing the input. –Read the volume of a lake. –Print the required number of stars.

The main() function #include int echo_line(void); void print_stars(int vol); main() { int value; while(echo_line() != EOF) { scanf("%d", &value); print_stars(value); } return EXIT_SUCCESS; }

Function Definitions int echo_line(void) { char c; if (scanf(" %c", &c)==EOF) return EOF; for( ; ; ) { putchar(c); if(c == '\n') return c; c = getchar(); } void print_stars(int vol) { while(vol-- > 0) putchar('*'); printf("\n\n"); }

Reading/Home Working Read Chapter 5, page 154 to 172. Work on Problems –Section 5.1, page 164, exercise 1, 3, 5, 7, 9. –Section 5.3, page 171, exercise 1, 3. Check your answers in the back of the textbook. Do not hand in.