User Defined Functions Lesson 1 CS1313 Spring 2009 1 User Defined Functions 1 Outline 1.User Defined Functions 1 Outline 2.Standard Library Not Enough.

Slides:



Advertisements
Similar presentations
User Defined Functions Lesson 1 CS1313 Fall User Defined Functions 1 Outline 1.User Defined Functions 1 Outline 2.Standard Library Not Enough #1.
Advertisements

Standard I/O Lesson Outline
Standard I/O Lesson CS1313 Spring Standard I/O Lesson Outline 1.Standard I/O Lesson Outline 2.Output via printf 3.Placeholders 4.Placeholders for.
Arithmetic Expressions Lesson #1 CS1313 Spring Arithmetic Expressions Lesson #1 Outline 1.Arithmetic Expressions Lesson #1 Outline 2.A Less Simple.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
While Loop Lesson CS1313 Spring while Loop Outline 1.while Loop Outline 2.while Loop Example #1 3.while Loop Example #2 4.Repetition and Looping.
Functions Most useful programs are much larger than the programs that we have considered so far. To make large programs manageable, programmers modularize.
Chapter 14: Overloading and Templates
1 Introduction to Computers and Programming Quick Review What is a Function? A module of code that performs a specific job.
CS 201 Functions Debzani Deb.
CHAPTER:09 METHODS(FUNCTIONS) IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Idiotproofing Lesson CS1313 Spring Idiotproofing Outline 1.Idiotproofing Outline 2.Idiotproofing 3.Idiotproofing Quotes 4.An Idiotproof Website.
User Defined Functions Lesson 2 CS1313 Spring User Defined Functions 2 Outline 1.User Defined Functions 2 Outline 2.Argument Order When Passing.
For Loop Lesson 3 CS1313 Spring for Loop Lesson 3 Outline 1. for Loop Lesson 3 Outline 2. for Loop with a float Counter: BAD! 3. float Counter Example.
For Loop Lesson 2 CS1313 Spring for Loop Lesson 2 Outline 1. for Loop Lesson 2 Outline 2. for Loop Application 3. Factorial 4. Factorial Program.
Characters & Strings Lesson 2 CS1313 Spring Characters & Strings Lesson 2 Outline 1.Characters & Strings Lesson 2 Outline 2.Character String Declaration.
CS1313: Standard Library Functions Lesson CS1313 Spring Standard Library Functions Outline 1.Standard Library Functions Outline 2.Functions in Mathematics.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
C++ for Engineers and Scientists Third Edition
Array Lesson 1 CS1313 Spring Array Lesson 1 Outline 1.Array Lesson 1 Outline 2.Mean of a List of Numbers 3.mean: Declarations 4.mean: Greeting,
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
Numeric Data Types Lesson CS1313 Spring Numeric Data Types Outline 1.Numeric Data Types Outline 2.Data Types 3.Integers in Mathematics 4.Integers.
Array Lesson 2 CS1313 Spring Array Lesson 2 Outline 1.Array Lesson 2 Outline 2.Reading Array Values Using for Loop #1 3.Reading Array Values Using.
18. DECLARATIONS.
Learners Support Publications Classes and Objects.
For Loop Lesson 1 CS1313 Spring for Loop Lesson 1 Outline 1. for Loop Lesson 1 Outline 2.A while Loop That Counts #1 3.A while Loop That Counts.
CPS120: Introduction to Computer Science Decision Making in Programs.
1 Functions every C++ program must have a function called main program execution always begins with function main any other functions are subprograms and.
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
CPS120: Introduction to Computer Science 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.
CPS120: Introduction to Computer Science Lecture 14 Functions.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Structures Lesson CS1313 Spring Structures Lesson Outline 1.Structures Lesson Outline 2.Beyond Arrays 3.A Company and Its Employees #1 4.A Company.
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
1 CS161 Introduction to Computer Science Topic #9.
Variables & Constants Lesson 2 Outline
Principles of Programming - NI Chapter 6: Function In this chapter, you will learn about Introduction to function User define function Function prototype.
Boolean Data Lesson CS1313 Fall Boolean Data Outline 1.Boolean Data Outline 2.Data Types 3.C Boolean Data Type: char or int 4.C Built-In Boolean.
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.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
CS1313: Arithmetic Expressions Lesson #2 CS1313 Spring Arithmetic Expressions Lesson #2 Outline 1.Arithmetic Expressions Lesson #2 Outline 2.Named.
If Lesson 1 CS1313 Fall if Lesson 1 Outline 1.if Lesson 1 Outline 2.Absolute Value 3.Absolute Value Definition 4.Absolute Value Implementation 5.What.
Idiotproofing Lesson CS1313 Spring Idiotproofing Outline 1.Idiotproofing Outline 2.Idiotproofing 3.Idiotproofing Quotes 4.Idiotproofing Example.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Programming Fundamentals Enumerations and Functions.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Dale Roberts CSCI N305 Functions Declarations Department of Computer and Information Science, School of Science, IUPUI.
Sorting Lesson CS1313 Spring Sorting Lesson Outline 1.Sorting Lesson Outline 2.How to Sort an Array? 3.Many Sorting Algorithms 4.Bubble Sort #1.
ECE 103 Engineering Programming Chapter 30 C Functions Herbert G. Mayer, PSU CS Status 8/9/2014 Initial content copied verbatim from ECE 103 material developed.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Functions.
Chapter 9: Value-Returning Functions
Idiotproofing Outline
Character Strings Lesson Outline
Functions Declarations CSCI 230
Functions Chapter 9 Copyright © 2008 W. W. Norton & Company. 1
Array Lesson 1 Outline Array Lesson 1 Array Lesson 1 Outline
while Loop Outline while Loop Outline while Loop Example #1
User Defined Functions 2 Outline
for Loop Lesson 3 Outline
Classes and Objects.
Variables Lesson Outline
Functions Imran Rashid CTO at ManiWeber Technologies.
Arithmetic Expressions Lesson #1 Outline
Presentation transcript:

