17 May 2012

Functions in C (Continued)

If you want to read the previous posts about functions here are the links:
Use of Functions
Coding Functions in 'C'

It is possible to send a list of arguments to a function:

DoCalcs(Num1, Num2, Num3)
The function header must match this list exactly:
Begin DoCalcs(argNum1, argNum2, argNum3)

There is a complication to this process. It is possible to pass the variables By Value or By Reference. There is no need to indicate which method is being used when you write your pseudocode; It is only relevant when you write your 'C code.

If the variable is passed By Value, then the function
only sees a copy of the data. If the data is changed by the function, the original value is preserved in the main program, when control returns there.

char, int, float, variables are automatically passed By Value.


If the variable is passed By Reference, a pointer to the actual data is passed, so that any change made by the function will be reflected in the main program, when control returns there.

string variable is automatically passed By Reference.


Returning Values from Functions


A function can return a single value to the module which called it.

Suppose I want a program to get two numbers from a user, and to determine and return only the smaller number.

More on Functions

A function is a block of code which has a specific purpose. Functions are used to break programs into manageable pieces, so that they are easier to write, test, and maintain.

A function should have a single purpose. It can receive data, and can return a single value.

Passing Arguments to Functions


Functions can be used to break a program into smaller modules for ease of coding and maintenance. However, variables defined in one function are not available for use in others.

This problem can be overcome by passing arguments from one function to another. The 'DrawBox' program.
Begin Program
    Get TopSize   
    DrawTop(TopSize)
    Get SideSize    
    For N= 1 to SideSize        
        DrawSide(TopSize)
    Next
    DrawTop(TopSize)
End

Begin DrawTop(argTopSize)
    For N = 1 to argTopSize         
        Display "*"
    Next
    New Line
End

Begin DrawSide(TopSize)
    Display "*"
    For N = 1 to TopSize - 2
        Display "*"
    Next
    Display "*"
End
The line DrawTop(TopSize) indicates that when the program goes to the DrawTop function, the variable TopSize is passed as an argument for the function to use.

the line Begin DrawTop(argTopSize) indicates that the argument passed to this function is stored in a variable call argTopSize, which can be used by the function. (The same applies to the variable passed to DrawSide)
Begin
    Get Num1
    Get Num2
    Small = GetSmall(Num1, Num2)
    Display "The smaller number was " Small
End
Begin GetSmall(argNum1, argNum2)
    Display "The numbers are " argNum1 argNum2
    Smaller = argNum1
    If argNum1 > argNum2
        Smaller = argNum2
    End If
    Return Smaller
End
if the function returns a value, it must contain a 'return' statement.

Sample Code
#include<stdio.h>

int FindSmaller(int, int);    // function prototype

void main()
{
    int a = 3, b = 6, smaller;

    smaller = FindSmaller(a,b);    // function call

    printf("The smaller number is %d", smaller);
}

int FindSmaller(int arga, int argb) // function header
{
    int small = argb;

    if (arga < argb)
    {
        small = arga;
    }
    return(small);
}
Note: You can create your own header file for your functions. you don't need a prototype in this case. however, you would need a #include"HeaderFileName.h".

No comments:

Post a Comment