CSI 121 Structured Programming Language Lecture 13 Functions (Part 1)

Slides:



Advertisements
Similar presentations
User Defined Functions
Advertisements

CSE1301 Computer Programming Lecture 4: C Primitives I.
1 CSE1301 Computer Programming Lecture 16 Pointers.
1 CSE1301 Computer Programming: Lecture 19 Character Strings.
1 CSE1301 Computer Programming Lecture 16 Pointers.
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.
Programming With C.
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.
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.
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
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.
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.
Programming Languages -1 (Introduction to C) functions Instructor: M.Fatih AMASYALI
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)
CSCE 206 Structured Programming in C
Programming what is C++
Functions Chapter 5 CS12 - Computer Programming 1 Chapter 5.
Functions and Structured Programming
EPSII 59:006 Spring 2004.
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Functions Dr. Sajib Datta
©2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
CSI 121 Structured Programming Language Lecture 12: Algorithm Design
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
11/10/2018.
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 21 Character Strings
CSI 121 Structured Programming Language Lecture 5: C Primitives 2
Functions, Part 1 of 3 Topics Using Predefined Functions
Functions, Part 2 of 3 Topics Functions That Return a Value
Introduction to C Topics Compilation Using the gcc Compiler
Parameter Passing in Java
Simulating Reference Parameters in C
Chapter 7: User-Defined Functions II
Introduction to Problem Solving and Programming
Introduction to C Topics Compilation Using the gcc Compiler
Functions, Part 1 of 3 Topics Using Predefined Functions
In C Programming Language
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
EECE.2160 ECE Application Programming
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
CPS125.
Scope Rules.
Presentation transcript:

CSI 121 Structured Programming Language Lecture 13 Functions (Part 1) CSE1301 Sem 2, 2003 CSI 121 Structured Programming Language Lecture 13 Functions (Part 1) Lecture 13: Functions Part 1

Topics Functions Parameters Return values

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.

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”

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

Example: hello1.c #include <stdio.h> /* * 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

Function definition Function call Example: hello1.c #include <stdio.h> /* * 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; Function definition Function call

Function name Function body Example: hello1.c #include <stdio.h> /* * 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; Function name Function body

Return type Formal Parameter List Example: hello1.c #include <stdio.h> /* * 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; Return type Formal Parameter List

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.

Parameters (aka Arguments) 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); Parameters (aka Arguments)

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 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); Actual parameters int main(void) { int x = 3, y = 5; badSort ( 10, 9 ); badSort ( y, x+4 ); return 0; }

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.

Example: badswap.c int main(void) /* 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); } int main(void) { int a = 3, b = 5; printf("%d %d\n",a,b); badSwap ( a, b ); return 0; }

Example: badswap.c Output: 3 5 /* 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); } int main(void) { int a = 3, b = 5; printf("%d %d\n",a,b); badSwap ( a, b ); return 0; } Output: 3 5

Example: badswap.c Output: 3 5 5 3 /* 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); } int main(void) { int a = 3, b = 5; printf("%d %d\n",a,b); badSwap ( a, b ); return 0; } Output: 3 5 5 3

Example: badswap.c Output: 3 5 5 3 /* 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); } int main(void) { int a = 3, b = 5; printf("%d %d\n",a,b); badSwap ( a, b ); return 0; } Output: 3 5 5 3

Called function’s environment: Calling function’s environment: 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); } int main(void) { int a = 3, b = 5; printf("%d %d\n",a,b); badSwap ( a, b ); return 0; } Called function’s environment: Calling function’s environment: a: 5 b: 3 a: 3 b: 5

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

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

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

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

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

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

Reading for this lecture CSE1301 Sem 2, 2003 Reading for this lecture Forouzan & Gilberg: Chapter 4 (4.2-4.3) Deitel & Deitel: Chapter 5 (5.1-5.5) Lecture 13: Functions Part 1