Programming is an increasingly important skill, whether you aspire to a career in software development, or in other fields. This course is the first in the specialization Introduction to Programming in C, but its lessons extend to any language you might want to learn. This is because programming is fundamentally about figuring out how to solve a class of problems and writing the algorithm, a clear set of steps to solve any problem in its class. This course will introduce you to a powerful problem-solving process—the Seven Steps—which you can use to solve any programming problem. In this course, you will learn how to develop an algorithm, then progress to reading code and understanding how programming concepts relate to algorithms.

SS

I love the focus of this first course in the specialization is about problem solving and developing a mindset for programming. The course is well thought out and concepts are clearly explained!

AM

Apr 08, 2019

Filled StarFilled StarFilled StarFilled StarFilled Star

Really great course. Took this as a refresher. I feel that the content of this course was significantly higher quality than the programming tuition I received in first semester of university.

从本节课中

Reading Code

In this module, you will learn to read code—this means you will be able to execute a piece of code by hand, and clearly illustrate what each statement does and what the state of the program is. Understanding how to read code is the only way to be sure you can write correct code. By the end of this module, you will be able to read and understand code with functions, conditional statements, iteration, and other fundamental techniques.

教学方

Andrew D. Hilton

Associate Professor of the Practice

Genevieve M. Lipp

Assistant Professor of the Practice

Anne Bracy

Senior Lecturer

脚本

The next piece of C that you are going to learn about is functions. To see why they are so important, let us take a look back at our closestPoint algorithm. There are two places here where this algorithm computes the distance between two points. Why is that important? Well, we need to do the same computation twice. Without functions, we would write the same code twice and that is just in this problem. What if we have other problems that also need to compute the distance between two points? We have a few examples of other problems that would need to compute the distance between two points, but there are many others. We could rewrite the code each time, which may not seem like a big deal for something as small as computing the distance but we want to avoid duplicating code whenever possible. Every time we rewrite code, we run the risk of making mistakes and introducing bugs into our program. Furthermore, it is really boring to rewrite the same code again and again. Instead, we should abstract the computation for distance out into a function of its own. Making distance its own function means we can reuse that algorithm any place we need it without rewriting it. How would this work? Well, our closestPoint function would, in its code, call the distance function. That is, it would ask the distance function to perform its computation. To do this, our closestPoint function would first pass in parameter values specifying which points the distance function should compute on. The distance function would execute its code, doing whatever statements are in it according to the rules you are learning. It finishes when it comes up with an answer which returns back to the function that called it. When it returns, the distance function is done and the calling function continues its own execution, making use of the answer it got from the function it called. So how is this helpful? First, we can reuse this distance computation any time we need. We don't have to rewrite it. We can just call the distance function to compute the distance between any two points. Whether we need to compute the distance at the various places in the closestPoint function or in some other problem we are solving doesn't matter, we just call it. The other important benefit of functions is the abstraction. Abstraction is the separation of the interface, meaning what something does, from its implementation, meaning how it does it. Once we have written the distance function, we can just make use of it without thinking about its inner workings. As you build larger and larger programs, abstraction becomes more and more important. You aren't limited to two functions either. You can have many functions which can call as many other functions as they need to. For example, suppose our closestPoint function were part of a much larger program, which has information about various types of locations and uses that information to give us the nearest location of a particular type. You might end up with something like this. The main function, which is where all C programs start, might call a function to find the store nearest point 42,17, passing in parameters to specify this information. That function might then call another function to find all locations whose type is store. The getByType function could then return back a list of locations for stores which findNearest can then use. It is totally fine for findNearest to then call another function such as our closestPoint function to find the point nearest the location we want. As we previously discussed, closestPoint can call distance to compute the distance between two points and it will return back the distance it computed. Of course, it is totally fine for closestPoint to call distance as many times as it needs, getting back the answer for whatever points it passed in. When closestPoint finishes its computation and figures out its answer, it returns that value to the function that called it which then finishes its computation and returns its answer to the function that called it. Okay, great. Now, you have the high-level concepts of functions. Let's dive into their syntax and semantics in C.