Fun fun ’til daddy takes your C turbo away! A C program is organized into one or more modules (source files created by a programmer), within which are.

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Introduction to C Programming
C Functions. What are they? In general, functions are blocks of code that perform a number of pre-defined commands to accomplish something productive.
C Language.
Spring Semester 2013 Lecture 5
An introduction to pointers in c
Standard I/O Lesson Outline
Standard I/O Lesson CS1313 Spring Standard I/O Lesson Outline 1.Standard I/O Lesson Outline 2.Output via printf 3.Placeholders 4.Placeholders for.
Computer Programming w/ Eng. Applications
Programming and Data Structure
Lecture 20 Arrays and Strings
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.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Chapter 7: User-Defined Functions II
Lecture 2 Introduction to C Programming
Introduction to C Programming
Introduction to C Programming
Topic 9 – Introduction To Arrays. CISC105 – Topic 9 Introduction to Data Structures Thus far, we have seen “simple” data types. These refers to a single.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
Topic 9A – Arrays as Function Arguments. CISC105 – Topic 9A Arrays as Function Arguments There are two ways to use arrays as function arguments: Use an.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
Topic 8 – Introduction to Pointers and Function Output Parameters.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
1 CS 201 Passing Function as Parameter & Array Debzani Deb.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Guide To UNIX Using Linux Third Edition
Introduction to C Programming
Topic 4 – Programmer- Defined Functions. CISC 105 – Topic 4 Functions So far, we have only seen programs with one function, main. These programs begin.
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
Fortran 1- Basics Chapters 1-2 in your Fortran book.
A First Book of ANSI C Fourth Edition Chapter 6 Modularity Using Functions: Part I.
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.
1 CSC103: Introduction to Computer and Programming Lecture No 6.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
CPS120: Introduction to Computer Science Decision Making in Programs.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Current Assignments Homework 2 is available and is due in three days (June 19th). Project 1 due in 6 days (June 23 rd ) Write a binomial root solver using.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
PHYS 2020 Basic C An introduction to writing simple but useful programs in C In these lectures I will take you through the basics of C, but you will need.
Chapter-4 Managing input and Output operation.  Reading, processing and writing of data are three essential functions of a computer program.  Most programs.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
Modularity using Functions Chapter 4. Modularity In programming blocks of code often can be "called up" and reused whenever necessary, for example code.
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. Midterm questions (1-10) review 1. Every line in a C program should end with a semicolon. 2. In C language lowercase letters are significant.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
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.
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Let’s summarize what we have learnt so far
User-Written Functions
Chapter 7: User-Defined Functions II
UNIT 5 C Pointers.
A bit of C programming Lecture 3 Uli Raich.
User-Defined Functions
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
C Arrays.
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Presentation transcript:

Fun fun ’til daddy takes your C turbo away! A C program is organized into one or more modules (source files created by a programmer), within which are various functions and variables. As a minimum the ‘main’ function must exist. S3-1A.Honey (Nov2003)

Fun fun fun functions A C program is organized into one or more modules (source files created by a programmer), within which are various functions and variables. As a minimum the ‘main’ function must exist. Functions are the C equivalent of subroutines. Functions allow a program to be broken into smaller more manageable and maintainable pieces. S3-2A.Honey (Nov2003)

Fun fun fun functions A C program is organized into one or more modules (source files created by a programmer), within which are various functions and variables. As a minimum the ‘main’ function must exist. Functions are the C equivalent of subroutines. Functions allow a program to be broken into smaller more manageable and maintainable pieces. Each function has a set of input and output requirements (declarations and arguments) that must be adhered to by any program which uses that function. S3-3A.Honey (Nov2003)

Function return type Each function also has a type, which is referred to as the functions ‘return type’. The return type is immediately in front of the function name. If you neglect to give a function a return type then it defaults to ‘int’. S3-4A.Honey (Nov2003)

Function return type Each function also has a type, which is referred to as the functions ‘return type’. The return type is immediately in front of the function name. If you neglect to give a function a return type then it defaults to ‘int’. At least one return statement must exist within the function and, that return statement must specify the ‘value’ to be returned. I use ‘value’ lightly because the function’s type and, therefore, the returned value, need not be a simple C type (integer, float, double, char). The return type can be a very complex ‘type’ created by the programmer. Alternatively, you can specify that a function’s type is ‘void’. In this case a return statement is not necessary but if it exists it must not have a return value. S3-5A.Honey (Nov2003)

Function arguments Every function may have a list of ‘calling arguments’. A function’s arguments appear as a comma separated list of variable declarations, enclosed in parenthesis, immediately following the function’s name. S3-6A.Honey (Nov2003)

