Monday 4 April 2011

Inline Functions vs Functions


Inline Functions vs Functions

Functions are pieces of code that when called jump to the address of that code and executes. Inline functions on the other hand are pieces of code that are expanded upon calling it, and then it executes the expanded code. Inline functions are faster than functions, but are only to be used if the inline is small. An inline with 100 line of coding wouldn’t be efficient while an inline with 10 lines would be.

To start an inline it has to outside of the main() like a function. You first define the inline function with “inline” followed by the datatype, the name of the inline, and finally the parameters. It should look something like this:

Inline char toUpperCase(char){

}

Now to test it just code inside of your inline, remember to make the code short and simple.

Inline char toUpperCase(char){
     return ((a >= 'a' && a <= 'z') ? a-('a'-'A') : a );
}

So we’re going to test this program, a simple program that converts lowercase characters into upper case characters:

01. #include <stdio.h> 
02. 
03. inline char toupper( char a ) {    
04.     return ((a >= 'a' && a <= 'z') ? a-('a'-'A') : a ); 
05. }
06.  
07. int main() {
08.     printf("Enter a character: ");
09.     char ch = toupper( getc(stdin) );
10.     printf( "%c\n\n", ch );
11. } 
In line 03 we define the inline, and in line 04 it will execute the code to change  lowercase to uppercase. In line 09 is where the inline is called. This is what actually happens:

01. #include <stdio.h> 
02. 
03. inline char toupper( char a ) {    
04.     return ((a >= 'a' && a <= 'z') ? a-('a'-'A') : a ); 
05. }
06.  
07. int main() {
08.     printf("Enter a character: ");
09.     char ch = inline char toupper( getc(stdin) ) {    
10.        return ((a >= 'a' && a <= 'z') ? a-('a'-'A') : a ); 
11.     }
12. }
13.     printf( "%c\n\n", ch );
14. } 

The inline function is expanded here during compile time making the script run faster. Functions are called and go to the address of the function making the program run slower.

*RULE: To make a inline need to use the keyword “INLINE”. Inline functions are only used for small codes, they aren’t meant for huge blocks of code.

The program and help was found at: Microsoft Inline Help




April Fools!

     On Friday April 1st I ended up waking up late at around 8:30. It usually takes me around an hour to get to school, I knew I was going to be late anyway so I took my time. While waiting for my second bus my friend Shavar texts me saying "We have a test in OOP". I looked at my phone in disbelief. I ended up calling Shavar and was serious about this test. I started to panic as I got on my bus. I ended up calling another friend of mine, Darren Butcher. He told me he wasn't coming to class today because he had to organize his tuition and bills for school. After I told him we had a test he was in shock and started getting ready for school. He lives around an hour away at Seneca@Newnham, it was already 9:40am and class starts in 10 minutes.
   
     I got on the bus and started to cram everything into my head in 10 minutes. I was standing up with a textbook in my hand in a bus so full of people I could barely move. I ended up getting to York at 10:10am and ran to class. I sat beside Shavar as we talked about the test. He looked on the wiki page and saw that out professor, Fardad posted that the test was today only at 9:33am. He had a hunch that the test was fake. Fardad said the test will be after the break. I opened up my laptop and started studying. Crammed as much stuff i could within 50 minutes. When we had the break he said for everyone to leave the room so he can place the tests on our table. His facial expression was so serious that we I thought the test was real. Everyone was huddled outside the door and started talking, we didn't know if the test was serious or not. One of my classmates proposed the test was real as Fardad wouldn't print out 60 sheets of paper for a prank. After Fardad finished distributing the tests he set up a stop time on the projector. He let us in the class shortly after that. He warned us about getting 0 on the test if we opened the test early! We had to wait for people to come back from break. One of my classmates came in and announced he just took a taxi and wasted $35 to get here to write the test. Another classmate ran down to the library and printed around 15 pages of notes. Since everyone was here we could start. The countdown clock started. We all flipped our test papers.... and "APRIL FOOLS". Everyone laughed in relief. That was a very stressful morning!