Difference between revisions of "ES101 - Lesson 7 : Functions with Pass-By-Value"

From Embedded Systems Learning Academy
Jump to: navigation, search
Line 1: Line 1:
 
== Objective ==
 
== Objective ==
The objective of this lesson is to learn how to write functions which lets you enclose and partition portion(s) of your code. You will also learn the differences between local and global variables.
+
The objective of this lesson is to learn how to write functions which lets you partition and re-use portion(s) of your code. You will also learn the differences between local and global variables.
  
 
Functions provide a mean to partition your code into smaller blocks and also reduce repetitive code. So far, you were using '''<code>main()</code>''' which itself is a function. When you call functions from main(), the CPU begins to run the code inside the called function, and once the code inside the function is complete, it returns back to the caller, such as the '''<code>main()</code>''' function.
 
Functions provide a mean to partition your code into smaller blocks and also reduce repetitive code. So far, you were using '''<code>main()</code>''' which itself is a function. When you call functions from main(), the CPU begins to run the code inside the called function, and once the code inside the function is complete, it returns back to the caller, such as the '''<code>main()</code>''' function.
Line 31: Line 31:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
<br/>
+
Note that when you call a function, you simply call it by typing the function name followed by () (brackets).  Now, let's get into more details of functions that perform useful tasks.  See examples below:
Now, let's get into more details of functions that perform useful tasks.  See examples below:
 
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
 
// Function without parameters
 
// Function without parameters
Line 78: Line 77:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
  
 
<BR/>
 
<BR/>
 
== Function Local Variables ==
 
== Function Local Variables ==
In the example above, the variables inside '''<code>main()</code>''' have nothing to do with variables inside '''<code>print_two_numbers</code>'''.  When '''<code>main()</code>''' provides its variables '''a''' and '''b''', these are completely separate variables from '''a''' and '''b''' of '''<code>print_two_numbers()</code>'''.  In the example below, note that the variable '''a''' inside '''<code>main()</code>''' will remain 5 because '''<code>test_function()</code>''' is only settings its own variable called '''a''' to 0.
+
In the example above, the variables inside '''<code>main()</code>''' have nothing to do with variables inside '''<code>print_two_numbers</code>'''.  When '''<code>main()</code>''' provides its variables '''a''' and '''b''', and these are completely separate variables from '''a''' and '''b''' of '''<code>print_two_numbers()</code>'''.  In the example below, note that the variable '''a''' inside '''<code>main()</code>''' will remain 5 because '''<code>test_function()</code>''' is only settings its own variable called '''a''' to 0.
  
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
Line 95: Line 95:
 
void test_function(int a)
 
void test_function(int a)
 
{
 
{
 +
    // Only this function's a is being set to zero.
 
     a = 0;
 
     a = 0;
 +
}
 +
</syntaxhighlight>
 +
 +
 +
<BR/>
 +
== Global Variables ==
 +
Global variables can be accessed by all functions.  To define a global variable, declare it outside the '''<code>main()</code>''' function just like you declare your functions.  It is recommended that your global variable use all capital letters to distinguish it from local variables.
 +
 +
<syntaxhighlight lang="c">
 +
int MY_GLOBAL_VAR = 0;
 +
 +
void test_function();
 +
 +
int main(void)
 +
{
 +
    test_function();
 +
    printf("MY_GLOBAL_VAR = %i\n", MY_GLOBAL_VAR);
 +
}
 +
 +
void test_function()
 +
{
 +
    MY_GLOBAL_VAR = 0;
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 20:41, 10 October 2012

Objective

The objective of this lesson is to learn how to write functions which lets you partition and re-use portion(s) of your code. You will also learn the differences between local and global variables.

Functions provide a mean to partition your code into smaller blocks and also reduce repetitive code. So far, you were using main() which itself is a function. When you call functions from main(), the CPU begins to run the code inside the called function, and once the code inside the function is complete, it returns back to the caller, such as the main() function.

Function Declaration and Definition

Think of functions as modules which perform a specific task. Functions can take zero or more parameters as input but cannot return more than one output. Before we go into detailed, examples, let's learn the basics of how to declare and define functions. Declaring a function is simply telling the compiler how the function signature looks like. Defining a function tells the compiler the code that should be run when the function is called. A function should be declared before your main() and defined after the main().

/* Declare functions before main():
 *   -- void at beginning means no return type
 *  |                         ---- void here means no parameters
 *  |                        |
 *                                    */
 void hello_world_function(void);

void main()
{
    // Call the function 3 times:
    hello_world_function();
    hello_world_function();
    hello_world_function();
}

// Define the function:
// Note: No semicolon after function name
void hello_world_function(void)
{
    printf("Hello World!\n");
}

Note that when you call a function, you simply call it by typing the function name followed by () (brackets). Now, let's get into more details of functions that perform useful tasks. See examples below:

// Function without parameters
void print_hello_world(void);

// Function with two integer parameters
void print_two_numbers(int a, int b);

// Function with two integer parameters that returns the sum of a+b
// Note that return type is changed from "void" to "int"
int print_two_numbers_get_sum(int a, int b);

int main()
{
    print_hello_world();
    print_two_numbers(1, 2);

    // You can also pass two integer variables:
    int a = 1;
    int b = 2;
    int sum = print_two_numbers_get_sum(a, b);
 
    printf("Sum = %i\n", sum);

    return 0;
}


void print_hello_world(void)
{
    printf("Hello World!\n");
}

// Function with two integer parameters
void print_two_numbers(int a, int b)
{
    printf("Numbers = %i and %i\n", a, b);
}

void print_two_numbers_get_sum(int a, int b)
{
    printf("Numbers = %i and %i\n", a, b);
    int sum = (a + b);
    return sum;
}



Function Local Variables

In the example above, the variables inside main() have nothing to do with variables inside print_two_numbers. When main() provides its variables a and b, and these are completely separate variables from a and b of print_two_numbers(). In the example below, note that the variable a inside main() will remain 5 because test_function() is only settings its own variable called a to 0.

void test_function(int a);

int main(void)
{
    int a = 5;
    test_function(a);
    printf("a = %i\n", a);
}

void test_function(int a)
{
    // Only this function's a is being set to zero.
    a = 0;
}



Global Variables

Global variables can be accessed by all functions. To define a global variable, declare it outside the main() function just like you declare your functions. It is recommended that your global variable use all capital letters to distinguish it from local variables.

int MY_GLOBAL_VAR = 0;

void test_function();

int main(void)
{
    test_function();
    printf("MY_GLOBAL_VAR = %i\n", MY_GLOBAL_VAR);
}

void test_function()
{
    MY_GLOBAL_VAR = 0;
}



Assignment

  1. Build a function called "hello_functions" with the following properties:
    • Returns nothing (void)
    • Prints out “Hello, I am using functions!”
  2. Build a function called "cubed" with the following properties:
    • Takes 3 integers as input
    • Returns the product of all the integers back (cubed)
  3. In your main(), call "hello_functions" five times.
  4. Declare 3 variables in main() and get input from the user.
  5. Pass these 3 variables to "cubed" and store the result in a variable
    • Note that function name and variable name cannot be the same.
  6. Print the cubed result to the user.


Sample Code

See the code samples above for sample code hints of this lesson.