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.

Slides:



Advertisements
Similar presentations
Spring Semester 2013 Lecture 5
Advertisements

Subroutines – parameter passing passing data to/from a subroutine can be done through the parameters and through the return value of a function subroutine.
Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
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.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
FunctionsFunctions Systems Programming. Systems Programming: Functions 2 Functions   Simple Function Example   Function Prototype and Declaration.
0 Chap. 4 Functions and Program Structure 4.1 Basics of Functions 4.2 Functions Returning Non-integers 4.3 External Variables 4.4 Scope Rules 4.5 Header.
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
Storage & Linkage: Effects on Scope Rudra Dutta CSC Spring 2007, Section 001.
C++ for Engineers and Scientists Third Edition
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
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.
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
C++ for Engineers and Scientists Second Edition Chapter 6 Modularity Using Functions.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
C++ Function 1. Function allow to structure programs in segment of code to perform individual tasks. In C++, a function is a group of statements that.
18. DECLARATIONS.
Learners Support Publications Classes and Objects.
 2007 Pearson Education, Inc. All rights reserved C Functions -Continue…-
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
Engineering Computing I Chapter 4 Functions and Program Structure.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
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.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
KIC/Computer Programming & Problem Solving 1.  Header Files  Storage Classes  Scope Rules  Recursion Outline KIC/Computer Programming & Problem Solving.
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
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.
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.
A First Book of ANSI C Fourth Edition
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.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
C Part 2 Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens The Three Attributes of an Identifier Identifiers have three essential.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
User-Written Functions
Chapter 7: User-Defined Functions II
C Functions -Continue…-.
C++.
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.
The Three Attributes of an Identifier
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
User-Defined Functions
C++ for Engineers and Scientists Second Edition
Chapter 5 - Functions Outline 5.1 Introduction
CS 240 – Lecture 5 Scope of Variables, The Stack, Automatic Variables, Global Variables, Constant Type.
6 Chapter Functions.
Functions and Modular Programming
Classes and Objects.
Presentation transcript:

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 with other functions, or separately and then loaded as desired –Function prototypes must appear in any file where functions are to be used the prototype describes the function’s header (name, return type, number and type of params) – although you can also place these in header files for simplicity –Parameters are always passed by copy this means that the value of the parameter is copied into the parameter in the function’s header, and the parameter then becomes a local variable no values are passed back from the function via the parameter list returning a single value is done by the function’s return returning multiple values must be done by passing pointers (or returning a structure like an array)

Function Format If no return type, it defaults to int (not void) –void is used if the function is to not return any value, otherwise at least one return statement is expected Functions can be compiled separately –So information about the function must be made known explicitly either declare the function, as you would a variable –example: double afunction(int, int); or place a prototype prior to the function that uses it –example: void anotherFunction(int x, int y); Note: prototypes do not need variable names, so it could also be specified as –void anotherFunction(int, int); –this makes the prototype look just like the declaration, the only difference is placement – the prototype goes before the function that calls it, the declaration goes with the var and const declarations inside the function that calls it the prototype approach is more common but either is acceptable return-type name(param list) { var and const declarations executable statements return statement(s) } NOTE: in C, declarations must precede executable statements, you cannot mix them up as you do in Java

Pass By Copy This is the only form of parameter passing in C –The value stored in the parameter in the function call is copied into the parameter in the function header that is, the formal parameter is initialized with the value in the actual parameter from that point forward, the formal parameter is completely independent of the actual parameter changing the formal parameter does nothing to the actual parameter’s value –If you pass a pointer: then the formal parameter is a pointer, pointing at the same location as the actual parameter by using the pointer, you can change the value being pointed to so changing the value that the pointer points to changes the actual parameter changing the formal parameter means that you are changing the pointer, or the memory location being pointed at, not the value being pointed at, so changing the formal parameter does nothing to the actual parameter and will now cause your formal parameter to point somewhere else in memory –this could lead to run-time and/or logical errors! Using parameters appropriately can be very tricky –This is one of the biggest sources of logical errors in C programming but passing pointers can result in all kinds of problem – be very careful!

