Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.

Slides:



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

Introduction to C Programming
C Language.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Chapter 7: User-Defined Functions II
Kernighan/Ritchie: Kelley/Pohl:
1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
Functions Definition: Instruction block called by name Good design: Each function should perform one task and do it well Functions are the basic building.
1 Modularity In “C”. 2 General Syntax data_type function_Name (parameter list) { … return expression; // return output } Body of the function is a compound.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
1 Functions and Structured Programming. 2 Structured Programming Structured programming is a problem-solving strategy and a programming methodology. –The.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
Topic 4 – Programmer- Defined Functions. CISC 105 – Topic 4 Functions So far, we have only seen programs with one function, main. These programs begin.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
 Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function 
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
Functions g g Data Flow g Scope local global part 4 part 4.
CSCI 130 Scope of Variables Chapter 6. What is scope Parts of program which can access a variable –accessibility –visibility How long variable takes up.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
History of C 1950 – FORTRAN (Formula Translator) 1959 – COBOL (Common Business Oriented Language) 1971 – Pascal Between Ada.
CSCI 130 Chapter 5 Functions. Functions are named uniquely Performs a specific task Is independent –should not interfere with other parts of program May.
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
1 Pointers to structs. 2 A pointer to a struct is used in the same way as a pointer to a simple type, such as an int. Pointers to structs were introduced.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
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 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
1 Functions  A function is a named, independent section of C++ code that performs a specific task and optionally returns a value to the calling program.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions Lecture 12.
CSCI 171 Presentation 6 Functions and Variable Scope.
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.
Pointers *, &, array similarities, functions, sizeof.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
Chapter 6: Function Introduction to function Standard functions User defined functions –function prototype –function definition Function call Storage classes.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
Lecture-3 Functions and Recursion. C Preprocessor Includes header files like stdio.h Expands macros defined Handles conditional compilations PreprocessorProcessor.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
Functions Functions, locals, parameters, and separate compilation.
ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed.
FUNCTIONS (CONT). Midterm questions (21-30) 21. The underscore can be used anywhere in an identifier. 22. The keyword void is a data type in C. 23. Floating.
Functions Chapter 5. Function A set of instructions that are designed to perform specific task. A complete and independent program. It is executed by.
Passing Function Arguments by Reference : A Sample Lesson for CS 1 using the C Programming Language Andy D. Digh Friday, May 29th, 1998.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
1 Functions Part 1 Prototypes Arguments Overloading Return values.
Announcements. Practice questions, with and without solutions will be uploaded by Friday 5 th November, make sure to check them before the weekend \\netstorage\Subjects\ITCA-b\Exam.
User-Written Functions
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
C Programming Tutorial – Part I
Functions and Structured Programming
Functions Dr. Sajib Datta
Pointers and Pointer-Based Strings
Functions in C Mrs. Chitra M. Gaikwad.
Functions Inputs Output
CSI-121 Structured Programming Language Lecture 14 Functions (Part 2)
2011/11/10: Lecture 21 CMSC 104, Section 4 Richard Chang
بنام خدا زبان برنامه نویسی C (21814( Lecture 11 Pointers
Functions, Part 2 of 3 Topics Functions That Return a Value
Simulating Reference Parameters in C
Pointers and Pointer-Based Strings
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
CPS125.
ICS103: Programming in C 6: Pointers and Modular Programming
Presentation transcript:

Functions in C

Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and type Function prototype Input parameter, output parameter Pass by value, pass by reference

Sample Program int a = 1; int findMax(int x, int y); int main(void) { const int j = 10; int k; k = findMax(a, j); return (0); } // End main int findMax(int x, int y) { int z; if (x < y) z = y; else z = x; a = z; return (z); } // End findMax

Identifier Scope (i.e., visibility) int a = 1; int findMax(int x, int y); int main(void) { const int j = 10; int k; k = findMax(a, j); return (0); } // End main int findMax(int x, int y) { int z; if (x < y) z = y; else z = x; a = z; return (z); } // End findMax Global scope for variable a Local scope for j, k Local scope for x, y, z Reading from a global variable Writing to a global variable

Declaring a function tells the compiler the function exists. The parameters tell the order, number, and type of arguments the function expects and the type of the value the function returns. The identifier for each parameter is optional but recommended. A function declaration is also called a function prototype float minValue (float x, float y); Defining a function tells the compiler what the function does float minValue (float x, float y) { if (x < y) return x; else return y; } // End minValue Using (calling) a function tells the compiler to switch control to this function and pass the argument values to the function c = minValue(a,b); Syntax for Function Declaration, Definition, and Use

Example of Declaration, Definition, Use int a = 1; int findMax(int x, int y); int main(void) { const int j = 10; int k; K = findMax(a, j); return (0); } // End main int findMax(int x, int y) { int z; if (x < y) z = y; else z = x; a = z; return (z); } // End findMax Function declaration (prototype) Function definition (and declaration) Function use (and implicit declaration) Function definition (and declaration)

Coding Standards for Functions Function names follow the same naming standard as variables Explicitly declare all programmer-defined functions before defining or using them. This is done by placing all function prototypes just above the definition of the function main If the function main is defined in a source code file, it's definition should occur before the definition of any other functions in the file

Location of Function Parts in a File // Include files #include // Global constants and types // Function prototypes int main (void) { // Function calls return (0); } // End main // Function definitions

Function Parameters and Arguments All arguments in C are "pass by value". This means that the only way to get a value out of a function is by using the return keyword and giving it the value Consequently, a function can never return more than one value and all function parameters are really only input parameters However, "pass by reference" can be implemented in C through the use of pointers (i.e., memory addresses) This allows function parameters to "act like" output parameters

Output Parameter Example #include int swap (int *x, int *y); int main (void) { int a = 27; int b = 56; // swap(a, b); // Wrong swap (&a, &b); // Right return(0); } // End main "int *" is a pointer type, so the type of x or y is a pointer, which is a memory address The * here is NOT an operator in this case. It is a decoration that goes with the type The & is the "address of" operator. It returns the memory address of the variable This is read as "the address of a"

Output Parameter Example (continued) int swap (int *x, int *y) { int temp; temp = *x; *x = *y; *y = temp; return(0); } // End swap "int *" is a pointer type, so the type of x or y is a pointer, which is a memory address The * here is NOT an operator in this case. It is a decoration on the variable name This * here IS an operator. It is called the indirection operator. In the location named temp, store the value pointed to by x In the location pointed to by x, store the value pointed to by y In the location pointed to by y, store the value in temp

Interpreting the & Operator & is the address operator (among other uses in C) When applied to a variable, the & returns the memory address of that variable. That memory address value is then passed as an argument to the function swap (&a, &b); This memory address can be thought of as the address of the "mailbox" with the variable name written on the outside (i.e., this is the "mailbox" of currentAmount) By passing a variable's "mailbox" address as a parameter to a function, the function can then put a value into the "mailbox" This is the first step the programmer needs to do to implement "pass by reference" After the function call is complete, the value of the variable is the value that the function put in the "mailbox"

Interpreting the * Operator * is the indirection operator (among other uses in C) When applied to a variable on the right side of an assignment statement, the * operator returns the value located in the "mailbox" of the memory address contained in the variable. temp = *x; When applied to a variable on the left side of an assignment statement, the * operator stores the value on the right side of the assignment statement into the "mailbox" of the memory address contained in the variable on the left side. *y = temp; If the * indirection operator occurs on both sides of the assignment statement, it means the following for the example below. The value pointed to by y is stored in the memory location pointed to by x *x = *y;

Interpreting the * Operator (continued) In the parameter list of a function, an output parameter is created by declaring the parameter as a pointer to the value that the function will "return" int swap (int *x, int *y); This is the second step the programmer needs to do to implement "pass by reference"

Standard C Library Functions A program can call on a large number of functions from the Standard C library These functions perform essential services such as input and output They also provide efficient implementations of frequently used operations All the functions in the C library are declared in one of the standard header files. One of these files is stdio.h Check out references to the Standard C Library for more information

printf and scanf Functions The printf and scanf functions are declared in the source code file named stdio.h that is located in the standard …\include folder location. Their prototypes are inserted into a program's source code file by using #include The printf and scanf functions are defined in the object code library file named libc.a that is located in the standard …\lib folder location The printf and scanf functions are used by placing them in a program's source code file printf("The value is %d", accountValue); scanf("%f", &salesAmount);