Search

7.1 — Function parameters and arguments

In Chapter 1, we covered function basics in the following sections:

  • 1.4 -- A first look at functions and return values
  • 1.4a -- A first look at function parameters and arguments
  • 1.7 -- Forward declarations and definitions
  • 1.8 -- Programs with multiple files
  • 1.9 -- Header files
  • You should be familiar with the concepts discussed in those lessons before proceeding.

    Parameters vs Arguments

    In the next three lessons, we’ll talk quite a bit about parameters and arguments, so let’s revisit those definitions before proceeding.

    In common usage, the terms parameter and argument are often interchanged. However, for the purposes of further discussion, we will make a distinction between the two:

    A function parameter (sometimes called a formal parameter) is a variable declared in the function declaration:

    An argument (sometimes called an actual parameter) is the value that is passed to the function by the caller:

    When a function is called, all of the parameters of the function are created as variables, and the value of the arguments are copied into the parameters. For example:

    When foo() is called with arguments 6 and 7, foo’s parameter x is created and assigned the value of 6, and foo’s parameter y is created and assigned the value of 7.

    Even though parameters are not declared inside the function block, function parameters have local scope. This means that they are created when the function is invoked, and are destroyed when the function block terminates:

    There are 3 primary methods of passing arguments to functions: pass by value, pass by reference, and pass by address. We’ll look at each of those in the next set of lessons.

    7.2 -- Passing arguments by value
    Index
    6.x -- Chapter 6 comprehensive quiz

37 comments to 7.1 — Function parameters and arguments

  • koorosh

    what are actual argument(actual parameter) and formal argument(formal parameter)?!
    they use in some books!
    what difference between argument and parameter?
    please explain

    • THE TERMINATOR

      Think of it like this: an argument is the giver and the paremeter is the reciever.

    • Shriram M

      Actual parameters are nothing but the actual values/arguments you are passing during function call[ex: foo(6,7);].
      Formal parameters are the parameters into function definition [ex: int foo(int i, int j)]

    • Meghnadh

      Actual parameter is the value or any other input passed to the function by user when function is invoked …..
      Formal parameter is the variable that is declared when function is declared…..

  • ender

    This helped thanks

  • chesslover

    Thanks for the lessons!
    They are amazingly well explained!
    Congratulations!

  • You should specify here that the call of function should be done in the main function 😀

  • Hello,
    Do you have any ideea how could I make the function foo with a variable number of parameters?

    Let’s say that I would like to make it do a sum of n numbers. Those parameters will be insterted from keyboard and can be 2, 3, 4, 5..

    • Alex

      If the user will be entering the numbers, then you’ll probably want to write a function that takes an array of numbers the user entered. The function can step through the array and process each element in whatever fashion it desires (e.g. add them up). In this way, you don’t need a variable number of parameters; Instead, the array is variable length to accommodate the number of user inputs.

  • Priscilla

    thanks…this helped alot

  • Tony

    Hey Alex, I’m reading your guide, but I’m a little worried; Are you going to be updating the rest of the guide anytime soon? I’d like to be caught up with the latest design patterns! Thank you!

    • Alex

      I’m working on it, but I can only write so fast! 🙂

      I’ve been putting dates on all the articles as they get updated, so check back occasionally and re-read the articles again as they get updated.

  • Muhammad Tanzeel Khalid

    8.    When arguments are passed by value, the function works with the original arguments in the calling program.           T /   F
    please reply??

  • kenny7891

    Hi Alex! I have extremely enjoyed your guide and have learned a ton.  In terms of learning the language, is it common to have to refer back to previous lessons? I ask this because I find myself looking back to remember how to do things.  For example, I completely understand the concepts (pointers, for each loops, etc.) but when I want to implement them I don’t remember the exact syntax.  I guess I want to make sure I am okay to continue into future lessons even if I need to look back to remember how to implement certain concepts.  Thanks a bunch!!

    • Alex

      Yes! It’s totally expected. I have to look up the specific syntax for things all the time.

      What’s important is that you understand the concepts themselves, and that when you see a code example, you can appropriately match the syntax to a concept (e.g. if you see an &, you know that this can mean address-of or reference depending on context).

  • Shiva

    Declaration or Definition?

    In the first example and the statement just above it you used the word declaration, where you really talk about definition. Typo?

  • Adib Khazaee

    Hi Alex. Can I declare an enum Variable as a Function parameter? For instance a function with a signature like this:
    int printenumString(enum AAAA)

    ps. So grateful for the amazing tutorial!

  • Esraa Alkhalaila

    how I can treat the inputs passed by arguments???

  • yekyawtun

    Dear Admin,

    I would like to learn about multithreading in visual studio on window 64 bits. Can you give me any suggestion about that topic? Which resources are suitable for me?

    Thank you very much for your support and help,
    Best Regards,
    Ye Kyaw Tun

  • Emmanuel Muniko

    function parameter =variable declared in function declaration
    function argument =value passed to the declared variable
    simple as that

Leave a Comment

Put C++ code inside [code][/code] tags to use the syntax highlighter