User Defined Functions Lesson 1 CS1313 Spring User Defined Functions 1 Outline 1.User Defined Functions 1 Outline 2.Standard Library Not Enough #1 3.Standard Library Not Enough #2 4.Calling a Function Instead 5.Why User-Defined Functions? 6.User-Defined arithmetic_mean 7. arithmetic_mean Flowchart 8.User-Defined Function Properties 9.Declarations Valid in Own Function #1 10.Declarations Valid in Own Function #2 11.Return Type 12.List of Arguments 13.Names of Arguments 14.Array Arguments 15.Local Variables & Named Constants #1 16.Local Variables & Named Constants #2 17.Returning the Return Value #1 18.Returning the Return Value #2 19.Declarations Inside Functions #1 20.Declarations Inside Functions #2 21.Declarations Inside Functions #3 22.General Form of Function Definitions 23.User-Defined Function Example #1 24.User-Defined Function Example #2 25.User-Defined Function Example #3 26.User-Defined Function Example #4 27.User-Defined Function Example #5 28.Another Used-Defined Function #1 29.Another Used-Defined Function #2 30.Another Used-Defined Function #3 31.Another Function Example #1 32.Another Function Example #2 33.Another Function Example #3 34.Function Prototype Declarations #1 35.Function Prototype Declarations #2 36.Actual Arguments & Formal Arguments 37.Actual Arguments 38.Formal Arguments 39.Yet Another Function Example #1 40.Yet Another Function Example #2 41.Yet Another Function Example #3

User Defined Functions Lesson 1 CS1313 Spring Standard Library Not Enough #1 Often, we have a particular kind of value that we need to calculate over and over again, under a variety of circumstances. For example, in PP#5, we have to calculate the arithmetic mean, which is a common function that comes up in a lot of contexts. sum = initial_sum; for (element = first_element; element < number_of_elements; element++) { sum = sum + independent_variable[element]; } /* for game */ independent_variable_arithmetic_mean = sum / number_of_elements;

User Defined Functions Lesson 1 CS1313 Spring Standard Library Not Enough #2 We know that the algorithm for calculating the arithmetic mean is always the same. So why should we have to write the same piece of code over and over and over and over and over? Wouldn’t it be better if we could write that piece of code just once and then reuse it in many applications?

User Defined Functions Lesson 1 CS1313 Spring Calling a Function Instead So, it’d be nice to replace the code sum = initial_sum; for (element = first_element; element < number_of_elements; element++) { sum = sum + independent_variable[element]; } /* for element */ independent_variable_arithmetic_mean = sum / number_of_elements; with calls to a function that would calculate the arithmetic mean for any array: independent_variable_arithmetic_mean = arithmetic_mean(independent_variable, number_of_elements);

