1 Structure of a C Program (continued) Presentation original from Dr. Turner’s class USF - COP-2270 - C for Engineers Summer 2008.

Slides:



Advertisements
Similar presentations
C Functions. What are they? In general, functions are blocks of code that perform a number of pre-defined commands to accomplish something productive.
Advertisements

Spring Semester 2013 Lecture 5
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Classes  All code in a Java program is part of a class  A class has two purposes  Provide functions to do work for the programmer  Represent data.
1 Loops. 2 Often we want to execute a block of code multiple times. Something is always different each time through the block. Typically a variable is.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
CS Data Structures Appendix 1 How to transfer a simple loop- expression to a recursive function (factorial calculation)
 2007 Pearson Education, Inc. All rights reserved C Functions.
 2007 Pearson Education, Inc. All rights reserved C Functions.
Topic 4 – Programmer- Defined Functions. CISC 105 – Topic 4 Functions So far, we have only seen programs with one function, main. These programs begin.
Macro & Function. Function consumes more time When a function is called, the copy of the arguments are passed to the parameters in the function. After.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Programming With C.
EPSII 59:006 Spring Introduction to C More Administrative Details The C Programming Language How a computer processes programs Your first C program.
Functions Manesh T 2 Chapter Topics Define Function Standard (Predefined) Functions User-Defined Functions Parts of functions.
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.
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
1 FUNCTIONS - I Chapter 5 Functions help us write more complex programs.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Dale Roberts CSCI 230 Functions Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI Dale.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
1 Loops II. 2 Recall the while Loop int sum = 0; int i = 1;... /* Sum the integers 1 to 10 */ while ( i
1 CSE1301 Computer Programming Lecture 13 Functions (Part 1)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
1 Unstructured Programming The Need for Procedures/Functions Procedural Programming Function Declaration/Prototypes/Invocation Example Functions Function.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
Dale Roberts CSCI N305 Functions Declarations Department of Computer and Information Science, School of Science, IUPUI.
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Objectives: How to define and call functions. Function declarations and how they differ from function definitions. How arguments are passed to functions.
L071 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program Reading Sections
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
2.1 First C Program. First Program Open visual studio, click new file Save as “programName.c” – Program must start with letter and have no spaces – Must.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Functions.
Programming what is C++
Chapter 6: User-Defined Functions I
C Functions -Continue…-.
Chapter 5 - Functions Outline 5.1 Introduction
Functions.
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
Chapter 5 - Functions Outline 5.1 Introduction
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Scope, Parameter Passing, Storage Specifiers
Functions Declarations CSCI 230
Static Methods 14-Nov-18.
Functions I Creating a programming with small logical units of code.
Chapter 4 void Functions
CSI 121 Structured Programming Language Lecture 13 Functions (Part 1)
Functions, Part 1 of 3 Topics Using Predefined Functions
Chapter 6: User-Defined Functions I
Functions, Part 1 of 3 Topics Using Predefined Functions
Introduction to Computing Lecture 08: Functions (Part I)
Functions, Part 1 of 3 Topics Using Predefined Functions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Functions I Creating a programming with small logical units of code.
Functions, Part 2 of 42 Topics Functions That Return a Value
Parameters and Arguments
Presentation transcript:

1 Structure of a C Program (continued) Presentation original from Dr. Turner’s class USF - COP C for Engineers Summer 2008

2 Functions Our primary tool for coping with complexity. Divide a big program into smaller pieces Each piece performs a well defined action Pieces can be further divided as needed Final result is nothing but small, understandable pieces. “Program Design” 2 USF - COP C for Engineers Summer 2008

Functions Avoid duplicating code. If the same code is needed in more than one place, make it a function. Call the function where that code is needed. 3 USF - COP C for Engineers Summer 2008

4 Functions Two kinds of functions: Compute a value sin(x) Do something printf() Functions that compute a value must have a return type. Functions that "do something" typically are declared as void. 4 USF - COP C for Engineers Summer 2008

5 Functions A function is a named block of code that can be executed by putting its name into a statement. A very simple example (Try it!) void say_hello() { printf ("Hello, world\n"); } int main () { say_hello(); return 0; } Function Header Function Body Function “Call” Function Definition Function Name 5 USF - COP C for Engineers Summer 2008

6 Functions Some things to notice. No semicolon at end of function header. For readability, align the curly brackets with the function header and indent the code within the function body by 4 spaces (The usual rules for alignment of curly brackets.) The function body is delimited by curly brackets. No semicolon at end of function definition. "void" says that the function does not return a value. 6 USF - COP C for Engineers Summer 2008 void say_hello() { printf ("Hello, world\n"); }

7 Returned Value A function can perform a computation and return the result. The result is the “value” of the function. Function call acts like a variable. Can be used in assignment statement. Can be used in an expression. The function header specifies the type of the returned value. 7 USF - COP C for Engineers Summer 2008

8 Function Return A function that returns a value must have a return type. int sum (int p1, int p2) { int result; result = p1 + p2; return result; } This function returns an integer value. 8 USF - COP C for Engineers Summer 2008

9 Function Parameters A function definition can include one or more variables, called parameters, to be supplied by the call. int sum (int p, int p2 ) { int result; result = p1 + p2; return result; } The function call must provide values for function’s parameters. A value passed to the function by the call is referred to as an argument. Each argument becomes the initial value of a parameter when the function is called. Parameters 9 USF - COP C for Engineers Summer 2008

10 Example: A Function that Computes Return to the earlier example in Visual Studio We will modify it to use a function to do the calculation. 10 USF - COP C for Engineers Summer 2008

Example: A Function that Computes a Value #include int sum (int p1, int p2) { int result; result = p1 + p2; return result; } int main() { int a; int b; int c; a = 5; b = 7; c = sum(a,b); printf ("The result is %d\n", c); getchar(); /* Keep the window open. */ return 0; } 11 USF - COP C for Engineers Summer 2008

12 Run It! 12 USF - COP C for Engineers Summer 2008

13 Function Parameters Parameter names are not significant to the call. Argument values are assigned to parameters in order. Parameters look and act like variables inside the function. Initial values provided by the call. Parameter gets a copy of the value in the call. Variables used in the call are not affected by the function. 13 USF - COP C for Engineers Summer 2008

14 Assignment Read Hour 2 and Hour 3 in the textbook. Do the examples from this class for yourself (if you didn’t do them in class) Repeat the examples from this class on Unix. If anything doesn't make sense, ask for help. 14 USF - COP C for Engineers Summer 2008