Material Type: Notes;
Class: Introduction to C Programming for Engineers;
Subject: Computer Systems Engineering ;
University: University of Alaska - Anchorage;
Term: Spring 2009;
Download Slides on Function Ordering - Introduction to C Programming for Engineers | CSE A205 and more Study notes Engineering in PDF only on Docsity!
CSE294A
Introduction to C Programming for Engineers
Lecture
Jeffrey Miller, Ph.D.
Functions Review
Functions allow certain pieces of code to execute only when thefunction is called
To create a function, include the return type, the function name, andthe parameter list
To call the function, include the name of the function and theparameters
If you would like to use the returned value, set a variable of the same typeas the return type equal to the function
int my_func(char c, double d) {
// C code
} void main() {
int val, val2; char ch = ‘j’; double doub = 3.3; val =
my_func(‘j’, 3.3);
val2 = my_func(ch, doub);
Function Ordering Code
int add(int value) {
if (value > 10) {
return addOne(value);
} return value + 2;
} int addOne(int value) {
if (value > 10) {
return value + 1;
} return add(value);
How can we place the function declaration above the function call in thisexample?
Function Prototypes
To provide a solution for the previous problem (and torelieve programmers from having to place functionimplementations above the calls to the functions), we havefunction prototypes
A function prototype is merely the function declaration,without the implementation
A function prototype consists of the return type, thefunction name, and the list of parameter types, followed bya semi-colon
It can optionally consist of the names of the parameters as well
(); int addOne(int);
Program
Expand on the program from the previouslecture to also determine the number ofseconds since midnight that has elapsed foreach of the times. Display these values tothe user. Make sure to use functionprototypes.