User Defined Functions Lesson 1 CS1313 Spring Why User-Defined Functions? independent_variable_arithmetic_mean = arithmetic_mean(independent_variable, number_of_elements); Obviously, the designers of C weren’t able to anticipate the zillion things that we might need functions to do – such as calculate the arithmetic mean – so there are no standard library functions to calculate something that is application- specific. Instead, we as C programmers are going to have to define our own function to do it.

User Defined Functions Lesson 1 CS1313 Spring User-Defined arithmetic_mean float arithmetic_mean (float* array, int number_of_elements) { /* arithmetic_mean */ const float initial_sum = 0.0; const int minimum_number_of_elements = 1; const int first_element = 0; const int program_failure_code = -1; float sum; int element; if (number_of_elements < minimum_number_of_elements) { printf("ERROR: can’t have an array "); printf("of length %d:\n", number_of_elements); printf(" it must have at least %d element.\n", minimum_number_of_elements); exit(program_failure_code); } /* if (number_of_elements <...) */ sum = initial_sum; for (element = first_element; element < number_of_elements; element++) { sum = sum + array[element]; } /* for element */ return sum / number_of_elements; } /* arithmetic_mean */

User Defined Functions Lesson 1 CS1313 Spring arithmetic_mean Flowchart float arithmetic_mean (float* array, int number_of_elements) { /* arithmetic_mean */ const float initial_sum = 0.0; const int minimum_number_of_elements = 1; const int first_element = 0; const int program_failure_code = -1; float sum; int element; if (number_of_elements < minimum_number_of_elements) { printf("ERROR: can’t have an array "); printf("of length %d:\n ", number_of_elements); printf( " it must have at least %d element.\n ", minimum_number_of_elements); exit(program_failure_code); } /* if (number_of_elements <...) */ sum = initial_sum; for (element = first_element; element < number_of_elements; element++) { sum = sum + array[element]; } /* for element */ return sum / number_of_elements; } /* arithmetic_mean */

User Defined Functions Lesson 1 CS1313 Spring User-Defined Function Properties In general, the definition of a user-defined function looks a lot like a program, except for the following things: 1.The function header begins with a return type that is appropriate for that function (for example, int, float, char ). 2.The function has a name that is chosen by the programmer. 3.At the end of the function header is a list of arguments, enclosed in parentheses and separated by commas, each argument preceded by its data type. 4.The function may declare local named constants and local variables. 5.In the body of the function, the return statement tells the function what value to return to the statement that called the function.

User Defined Functions Lesson 1 CS1313 Spring Declarations Valid in Own Function #1 float arithmetic_mean (float* array, int number_of_elements) { /* arithmetic_mean */ const float initial_sum = 0.0; const int minimum_number_of_elements = 1; const int first_element = 0; const int program_failure_code = -1; float sum; int element;... } /* arithmetic_mean */ The compiler treats each function completely independently of the others. Most importantly, the declarations inside a function – including the declarations of its arguments – apply only to that function, not to any others.

User Defined Functions Lesson 1 CS1313 Spring Declarations Valid in Own Function #2 float arithmetic_mean (float* array, int number_of_elements) { /* arithmetic_mean */ const float initial_sum = 0.0; const int minimum_number_of_elements = 1; const int first_element = 0; const int program_failure_code = -1; float sum; int element;... } /* arithmetic_mean */ For example, the declaration of initial_sum in the function arithmetic_mean is visible only to the function arithmetic_mean and not to the main function or to any other function. If another function wants to have the same named constant, it must have its own declaration of that named constant.

User Defined Functions Lesson 1 CS1313 Spring Return Type float arithmetic_mean (float* array, int number_of_elements) { /* arithmetic_mean */... } /* arithmetic_mean */ In the function header, immediately before the name of the function, is a data type. This data type specifies the return type, which is the data type of the value that the function will return. The return type (for now) must be a basic scalar type (for example, int, float, char ). Notice that the return type of the function isn’t declared in the traditional way, but is declared nonetheless.