Example: Swapping As you are aware, swapping 2 values is a common routine – for instance, when sorting Here, we look at the wrong way and the right way to swap two values in a function void swap(int a, int b) { int temp = a; a = b; b = temp; } The wrong way to swap When called with swap(x, y); x and y will remain the same void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } The right way to swap When called with swap(&x, &y); where x and y are int values, then a points to x and b points to y, so the values pointed to are swapped, not the pointers themselves

extern Global variables are available within a file by declaring variables prior to the functions –Global variables are available between files using the reserved word extern to define them The extern declaration appears in the files that are using the externally declared variables, not in the file where they were originally declared –So, if file 1 declares variables and file 2 wants to use those variables, you use an #include statement to load file 1 and extern statements to declare any of file 1’s global variables to be used in file 2 –Take care when using global variables, they are not encouraged and lead to hard-to-read and hard-to-debug code In fact, I would recommend that you avoid them if at all possible! NOTE that extern defines an external variable, but does not declare it, a separate declaration is required although they can be combined into a single statement as in: –extern int x;

Scope Rules Scope for a variable (or a function) are the locations within a program where that variable (or function) can be accessed There are generally two types of scope: local and global –A local variable is defined within the local environment: the current block or the current function Variables declared within { } are local to that block, whether the block is a block within a function or the function itself Variables declared within a for-loop are local to that for-loop Parameters in a function header are local to that function Note that if two variables share the same name but are in different blocks or functions, then the variable declared in this environment will be the one used in a reference –A global variable is defined outside of the local environment and available anywhere within the file, or in other files by using extern

Scope Example with Blocks #include void func1(int, int); void func2(int, int); void main() { int x = 5, y = 10; printf("Values before first function call: %d %d\n", x, y); func1(x, y); { int x = 15, y = 20; printf("Values inside block: %d %d\n", x, y); func2(x, y); } printf("Values before after block: %d %d\n", x, y); } void func1(int x, int y) { x++; y--; printf("Values inside func1: %d %d\n", x, y); } void func2(int x, int y) { x+=2; y-=2; printf("Values inside func2: %d %d\n", x, y); { int x = 0, y = 1; printf("Values inside func2's block: %d %d\n", x, y); }

Header Files In order to call upon functions compiled in separate files, you need to include their definition as a declaration or a prototype –for simplicity, if you have functions in several files, each of which call upon some of the same functions, you can place the prototypes in a single file, called a header file all other shared definitions and declarations can go here as well A header files typically only contain definitions and declarations, not executable code –consider as an example a calculator program that has its’ functions split into multiple files: a main function in one file which calls upon stack operations in a file stack.c a parsing operation to get tokens from a string in the file getop.c a function to get char input in the file getch.c a header file contains prototypes and common declarations called calc.h

Static and Register The reserved word static is used to define a variable as being static, typically used with a variable defined using extern –Static means that the storage of that variable remains in existence Local variables are removed from memory once the function terminates, a static variable remains in memory so that it can be accessed later The register reserved word is used to suggest to the compiler that a particular variable should be moved to and kept in a register –The idea is to give the compiler some advice –The register declaration is only permitted for local variables and parameters and the advice is not necessarily taken by the compiler Just because you place register in front of a variable’s declaration does not mean that the variable will be placed into a register

Macro Substitution We saw #define can be used for constants –Form: #define name substitution-text Example: #define MAX 10 –But unlike a constant in other languages as a variable whose value does not change here the definition is a form of macro substitution –all instances of the name are replaced by the value this can be used in some interesting ways, such as in the following: –#define forever for( ; ; ) /* infinite loop */ –#define max(A, B) ((A) > (B) ? (A) : (B)) /* defines a max operation */ #define is actually a preprocessing directive – that is, an activity to be performed by the compiler prior to attempting compilation

Optional Parameters C permits functions to have optional parameters –The format is returntype name(params, …) That is, the … indicates that further parameters can be passed The … must be listed only after the required parameters –Notice that, since you specify the parameters as …, you do not know their names! How then can you use these additional parameters (if they were passed)? –The stdarg.h file contains the definition of va_list (variable argument list) so that you can step through the optional parameters Declare a variable of type va_list Use the macro va_start which initializes your variable to the first of the optional params The function va_arg returns the next argument –We will skip further coverage of this topic, but if you are interested, consult your C textbook which should talk about it