Function arguments Every function may have a list of ‘calling arguments’. A function’s arguments appear as a comma separated list of variable declarations, enclosed in parenthesis, immediately following the function’s name. There is an upper limit to the number of arguments that a function may have. This limit is somewhat compiler dependent but typically 10 is the maximum. S3-7A.Honey (Nov2003)

Function arguments Every function may have a list of ‘calling arguments’. A function’s arguments appear as a comma separated list of variable declarations, enclosed in parenthesis, immediately following the function’s name. There is an upper limit to the number of arguments that a function may have. This limit is somewhat compiler dependent but typically 10 is the maximum. However, it is perfectly valid to have a function with no calling arguments. S3-8A.Honey (Nov2003)

Creating a function Let’s create a simple function of type float, which has four floating-point arguments, say c1, c2, c3, and x. The function should return the result of evaluating the polynomial: c1*x*x + c2*x + c3. S3-9A.Honey (Nov2003)

Creating a function S3_pgm1 Let’s create a simple function of type float, which has four floating-point arguments, say c1, c2, c3, and x. The function should return the result of evaluating the polynomial: c1*x*x + c2*x + c3. The function declaration should be similar to: float polyfun1(float c1, float c2, float c3, float x) S3-10A.Honey (Nov2003)

Creating a function S3_pgm1 Let’s create a simple function of type float, which has four floating-point arguments, say c1, c2, c3, and x. The function should return the result of evaluating the polynomial: c1*x*x + c2*x + c3. The function declaration should be similar to: float polyfun1(float c1, float c2, float c3, float x) { return(c1*x*x+c2*x+c3); } Create the main routine and use command line arguments to test your function. S3-11A.Honey (Nov2003)

More functions xtank Let’s build an xtank program. What is an xtank you ask? S3-12A.Honey (Nov2003)

More functions xtank Let’s build an xtank program. What is an xtank you ask? An xtank program is used to control an ‘x’ on the screen. By holding down certain keys the operator can drive the tank (x) around the screen. S3-13A.Honey (Nov2003)

More functions xtank Let’s build an xtank program. What is an xtank you ask? An xtank program is used to control an ‘x’ on the screen. By holding down certain keys the operator can drive the tank (x) around the screen. Assume ‘int joystick()’ exists and returns: 1 if the joystick is pushed forward; 2 for reverse; 3 for left; 4 for right; 0 for stopped, and the previous value is returned if no key is pressed. We will respectively equate these to the ‘f’, ’b’, ’l’, ‘r’, and ‘s’ keys. S3-14A.Honey (Nov2003)

More functions xtank These are the other functions you need: void motor_on() void forward() void reverse() void left() void right() void alloff() In main you must call motor_on() once and then create an infinite loop to repeatedly call joystick(), for input, and, based on the return value, call the appropriate motor control function. Don’t forget to pause for a second each iteration or everything will be too fast for your eyes! S3-15A.Honey (Nov2003)

More functions xtank Notice that we grouped the functions that are similar into modules. For instance motor.c contains all the functions to move the xtank. I am not too concerned if you understand what each of those functions does as their statements are simply relevant to moving around the screen on Win. S3-16A.Honey (Nov2003)

More functions xtank Notice that we grouped the functions that are similar into modules. For instance motor.c contains all the functions to move the xtank. I am not too concerned if you understand what each of those functions does as their statements are simply relevant to moving around the screen on Win. Also notice that we grouped constants, that need to be used in more than one module, into an ‘include file’, namely, tank.h. The modules which need those constants then simply have a preprocessor #include statement. S3-17A.Honey (Nov2003)

More functions xtank Notice that we grouped the functions that are similar into modules. For instance motor.c contains all the functions to move the xtank. I am not too concerned if you understand what each of those functions does as their statements are simply relevant to moving around the screen on Win. Also notice that we grouped constants, that need to be used in more than one module, into an ‘include file’, namely, tank.h. The modules which need those constants then simply have a preprocessor #include statement. The include file also contains the ‘function prototypes’ for each of the functions in the various modules. The prototypes allow the compiler to validate the calling parameters, thereby, protecting the programmers from themselves! S3-18A.Honey (Nov2003)

More functions xtank Now create a function ‘void move( int direction)’ that you call, instead of performing the nested if statements within main. You will essentially put the nested if within move(). S3-19A.Honey (Nov2003)

More functions xtank Now create a function ‘void move( int direction)’ that you call, instead of performing the nested if statements within main. You will essentially put the nested if within move(). The while loop, within main(), can then be simplified to: While (1) { move( joystick() ); sleep( ); } S3-20A.Honey (Nov2003)

Point me too the right location! A ‘pointer’ is used to hold the address of a memory location containing some data. S3-21A.Honey (Nov2003)

Pointers! A ‘pointer’ is used to hold the address of a memory location containing some data. The declaration of a pointer is similar to what we have seen for the argument declarations of main(), namely: * ; Such as: int *ivar; float *fvar; S3-22A.Honey (Nov2003)

