Functions, Part 2 of 42 Topics Functions That Return a Value

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

Lab 8 User Defined Function.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
CS 201 Functions Debzani Deb.
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
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.
CMSC 1041 Functions II Functions that return a value.
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
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.
CCSA 221 Programming in C CHAPTER 8 – PART 1 WORKING WITH FUNCTIONS 1.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
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.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
User-Defined Functions (cont’d) - Reference Parameters.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
L071 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program Reading Sections
Introduction to C Topics Compilation Using the gcc Compiler
UMBC CMSC 104 – Section 01, Fall 2016
User-Written Functions
CSE 220 – C Programming C Fundamentals.
Functions, Part 2 of 2 Topics Functions That Return a Value
Functions and Structured Programming
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.
Introduction to C Topics Compilation Using the gcc Compiler
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Arrays, Part 1 of 2 Topics Definition of a Data Structure
2011/11/10: Lecture 21 CMSC 104, Section 4 Richard Chang
Functions I Creating a programming with small logical units of code.
Functions, Part 2 of 2 Topics Functions That Return a Value
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
Pointers Call-by-Reference CSCI 230
Introduction to C Topics Compilation Using the gcc Compiler
Creating your first C program
A function with one argument
Arrays, Part 1 of 2 Topics Definition of a Data Structure
UMBC CMSC 104 – Section 01, Fall 2016
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Introduction to C Topics Compilation Using the gcc Compiler
Functions, Part 1 of 3 Topics Using Predefined Functions
Introduction to Problem Solving and Programming
Functions Imran Rashid CTO at ManiWeber Technologies.
More Loops Topics Relational Operators Logical Operators for Loops.
Introduction to Computing Lecture 08: Functions (Part I)
Variables in C Topics Naming Variables Declaring Variables
Functions, Part 1 of 3 Topics Using Predefined Functions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Introduction to C Topics Compilation Using the gcc Compiler
Introduction to C Programming
Functions I Creating a programming with small logical units of code.
Functions, Part 2 of 3 Topics Functions That Return a Value
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Functions, Part 2 of 3 Topics Functions That Return a Value
CPS125.
Corresponds with Chapter 5
Functions that return a value
Presentation transcript:

Functions, Part 2 of 42 Topics Functions That Return a Value Parameter Passing Local Variables Miscellaneous hints Reading Sections 3.1 – 3.6

Functions Can Return Values /**************************************************************************** ** averageTwo - calculates and returns the average of two numbers ** Inputs: num1 - an integer value ** num2 - an integer value ** Outputs: the floating point average of num1 and num2 *****************************************************************************/ float averageTwo (int num1, int num2) { float average ; /* average of the two numbers */ average = (num1 + num2) / 2.0 ; return average ; }

Using averageTwo #include <stdio.h> float averageTwo (int num1, int num2) ; int main ( ) { float ave ; int value1 = 5, value2 = 8 ; ave = averageTwo (value1, value2) ; printf (“The average of %d and %d is %f\n”, value1, value2, ave) ; return 0 ; } float averageTwo (int num1, int num2) float average ; average = (num1 + num2) / 2.0 ; return average ;

Parameter Passing Actual parameters are the parameters that appear in the function call. average = averageTwo (value1, value2) ; Formal parameters are the parameters that appear in the function header. float averageTwo (int num1, int num2) Actual and formal parameters are matched by position. Each formal parameter receives the value of its corresponding actual parameter.

Parameter Passing (con’t) Corresponding actual and formal parameters do not have to have the same name, but they may. Corresponding actual and formal parameters must be of the same data type, with some exceptions.

Local Variables Functions only “see” (have access to) their own local variables. This includes main( ) . Formal parameters are declarations of local variables. The values passed are assigned to those variables. Other local variables can be declared within the function body. There is another type of variable called a global variable. Do not use them! They are a major source of errors!

Parameter Passing and Local Variables #include <stdio.h> float averageTwo (int num1, int num2) float averageTwo (int num1, int num2) ; { int main ( ) float average ; { float ave ; average = (num1 + num2) / 2.0 ; int value1 = 5, value2 = 8 ; return average ; } ave = averageTwo (value1, value2) ; printf (“The average of “) ; printf (“%d and %d is %f\n”, value1, value2, ave) ; return 0 ; value1 value2 ave num1 num2 average 5 8 int int float int int float

