3.4. Function Parameters

As a young child, you probably heard Happy Birthday sung to a couple of people, and then you could sing to a new person, say Maria, without needing to hear the whole special version with Maria’s name in it word for word. You had the power of abstraction. With examples like the versions for Emily and Andre, you could figure out what change to make it so the song could be sung to Maria!

Unfortunately, C# is not that smart. It needs explicit rules. If you needed to explain explicitly to someone how Happy Birthday worked in general, rather than just by example, you might say something like this:

First you have to be given a person’s name. Then you sing the song with the person’s name inserted at the end of the third line.

C# works something like that, but with its own syntax. The term “person’s name” serves as a stand-in for the actual data that will be used, “Emily”, “Andre”, or “Maria”. This is just like the association with a variable name in C#. “person’s name” is not a legal C# identifier, so we will use just person as this stand-in. It will be a variable in the program, so it needs a type in C#. The names are strings, so the type of person is string.

The function definition indicates that the variable name person will be used inside the function by inserting it between the parentheses of the definition, preceded by its type. Then in the body of the definition of the function, person is used in place of the real data for any specific person’s name. Read and then run example program birthday4.cs:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
using System;

class Birthday4
{
    static void happyBirthday(string person)
    {   
        Console.WriteLine ("Happy Birthday to you!");
        Console.WriteLine ("Happy Birthday to you!");
        Console.WriteLine ("Happy Birthday, dear " + person + ".");
        Console.WriteLine ("Happy Birthday to you!");
    }
    
    static void Main()
    {   
        happyBirthday("Emily");
        happyBirthday("Andre");
    }

}

In the definition heading for happyBirthday, person is referred to as a parameter, or a formal parameter. This variable name is a placeholder for the real name of the person being sung to.

Main now has two calls to the same function, but between the parentheses, where there was the placeholder person in the definition, now we have the actual people being sung to. The value between the parentheses here in the function call is referred to as an argument or actual parameter of the function call. The argument supplies the actual data to be used in the function execution. When the call is made, C# does this by associating the formal parameter name person with the actual parameter data, as in an assignment statement. In the first call, this actual data is 'Emily'. We say the actual parameter value is passed to the function.

The execution in greater detail:

  1. Lines 13: Execution starts in Main.

  2. Line 15: Call to happyBirthday, with actual parameter 'Emily'.

  3. Line 5: 'Emily' is passed to the function, so person = 'Emily'.

  4. Lines 7-10: The song is printed, with 'Emily' used as the value of person in line 9: printing

    Happy Birthday, dear Emily.
    
  5. End of line 15 after returning from the function call

  6. Line 16: Call to happyBirthday, this time with actual parameter 'Andre'

  7. Line 5: 'Andre' is passed to the function, so person = 'Andre'.

  8. Lines 7-10: The song is printed, with 'Andre' used as the value of person in line 9: printing

    Happy Birthday, dear Andre.
    
  9. End of line 16 after returning from the function call, and the program is over.

The beauty of this system is that the same function definition can be used for a call with a different actual parameter variable, and then have a different effect. The value of the variable person is used in the third line of happyBirthday, to put in whatever actual parameter value was given.

This is the power of abstraction. It is one application of the most important principal in programming. Rather than have a number of separately coded parts with only slight variations, see where it is appropriate to combine them using a function whose parameters refer to the parts that are different in different situations. Then the code is written to be simultaneously appropriate for the separate specific situations, with the substitutions of the right parameter values.

Note

Be sure you completely understand birthday4.cs and the sequence of execution! It illustrates extremely important ideas that many people miss the first time! It is essential to understand the difference between

  1. Defining a function (lines 5-11) with the heading including formal parameter name and type, where the code is merely instructions to be remembered, not acted on immediately.
  2. Calling a function with an actual parameter value to be substituted for the formal parameter, (with no type included!) and have the function code actually run when the instruction containing the call is run. Also note that the function can be called multiple times with different expressions as the actual parameter (line 15 and again in line 16).

We can combine function parameters with user input, and have the program be able to print Happy Birthday for anyone. Check out the main method and run birthday_who.cs:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
using System;

class Birthday_Who
{
   static void happyBirthday(string person)
   {
      Console.WriteLine ("Happy Birthday to you!");
      Console.WriteLine ("Happy Birthday to you!");
      Console.WriteLine ("Happy Birthday, dear " + person + ".");
      Console.WriteLine ("Happy Birthday to you!");
   }

   static void Main()
   {
      string userName;
      Console.WriteLine("Who would you like to sing Happy Birthday to?");
      userName = Console.ReadLine();
      happyBirthday(userName);
   }

}

This last version illustrates several important ideas:

  1. There are more than one way to get information into a function:
    1. Have a value passed in through a parameter (from line 18 to line 5).
    2. Prompt the user, and obtain data from the keyboard (lines 16-17).
  2. It is a good idea to separate the internal processing of data from the external input from the user by the use of distinct functions. Here the user interaction is in main, and the data is manipulated in happyBirthday.
  3. In the first examples of actual parameters, we used literal values. In general an actual parameter can be an expression. The expression is evaluated before it is passed in the function call. One of the simplest expressions is a plain variable name, which is evaluated by replacing it with its associated value. Since it is only the value of the actual parameter that is passed, not any variable name, there is no need to have a variable name used in an actual parameter match a formal parameter name. (Here we have the value of userName in main becoming the value of person in happyBirthday.)

3.4.1. Birthday Function Exercise

Make your own further change to birthday4.cs and save it as birthdayMany.cs: Add a function call (but not another function definition), so Maria gets a verse, in addition to Emily and Andre. Also print a blank line between verses. (There are two ways to handle the blank lines: You may either do this by adding a print line to the function definition, or by adding a print line between all calls to the function. Recall that if you give Console.WriteLine an empty parameter list, it just goes to the next line.)

Table Of Contents

Previous topic

3.3. Multiple Function Definitions

Next topic

3.5. Multiple Function Parameters

This Page