1 CSE1301 Computer Programming Lecture 13 Functions (Part 1)

Slides:



Advertisements
Similar presentations
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Advertisements

Chapter 7: User-Defined Functions II
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
CSE1301 Computer Programming Lecture 4: C Primitives I.
1 CSE1301 Computer Programming Lecture 16 Pointers.
Chapter 6: User-Defined Functions I
Chapter 6: User-Defined Functions I
1 CSE1301 Computer Programming: Lecture 19 Character Strings.
1 Lecture 17:User-Definded function II Introduction to Computer Science Spring 2006.
1 CSE1301 Computer Programming Lecture 16 Pointers.
Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Functions Lecture 4 – Section 2: 9/21/05 Section 4: 9/22/05.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
C Programming Lecture 8-2 : Function (advanced). Recursive Function (recursion) A function that calls itself (in its definition) Classic example : factorial.
1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1) Linda M c Iver.
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.
Functions Manesh T 2 Chapter Topics Define Function Standard (Predefined) Functions User-Defined Functions Parts of functions.
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)
FUNCTIONS AND STRUCTURED PROGRAMMING CHAPTER 10. Introduction A c program is composed of at least one function definition, that is the main() function.
Computer programming Outline Functions [chap 8 – Kochan] –Defining a Function –Arguments and Local Variables Automatic Local.
FUNCTION Dong-Chul Kim BioMeCIS UTA 12/7/
1 Structure of a C Program (continued) Presentation original from Dr. Turner’s class USF - COP C for Engineers Summer 2008.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
Algorithms and Programming Functions Lecture 28. Summary of Previous Lecture while statement for statement break statement Nested loops.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Programming Languages -1 (Introduction to C) functions Instructor: M.Fatih AMASYALI
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Functions Programming Applications. Functions every C program must have a function called main program execution always begins with function main any.
Computer Programming A simple example /* HelloWorld: A simple C program */ #include int main (void) { printf (“Hello world!\n”); return.
1 CSC103: Introduction to Computer and Programming Lecture No 16.
FUNCTIONS (METHODS) Pascal C, C++ Java Scripting Languages Passing by value, reference Void and non-void return types.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
Functions Dr. Sajib Datta Functions A function is a self-contained unit of program code designed to accomplish a particular task. Some functions.
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
1 Functions Part 1 Prototypes Arguments Overloading Return values.
CSCE 206 Structured Programming in C
Programming what is C++
Functions Chapter 5 CS12 - Computer Programming 1 Chapter 5.
Functions and Structured Programming
Functions Dr. Sajib Datta
Chapter 5 Functions DDC 2133 Programming II.
©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
CSI-121 Structured Programming Language Lecture 16 Pointers
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
Buy book Online -
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
CSI-121 Structured Programming Language Lecture 14 Functions (Part 2)
Functions I Creating a programming with small logical units of code.
CSI 121 Structured Programming Language Lecture 13 Functions (Part 1)
Functions, Part 1 of 3 Topics Using Predefined Functions
Functions, Part 2 of 3 Topics Functions That Return a Value
Parameter Passing in Java
Introduction to Problem Solving and Programming
Functions, Part 1 of 3 Topics Using Predefined Functions
CS149D Elements of Computer Science
In C Programming Language
CS150 Introduction to Computer Science 1
Introduction to Computing Lecture 08: Functions (Part I)
ETE 132 Lecture 6 By Munirul Haque.
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
Functions, Part 2 of 3 Topics Functions That Return a Value
Presentation transcript:

1 CSE1301 Computer Programming Lecture 13 Functions (Part 1)

2 Topics Functions Parameters Return values

3 User-Defined Functions Create your own functions, similar to printf() or sqrt() Recall a procedure in an algorithm – a named collection of instructions –InviteToParty –RingUp –MakeToParty A function implements the procedure or function parts of an algorithm

4 Writing User-defined Functions Need to specify: –the name of the function –its parameters –what it returns –block of statements to be carried out when the function is called The block of statements is called the “function body”

5 Prints a simple greeting. procedure sayHello { output “Hello World!” } Main Program { do procedure sayHello } Example: hello1.c

6 #include /* * Print a simple greeting. */ void sayHello ( void ) { printf(“Hello World!\n”); } /* * Call a function which * prints a simple greeting. */ int main(void) { sayHello(); return 0; } Prints a simple greeting. procedure sayHello { output “Hello World!” } Main Program { do procedure sayHello } Example: hello1.c