User Defined Functions Lesson 1 CS1313 Spring List of Arguments float arithmetic_mean (float* array, int number_of_elements) { /* arithmetic_mean */... } /* arithmetic_mean */ At the end of the function header, immediately after the function name, is a list of arguments, enclosed in parentheses and separated by commas, each argument preceded by its data type. Thus, the function’s arguments are declared, but not in the function’s declaration section.

User Defined Functions Lesson 1 CS1313 Spring Names of Arguments float arithmetic_mean (float* array, int number_of_elements) { /* arithmetic_mean */... } /* arithmetic_mean */ The names of these arguments DON’T have to match the names of the arguments that are passed into the function by the main function (or by whatever other function) that calls the function. They should be meaningful with respect to the function in which they occur, not with respect to the other function(s) that call that function.

User Defined Functions Lesson 1 CS1313 Spring Array Arguments float arithmetic_mean (float* array, int number_of_elements) When passing an array argument, you must also pass an argument that represents the length of the array. Not surprisingly, this length argument should be of type int. Also, when passing an array argument, you have two choices about how to express the argument’s data type. The first is above; the second is below: float arithmetic_mean (float array[], int number_of_elements) In CS1313, we prefer * notation to [] notation.

User Defined Functions Lesson 1 CS1313 Spring Local Variables & Named Constants #1 float arithmetic_mean (float* array, int number_of_elements) { /* arithmetic_mean */ const float initial_sum = 0.0; const int minimum_number_of_elements = 1; const int first_element = 0; const int program_failure_code = -1; float sum; int element;... } /* arithmetic_mean */ The function’s declaration section may contain declarations of local named constants and local variables. These names that are valid ONLY within the function that is being defined. On the other hand, these same names can be used with totally different meanings by other functions (and by the calling function).

User Defined Functions Lesson 1 CS1313 Spring Local Variables & Named Constants #2 float arithmetic_mean (float* array, int number_of_elements) { /* arithmetic_mean */ const float initial_sum = 0.0; const int minimum_number_of_elements = 1; const int first_element = 0; const int program_failure_code = -1; float sum; int element;... } /* arithmetic_mean */ Good programming style requires declaring: 1. local named constants, followed by 2. local variables inside the function definition. Note that these declarations should occur in the usual order.

User Defined Functions Lesson 1 CS1313 Spring Returning the Return Value #1 float arithmetic_mean (float* array, int number_of_elements) { /* arithmetic_mean */... sum = initial_sum; for (element = first_element; element < number_of_elements; element++) { sum = sum + array[element]; } /* for element */ return sum / number_of_elements; } /* arithmetic_mean */ In the body of the function, the return statement tells the function to return the return value. If the function does not return a value, then the compiler may get upset. The return value is returned to the statement that called the function, and in some sense “replaces” the function call in the expression where the function call appears.

User Defined Functions Lesson 1 CS1313 Spring Returning the Return Value #2 float arithmetic_mean (float* array, int number_of_elements) { /* arithmetic_mean */... sum = initial_sum; for (element = first_element; element < number_of_elements; element++) { sum = sum + array[element]; } /* for element */ return sum / number_of_elements; } /* arithmetic_mean */ The return value is returned to the statement that called the function, and in some sense “replaces” the function call in the expression where the function call appears. independent_variable_arithmetic_mean = arithmetic_mean(independent_variable, number_of_elements);

User Defined Functions Lesson 1 CS1313 Spring Declarations Inside Functions #1 The following point is EXTREMELY important: For our purposes, the only user-defined identifiers that a given function is aware of – whether it’s the main function or otherwise – are those that are explicitly declared in the function’s declaration section, or in the function’s argument list. (The above statement isn’t literally true, but is true enough for our purposes.)

User Defined Functions Lesson 1 CS1313 Spring Declarations Inside Functions #2 Thus, a function is aware of: 1.its arguments, if any; 2.its local named constants, if any; 3.its local variables, if any; 4.other functions that it has declared prototypes for, if any (described later).

User Defined Functions Lesson 1 CS1313 Spring Declarations Inside Functions #3 The function knows NOTHING AT ALL about variables or named constants declared inside any other function. It isn’t aware that they exist and cannot use them. Therefore, the ONLY way to send information from one function to another is by passing arguments from the calling function to the called function.

