lecture_16 - ECE 190 Lecture 16 March 9 2011 Functions in C...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
ECE 190 Lecture 16 March 9, 2011 1 V. Kindratenko Functions in C Lecture Topics Introduction to using functions in C Syntax Examples Memory allocation for variables Lecture materials Textbook §14.1-14.2, 12.5 Homework Machine problem MP3.2 due March 18, 2011 at 5pm submitted electronically. Announcements Exams next week
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
ECE 190 Lecture 16 March 9, 2011 2 V. Kindratenko Introduction to functions in C A function in C is roughly equivalent to a subroutine in LC-3 assembly language It is a segment of code that implements some well-defined function in the program Example from MP3 o Clear board o Setup board o Display board All these are functions that do some well-defined work. The main program then just calls them when needed Using functions enables o Hiding low-level details o Giving high-level structure to the program o Efficiently reusing code Syntax Using functions in C requires: 1. A function prototype , or function’s declaration o Note we did not need one in LC-3 assembly language o Example: int Factorial( int n); o Function prototype specifies three things: Name of the dunction, e.g., sin, cons, printf, Factorial, etc. Types of all arguments that are passed to the function, e.g., int for n in the example above Type of return value o Examples: double cos(double x); <- cos function requires one argument of type double and returns a value of type double int getchar(void); <- reads a character form keyboard and returns its ascii value; does not require any arguments = no input. void clearscreen(void); <- clears screen, takes no arguments, returns nothing. o A function may return no value; in this case its return type is “void” o A function may not require any arguments; in this case its arguments list is declared as “void” o Function prototype must be provided before the function is called in the program 2. Function definition or implementation o Note we needed this in LC-3 assembly as well o This is the actual source code of the function o It includes a formal list of arguments – a list of variables declared and the order in which they are exposed to the user o Example
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 7

lecture_16 - ECE 190 Lecture 16 March 9 2011 Functions in C...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online