Lecture 6: Variables Conversion and Function Introduction
Wednesday, February 15, 2012[Up] [Previous Lecture] [Next Lecture]
Reading Assigned: 3.1-3.4
Homework Due: None
Homework Assigned: Homework 02 -- assigned last class, Homework 03 -- assigned this class
Classwork Assigned: Classwork 04
Topics Covered:
- #define directive
- Fatal errors -- Ran out of time last class
- Reminder - Variable types (and sizes)
- int (4 bytes)
- double (8 bytes) format
- char (1 byte)
- Variable conversion
- Automatic conversion
- Explicit conversion
- Why do we need to use %lf for scanf but %f works for printf?
- Introduction to functions
- Allows us to break up problems into smaller pieces
- Effective programs break problems into the smallest reasonable pieces
- Allows us to write code once and use it multiple times
- Function example:
/* File: func_example.c Name: D. Sheets Username: dsheets Date: 02/14/12 Description: A simple function example */ #include <stdio.h> /* A function to print a line of text */ void my_function(void) { printf("This print is coming from my_function\n"); } /* Main entry point for program */ int main(void) { printf("Getting ready to call a function\n"); my_function(); printf("We called a function!\n"); return(0); }
- Definitions of functions that don't take arguments are made up of of the following elements:
- return type
- function name
- (void)
- {
- Code of function
- }
- Functions that return values should end with "return(value)" where value is a constant or variable
- Functions that don't return values (ie. void) should end with "return()"