1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)

Slides:



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

User Defined Functions
Chapter Five Functions
C++ Programming: From Problem Analysis to Program Design, Third Edition 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.
Chapter 5 Functions.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 5 Function Basics.
CSE1301 Computer Programming Lecture 4: C Primitives I.
Chapter 6: User-Defined Functions I
COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
Chapter 6: User-Defined Functions I
1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)
1 Lecture 17:User-Definded function II Introduction to Computer Science Spring 2006.
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.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
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.
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.
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)
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.
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 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
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.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
CSCE 206 Structured Programming in C
Programming what is C++
Chapter 6: User-Defined Functions I
Functions Chapter 5 CS12 - Computer Programming 1 Chapter 5.
Functions Dr. Sajib Datta
Chapter 5 Functions DDC 2133 Programming II.
Chapter 5 Function Basics
CSCI 161: Introduction to Programming Function
©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
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
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 3 Topics Functions That Return a Value
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 12 Functions (Part 1)

2 Topics Functions Parameters Return values Reading: D&D (2/e or 3/e) Chapter 5, Sections 5.1 to 5.5

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

4 User-Defined Functions Maths functions return a value: x = sin y = x 3 + 2x 2 + 4x + 6 C functions also sometimes return a value - it’s called the “return value” of the function

5 User-Defined Functions Functions have parameters: x = sin y = √x printf(“hello world”); printf(“a = %d, b = %f\n”,a,b);

6 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”

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

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

9 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() { sayHello(); return 0; }

10 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() { sayHello(); return 0; }

11 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() { sayHello(); return 0; }

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

13 /* 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)

14 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); }

15 int main() { 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

16 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.

17 int main() { 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); }

18 Example: badswap.c Output: 3 5 int main() { 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 Output: int main() { 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 Example: badswap.c Output: int main() { 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); }

21 Example: badswap.c Calling function’s environment: a: 3 b: 5 Called function’s environment: a: 5 b: 3 int main() { 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); }

22 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:

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

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 Return type

25 /* 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.

26 /* 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.

27 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:

28 Next Lecture Reading: D&D (2/e or 3/e) Chapter 5 Sections 5.6 to 5.8 Section 5.12