Pointers! A ‘pointer’ is used to hold the address of a memory location containing some data. The declaration of a pointer is similar to what we have seen for the argument declarations of main(), namely: * ; Such as: int *ivar; float *fvar; When the compiler encounters a variable, that has been declared as a pointer, the compiler knows that the data is a pointer, or memory address, and can attempt to ensure that it is used correctly. S3-23A.Honey (Nov2003)

Pointers! Assignment to a pointer depends somewhat on the context but in general you must provide the address of another data value. S3-24A.Honey (Nov2003)

Pointers! Assignment to a pointer depends somewhat on the context but in general you must provide the address of another data value. The syntax to do that is identical to the way we passed parameters to the scanf() function, namely, by preceding the variable name by an ampersand. An example of scanf: int ivar; float fvar; scanf( “%d %f”,&ivar, &fvar ); S3-25A.Honey (Nov2003)

Pointers! Assignment to a pointer depends somewhat on the context but in general you must provide the address of another data value. The syntax to do that is identical to the way we passed parameters to the scanf() function, namely, by preceding the variable name by an ampersand. An example of scanf: int ivar; float fvar; scanf( “%d %f”,&ivar, &fvar ); So pointer use would be something like: int *myptr; myptr = &ivar; Thereby, assigning the address of ivar to myptr. S3-26A.Honey (Nov2003)

Pointers! When the actual value of a pointer is needed that pointer must be ‘de-referenced’. S3-27A.Honey (Nov2003)

Pointers! When the actual value of a pointer is needed that pointer must be ‘de-referenced’. The syntax for de-referencing is to simply insert the ‘*’ in front of the variable: int xval = ; int *myptr; myptr = &xval; *myptr = 9876; printf(“myptr=%d, xval=%d\n”,*myptr, xval); S3-28A.Honey (Nov2003)

Pointers! When the actual value of a pointer is needed that pointer must be ‘de-referenced’. The syntax for de-referencing is to simply insert the ‘*’ in front of the variable: int xval = ; int *myptr; myptr = &xval; *myptr = 9876; printf(“myptr=%d, xval=%d\n”,*myptr, xval); The statement before the printf instructs the compiler to put 9876 into the memory location pointed to by myptr. So tell me, what is going to be printed? S3-29A.Honey (Nov2003)

Pointers! When the actual value of a pointer is needed that pointer must be ‘de-referenced’. The syntax for de-referencing is to simply insert the ‘*’ in front of the variable: int xval = ; int *myptr; myptr = &xval; *myptr = 9876; printf(“myptr=%d, xval=%d\n”,*myptr, xval); The statement before the printf instructs the compiler to put 9876 into the memory location pointed to by myptr. So tell me, what is going to be printed? Yep 9876 twice! S3-30A.Honey (Nov2003)

Pointers! One of the primary reasons for pointers is to be able to ‘pass by reference’ parameters into functions, so that those functions can manipulate the data values of variables within the calling functions. This is exactly what we have done with the scanf() function. scanf( “%d”, &ivar ); S3-31A.Honey (Nov2003)

Pointers! One of the primary reasons for pointers is to be able to ‘pass by reference’ parameters into functions, so that those functions can manipulate the data values of variables within the calling functions. This is exactly what we have done with the scanf() function. scanf( “%d”, &ivar ); The above statement passed ivar by reference so that the scanf() function can get an integer value from the user, via the keyboard, and return that value in the variable ivar. Thus scanf will manipulate a data value within the function which used scanf. S3-32A.Honey (Nov2003)

Pointers! S3_pgm2 So let’s create a new program which manipulates our xtank by randomly moving it on the screen. Create a function with the following prototype: void weird_joystick( int *xpos, int *ypos ) The function should call the randomize function twice, once for the x value and once for the y position. S3-33A.Honey (Nov2003)

Pointers! S3_pgm2 So let’s create a new program which manipulates our xtank by randomly moving it on the screen. Create a function with the following prototype: void weird_joystick( int *xpos, int *ypos ) The function should call the randomize function twice, once for the x value and once for the y position. Then create main which calls weird_joystick() to get the desired positions and then use the move function (provided) with the following prototype: void move( int xpos, int ypos ) S3-34A.Honey (Nov2003)

Pointers! S3_pgm2 So let’s create a new program which manipulates our xtank by randomly moving it on the screen. Create a function with the following prototype: void weird_joystick( int *xpos, int *ypos ) The function should call the randomize function twice, once for the x value and once for the y position. Then create main which calls weird_joystick() to get the desired positions and then use the move function (provided) with the following prototype: void move( int xpos, int ypos ) Main should contain an infinite while-loop just as we did for the xtank program. S3-35A.Honey (Nov2003)