User Defined Functions Lesson 1 CS1313 Spring General Form of Function Definitions returntype funcname ( datatype1 arg1, datatype2 arg2,... ) { /* funcname */ const localconst1type localconst1 = localvalue1 ; const localconst2type localconst2 = localvalue2 ;... localvar1type localvar1 ; localvar2type localvar2 ;... [ function body: does stuff ] return returnvalue ; } /* funcname */

User Defined Functions Lesson 1 CS1313 Spring User-Defined Function Example #1 #include int main () { /* main */ const int first_index = 0; const int program_failure_code = -1; const int program_success_code = 0; const int minimum_number_of_elements = 1; float* input_value = (float*)NULL; float input_arithmetic_mean; int number_of_elements, index; float arithmetic_mean(float* array, int number_of_elements); Function prototype

User Defined Functions Lesson 1 CS1313 Spring User-Defined Function Example #2 printf("How many elements are in the array\n"); printf(" (at least %d)?\n", minimum_number_of_elements); scanf("%d", &number_of_elements); if (number_of_elements < minimum_number_of_elements) { printf( "ERROR: There must be at least %d elements\n", minimum_number_of_elements); exit(program_failure_code); } /* if (number_of_elements <...) */

User Defined Functions Lesson 1 CS1313 Spring User-Defined Function Example #3 input_value = (float*)malloc(sizeof(float) * number_of_elements); if (input_value == (float*)NULL) { printf("ERROR: can’t allocate a float array"); printf(" of %d elements.\n", number_of_elements); exit(program_failure_code); } /* if (input_value == (float*)NULL) */

User Defined Functions Lesson 1 CS1313 Spring User-Defined Function Example #4 printf("What are the %d elements?\n", number_of_elements); for (index = first_index; index < number_of_elements; index++) { scanf("%f", &input_value[index]); } /* for index */ input_arithmetic_mean = arithmetic_mean(input_value, number_of_elements); printf("The arithmetic_mean of "); printf("the %d elements is %f.\n", number_of_elements, input_arithmetic_mean); free(input_value); input_value = (float*)NULL; return program_success_code; } /* main */ Function call

User Defined Functions Lesson 1 CS1313 Spring User-Defined Function Example #5 % gcc -o arithmetic_meanfunctestall \ arithmetic_meanfunctestall.c arithmetic_mean.c % arithmetic_meanfunctestall How many elements are in the array (at least 1)? 5 What are the 5 elements? The arithmetic mean of the 5 elements is

User Defined Functions Lesson 1 CS1313 Spring Another Used-Defined Function #1 float cube_root (float base) { /* cube_root */ const float cube_root_power = 1.0 / 3.0; return pow(base, cube_root_power); } /* cube_root */ What can we say about this user-defined function? 1.Its name is cube_root. 2.Its return type is float. 3.It has one argument, base, whose type is float. 4.It has one local named constant, cube_root_power. 5.It has no local variables. 6.It calculates and returns the cube root of the incoming argument.

User Defined Functions Lesson 1 CS1313 Spring Another Used-Defined Function #2 float cube_root (float base) { /* cube_root */ const float cube_root_power = 1.0 / 3.0; return pow(base, cube_root_power); } /* cube_root */ So, cube_root calculates the cube root of a float argument and returns a float result whose value is the cube root of the argument. Notice that cube_root simply calls the C standard library function pow, using a specific named constant for the exponent. We say that cube_root is a wrapper around pow, or more formally that cube_root encapsulates pow.

User Defined Functions Lesson 1 CS1313 Spring Another Used-Defined Function #3 float cube_root (float base) { /* cube_root */ const float cube_root_power = 1.0 / 3.0; return pow(base, cube_root_power); } /* cube_root */ Does the name of a user-defined function have to be meaningful? From the compiler’s perspective, absolutely not; you could easily have a function named square_root that always returns 12. But from the perspective of programmers, that’d be a REALLY REALLY BAD IDEA, and you’d get a VERY BAD GRADE.

User Defined Functions Lesson 1 CS1313 Spring Another Function Example #1 #include int main () { /* main */ const int number_of_inputs = 3; const int program_success_code = 0; float input_value1, cube_root_value1; float input_value2, cube_root_value2; float input_value3, cube_root_value3; float cube_root(float base); printf("What %d real numbers would you\n", number_of_inputs); printf(" like the cube roots of?\n"); scanf("%f %f %f", &input_value1, &input_value2, &input_value3); Function prototype