7 Function definition Function call #include /* * Print a simple greeting. */ void sayHello ( void ) { printf(“Hello World!\n”); } /* * Call a function which * prints a simple greeting. */ int main(void) { sayHello(); return 0; }

8 Example: hello1.c Function name Function body #include /* * Print a simple greeting. */ void sayHello ( void ) { printf(“Hello World!\n”); } /* * Call a function which * prints a simple greeting. */ int main(void) { sayHello(); return 0; }

9 Example: hello1.c Return type Formal Parameter List #include /* * Print a simple greeting. */ void sayHello ( void ) { printf(“Hello World!\n”); } /* * Call a function which * prints a simple greeting. */ int main(void) { sayHello(); return 0; }

10 Parameters Information passed to a function “Formal” parameters are local variables declared in the function declaration “Actual” parameters are values passed to the function when it is called

11 /* Print two numbers in order. */ void badSort ( int a, int b ) { int temp; if ( a > b ) { printf("%d %d\n", b, a); } else { printf("%d %d\n", a, b); } Example: badsort.c Parameters (aka Arguments)

12 Example: badsort.c /* Print two numbers in order. */ void badSort ( int a, int b ) { int temp; if ( a > b ) { printf("%d %d\n", b, a); } else { printf("%d %d\n", a, b); }

13 int main(void) { int x = 3, y = 5; badSort ( 10, 9 ); badSort ( y, x+4 ); return 0; } Example: badsort.c /* Print two numbers in order */ void badSort ( int a, int b ) { int temp; if ( a > b ) { printf("%d %d\n", b, a); } else { printf("%d %d\n", a, b); } Formal parameters Actual parameters

14 Parameters (cont) Parameters are passed by copying the value of the actual parameters to the formal parameters Changes to formal parameters do not affect the value of the actual parameters

15 int main(void) { int a = 3, b = 5; printf("%d %d\n",a,b); badSwap ( a, b ); printf("%d %d\n",a,b); return 0; } Example: badswap.c /* Swap the values of two variables. */ void badSwap ( int a, int b ) { int temp; temp = a; a = b; b = temp; printf("%d %d\n", a, b); }

16 Example: badswap.c Output: 3 5 int main(void) { int a = 3, b = 5; printf("%d %d\n",a,b); badSwap ( a, b ); printf("%d %d\n",a,b); return 0; } /* Swap the values of two variables. */ void badSwap ( int a, int b ) { int temp; temp = a; a = b; b = temp; printf("%d %d\n", a, b); }

17 Example: badswap.c Output: int main(void) { int a = 3, b = 5; printf("%d %d\n",a,b); badSwap ( a, b ); printf("%d %d\n",a,b); return 0; } /* Swap the values of two variables. */ void badSwap ( int a, int b ) { int temp; temp = a; a = b; b = temp; printf("%d %d\n", a, b); }

18 Example: badswap.c Output: int main(void) { int a = 3, b = 5; printf("%d %d\n",a,b); badSwap ( a, b ); printf("%d %d\n",a,b); return 0; } /* Swap the values of two variables. */ void badSwap ( int a, int b ) { int temp; temp = a; a = b; b = temp; printf("%d %d\n", a, b); }

19 Example: badswap.c Calling function’s environment: a: 3 b: 5 Called function’s environment: a: 5 b: 3 int main(void) { int a = 3, b = 5; printf("%d %d\n",a,b); badSwap ( a, b ); printf("%d %d\n",a,b); return 0; } /* Swap the values of two variables. */ void badSwap ( int a, int b ) { int temp; temp = a; a = b; b = temp; printf("%d %d\n", a, b); }

20 Parameters (cont) If a function does not take parameters, declare its formal argument list void void sayHello ( void ) { printf(“Hello World!\n”); } sayHello(); Function call: Declaration:

21 Return Values Values are returned by copying a value specified after the return keyword

22 /* Returns the larger of two numbers. */ int max (int a, int b) { int result; if (a > b) { result = a; } else { result = b; } return result; } Example: max.c Return type

23 /* Returns the larger of two numbers. */ int max (int a, int b) { int result; if (a > b) { result = a; } else { result = b; } return result; } Example: max.c For example: The value of the expression max(7,5) is the integer 7

24 /* Returns the larger of two numbers. */ int max (int a, int b) { int result; if (a > b) { result = a; } else { result = b; } return result; } Example: max.c This style okay.

25 Return Values (cont) If a function does not return a value, declare its return type void void sayHello ( void ) { printf(“Hello World!\n”); } sayHello(); Function call: Declaration:

26 Reading for this lecture King: Chapter 9 ( ) Deitel & Deitel: Chapter 5 ( )