Same Name, Still Different Memory Locations #include <stdio.h> float averageTwo (int num1, int num2) float averageTwo (int num1, int num2) ; { int main ( ) float average ; { float average ; average = (num1 + num2) / 2.0 ; int num1 = 5, num2 = 8 ; return average ; } average = averageTwo (num1, num2) ; printf (“The average of “) ; printf (“%d and %d is %f\n”, num1, num2, average) ; return 0 ; num1 num2 average num1 num2 average 5 8 int int float int int float

Changes to Local Variables Do NOT Change Other Variables with the Same Name #include <stdio.h> void addOne (int number) ; void addOne (int num1) { int main ( ) num1++ ; { printf (“In addOne: “) ; int num1 = 5 ; printf (“num1 = %d\n”, num1) ; addOne (num1) ; } printf (“In main: “) ; printf (“num1 = %d\n”, num1) ; num1 return 0 ; } int num1 5 OUTPUT int In addOne: num1 = 6 In main: num1 = 5

Sample Program #include <stdio.h> int funcA(int a, int b, int c ); int main( void ) { int x = 3, y = 4, z = 5; int result; result = funcA( x, y, z ); printf( "result = %d\n", result ); result = funcA( 6 + 4, y + 2, 9 ); result = funcA( 2.1, 3.5, 4.0 ); return 0; }

Sample Program (cont’d) int funcA(int a, int b, int c ) { printf(" a = %d b = %d c = %d\n", a, b, c ); return ( a + b + c ); }

Sample Program Output [burt@linux3 ~]$ a.out a = 3 b = 4 c = 5 result = 12 a = 10 b = 6 c = 9 result = 25 a = 2 b = 3 c = 4 result = 9

Sample Program Analysis - 1 int x = 3, y = 4, z = 5; result = funcA( x, y, z ); printf( "result = %d\n", result ); =================== a = 3 b = 4 c = 5 result = 12 Looks good!

Sample Program Analysis - 2 result = funcA( 6 + 4, y + 2, 9 ); printf( "result = %d\n", result ); =================== a = 10 b = 6 c = 9 result = 25 6 + 4 is 10, OK. y is 4 + 2 is 6, OK.

Sample Program Analysis - 3 result = funcA( 2.1, 3.5, 4.0 ); printf( "result = %d\n", result ); =================== a = 2 b = 3 c = 4 result = 9 Notice that 2.1 became 2, 3.5 became 3 and 4.0 became 4. This is called truncation. The compiler tried to make the data fit the prototype! This is called an implied conversion, and can be the source for errors that is hard to find. Do not use implied conversions!

Another Example #include <stdio.h> #include <stdlib.h> int main( void ) { int negNum = -3; printf( "%d\n", abs( negNum ) * 2 ); return 0; }

Another Example Output [burt@linux3 ~]$ a.out 6 [burt@linux3 ~]$ =================== In this example the expression was the result of another function multiplied by a constant. Whatever the argument is, it is evaluated to the proper data type and then the function is invoked that that final version of the argument.

system( ) function The system( ) function allows you to do any shell command from inside a program. The function must have an argument that is the command you wish to have executed. Must #include <stdlib.h>

system( ) Function Example #include <stdio.h> #include <stdlib.h> int main ( void ) { /* Clear the screen */ system( "clear" ); printf( "Hello, World!\n" ); return 0; }

Good Programming Style Functions should do one thing and do it well! Functions can only return one thing! In CMSC201 and CMSC341, you will learn that it can be a data structure as will as a simple variable. It is better to have more than one function than one complicated function.

Good Programming Style (cont’d) Function subprograms allow us to remove the the main function the code that provides the detailed solutions to a subproblem. Never write the same code twice. Limit your functions to a maximum of 50-100 lines of code and comments. Many functions have only one line of code.

Good Programming Style (cont’d) Forgetting to return a value from a function that is suppose to return a value can lead to unexpected errors. Programs should be written as a collection of small functions. The function prototype, function header and function calls should all agree in the number, type, and order of argument s and parameters, and the type of the return value.