ES101 - Lesson 7 : Functions with Pass-By-Value

From Embedded Systems Learning Academy
Revision as of 20:00, 10 October 2012 by Preet (talk | contribs) (Created page with "== 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 differenc...")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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.

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");
}


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, 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)
{
    a = 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

int main(void)
{

}