User Defined Functions Lesson 1 CS1313 Spring Another Function Example #2 cube_root_value1 = cube_root(input_value1); cube_root_value2 = cube_root(input_value2); cube_root_value3 = cube_root(input_value3); printf("The cube root of %f is %f.\n", input_value1, cube_root_value1); printf("The cube root of %f is %f.\n", input_value2, cube_root_value2); printf("The cube root of %f is %f.\n", input_value3, cube_root_value3); return program_success_code; } /* main */ Function calls

User Defined Functions Lesson 1 CS1313 Spring Another Function Example #3 % gcc -o cube_root_scalar \ cube_root_scalar.c cube_root.c -lm % cube_root_scalar What 3 real numbers would you like the cube roots of? The cube root of is The cube root of is The cube root of is

User Defined Functions Lesson 1 CS1313 Spring Function Prototype Declarations #1 #include int main () { /* main */ const int number_of_inputs = 3; const int program_success_code = 0; float input_value1, cube_root_value1; float input_value2, cube_root_value2; float input_value3, cube_root_value3; float cube_root(float base);... } /* main */ Notice this declaration: float cube_root(float base); This declaration is a function prototype declaration.

User Defined Functions Lesson 1 CS1313 Spring Function Prototype Declarations #2 float cube_root(float base); This declaration is a function prototype declaration. The function prototype declaration tells the compiler that there’s a function named cube_root with a return type of float, and that it’s declared external to (outside of) the function that’s calling the cube_root function. You MUST declare prototypes for the functions that you’re calling. Otherwise, the compiler will assume that, by default, the function returns an int and has no arguments. If that turns out not to be the case (that is, most of the time), then the compiler will become ANGRY.

User Defined Functions Lesson 1 CS1313 Spring Actual Arguments & Formal Arguments #include int main () { /* main */... cube_root_value1 = cube_root(input_value1); cube_root_value2 = cube_root(input_value2); cube_root_value3 = cube_root(input_value3);... } /* main */ float cube_root (float base) { /* cube_root */... } /* cube_root */ When we talk about the arguments of a function, we’re actually talking about two very different kinds of arguments: actual arguments and formal arguments.

User Defined Functions Lesson 1 CS1313 Spring Actual Arguments #include int main () { /* main */... cube_root_value1 = cube_root(input_value1); cube_root_value2 = cube_root(input_value2); cube_root_value3 = cube_root(input_value3);... } /* main */ The arguments that appear in the call to the function – for example, input_value1, input_value2 and input_value3 in the program fragment above – are known as actual arguments, because they’re the values that actually get passed to the function. Mnemonic: The aCtual arguments are in the function Call.

User Defined Functions Lesson 1 CS1313 Spring Formal Arguments float cube_root (float base) { /* cube_root */... } /* cube_root */ The arguments that appear in the definition of the function – for example, base, in the function fragment above – are known as formal arguments, because they’re the names that are used in the formal definition of the function. Jargon: Formal arguments are also known as dummy arguments. Mnemonic: The Formal arguments are in the function deFinition.

User Defined Functions Lesson 1 CS1313 Spring Yet Another Function Example #1 #include int main () { /* main */ const int first_input = 0; const int number_of_inputs = 5; const int program_success_code = 0; float input_value[number_of_inputs]; float cube_root_value[number_of_inputs]; int index; float cube_root(float base); printf("What %d real numbers would you\n", number_of_inputs); printf(" like the cube roots of?\n"); for (index = first_input; index < number_of_inputs; index++) { scanf("%f", &input_value[index]); } /* for index */

User Defined Functions Lesson 1 CS1313 Spring Yet Another Function Example #2 for (index = first_input; index < number_of_inputs; index++) { cube_root_value[index] = cube_root(input_value[index]); } /* for index */ for (index = first_input; index < number_of_inputs; index++) { printf("The cube root of %f is %f.\n", input_value[index], cube_root_value[index]); } /* for index */ return program_success_code; } /* main */

User Defined Functions Lesson 1 CS1313 Spring Yet Another Function Example #3 % gcc -o cube_root_array \ cube_root_array.c cube_root.c -lm % cube_root_array What 5 real numbers would you like the cube roots of? The cube root of is The cube root of is The cube root of is The cube root of is The cube root of is