Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Objective-C Basics (Retired) Functional Programming in C Functions

omar ramirez
omar ramirez
226 Points

Order in which a function works.

Why type printf first, and then the math operation we want the program to perform?

1 Answer

Good question, many people also have the same question when they are first learning about functions.

When the computer executes the "printf" statement, it basically goes as follows:

It first looks at what is inside of the "printf" parenthesis, (the () ). When it makes its way to looking at the function name (in this case funky_math), the computer will leap/go to where that function has been implemented (Where you added the code to the body of your function). The computer will continue to follow and run the code that you put in that function until the last line of code in that function. Because funky_math is a function that returns an integer, the last line of code is “return a + b + 343”. Now, the computer because it has looked at all of the lines of code in the function, it leaps back/goes back to where it originally lept from (inside the printf statement, looking at the function name, “funky_math”). Now, because the computer knows that funky_math has to return the number: a + b + 343, the computer will print out to the screen the contents of the printf statement followed by a + b + 343.

You see, in C, the computer does not read the code from top to bottom all the time. when you insert a function name into an area of code (also called “Calling a Function”), it instantly goes to the function and runs all the code from that function and executes what the code says to do before going back to where it had left off.

I hope that this made at least a little sense, the key thing to remember is that when a function is called, the computer will start executing whatever type of code is inside of that function.

Best of Luck in iOS development!