11.1 — Function parameters and arguments

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

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.

11.2 -- Passing arguments by value
10.x -- Chapter 10 comprehensive quiz

62 comments to 11.1 — Function parameters and arguments

  • cjames

    Minor technical issue on this page: the entire lesson appears to be a child element of the list at the beginning, and that causes everything to be indented at the same level as the bullets in the list.

  • yeokaiwei

    Problem is unable to see where are the common mistakes, errors in train of thought (wrong assumptions), where students stop.

    How about setting up accounts to send in answers?
    Each account will have data to see where are the common errors and mistakes.
    Data analytics could mark the papers like TIOBE.
    You can see where they stop and pause for extremely long periods of time ie most likely pointers and arrays. Specifically, passing an array through a function.

  • Ganesh Bhadane

    If we can declared variable inside the function then why we need to declare argument and parameter.

    For example:
    1. Variable declared inside function

    Void func()
    int i

    2. Pass by argument

    Void func(int i)


    I don't understand why we need 2. If 1 is useful then why 2?

    • CereFR

      In 1, you have to know the value of 'i' at compile time.
      In 2, the value of 'i' can be either known at compile time or at runtime.
      If a value is known at compile time, it should not be an argument but stored as a constexpr likely in a header file.

      If you need to experience the above, try writing a function calculating the average gravitational force the Earth will exert on an object sitting on its surface.
      You will need the following variables:
      - G the gravitational constant
      - R the average radius of the Earth
      - M the mass of the Earth
      - m the mass of the object
      The formula is F = GMm/R².

      Of course the function has to work for objects of any mass...

  • Alek

    hey nascardriver I have another ambiguity here,you said that all of the function parameters are created and then the value of the argument is copied into here is what I think that happens:
    1:the function parameters are created and the value of argument is copied into function parameter,this copy costs the same amount of byte as the type that is copied right ?so for an int argument totally 8 bytes is consumed for this process.
    2:if the above reasoning is correct, in case of passing by reference or pointer the same things happen but the difference is that the copy that is passed  is an address.however in passing by address section I asked you some questions and you said the cost of copying address is 8 bytes. don't addresses always occupy 4 bytes ? because they are the value that a pointer holds and pointers are always 4 bytes why did you say it is 8 bytes ?
    thanks in advance!

    • nascardriver

      1: Function parameters are normal variables that are initialized via copy-initialization by the caller.

      2: An integer is usually 4 bytes wide, a pointer 8 bytes. On 32 bit systems, pointers were 4 bytes wide.

  • Viktar

    Dear Alex and nascardriver,

    I want to tell you big thanks for your site, effort and patience to bring us the holy C++ knowledge :)
    I finished reading and now is trying to do first small steps with C++ development. According a task in www I have to use qsort to do sorting of data in memory. As I find out it's not a good idea to use qsort. Anyway I have to use it.

    According qsort specification

    Could you explain please what const void* p1, const void* p2 mean are ? Are these function pointers .... confusing syntax void for function parameter.
    Actually I didn't find any mentions, references or examples in your tutorials... I'd be very thankful.. :) Do you think is it needs to be described in tutorials ?

    And please one more question. I can't understand why qsort with vectors does sorting the only for specified "id" field in custom type? It doesn't sort Worker structures in the current vector... but does sorting of id for the appropriate Workers structures. I supposed to get sorted Worker dataset in the vector.

      ID  Surnmame  Name        Patronymic  Post    h/Pay Hours Salary
      50  Smith       Alex           Test_2      Driver    10     40    400  
      30  Columbus  Christopher Test_1      Seaman  100   720   7200

    After sorting

      ID  Surnmame  Name        Patronymic  Post    h/Pay Hours Salary
      30  Smith       Alex           Test_2      Driver    10     40    400  
      50  Columbus  Christopher Test_1      Seaman  100   720   7200

    • Viktar

      My apologies, just after reading I found ... Void pointers :)

      Anyway, the second question is actual )

      • Viktar

        Please skip and the second question.

        According "The type of the elements of the array must be a TrivialType, otherwise the behavior is undefined."
        In my case each element is not a Trivial Type.

        Thank you.

  • Wallace

    Minor typo: The opening sentence mentions Chapter 1 but all linked sections below are from Chapter 2. I suspect refactoring. :)

  • ryder

    Why this happen?


       The compile okay and programm works.


       Even I put ";" at the end of function:


       The complier still complain about ";".

       My question is what ";" really means? Why it works when the
       function is defined out of the main function, but not within the
       main function?

  • Samira Ferdi

    Hi Alex and Nascardriver! I don't understand the meaning of the term 'copied' in sentence 'the value of the arguments are copied into the parameters'. Could you explain to me? cause it's confuse me.

    • Think of the parameter as a variable. Someone the value for that variable has to come from the caller to the function.
      Think of it like this:

      @i's value was copied to @j. They have the same value, but they're not the same. When you call a function, the same thing happens.

  • Steven

    I'm preparing for an IT exam in University and I'm facing a little problem. In one exercise it ask to write the prototype of the function fz, giving you these details:

    int x;
    double y[100];

    I have to write the prototype based on this last information:

    1.5 + fz(y, y[4], x + y[x]);

    in a way that it is possible.

    My question is: is it possible?
    'cause searching everywhere, I can't find anything that says that is correct to identify a var with (x+y[x]), so I wanted to ask you if it is correct.
    In that case my prototype should be: int fz (int[], int[], int[]); is it correct?

    Thanks for your answer!

    • nascardriver

      Hi Steven!
      y is a double array, so int[] is wrong.
      Judging by the context fz returns a double (1.5 + fz()).
      The first parameter to fz is y, we know y is a double[].
      The second parameter is y[4], since y is a double array every element in y is a double.
      The third parameter is (int + double) which is a double.

    • Alex

      The answer I'd go with would be:
      double fz(double[], double, double)

      y is defined as a double array, so the first parameter is clearly double[].
      y[4] is the type of element 4 in the double array, so the second parameter is clearly double.
      An int plus a double yields a double result, so the third parameter is also a double.

      The return value isn't well defined (it could be anything), but since it's being added to a double, it's most likely a double.

  • Hajra Farooq

    I had to enter a series of even number,but it is not working as it should.

    #include <iostream>
    using namespace std;

    bool number(int x);
    int main()
        int x = 0,i=0;
        while (i!=-1)
            cout << number(x);
            if (number(x) == true)
                cout << "Its even"<<endl;
                cout << "its odd"<<endl;
        return 0;
    bool number(int x = 0)
        int t = 0;

        cout << "Enter Number";
        cin >> x;
            t = x % 2;
            if (t = 0)
                return true;
                return false;

    • Alex

      There are several errors here. First, in function number(), you're using assignment (=) instead of comparison (==). Second, you call number(x) from main() twice per iteration. You should only call it once (stick the result in a variable if need be).

  • Michael

    Hi Alex,
    Just write this down to thank you for putting such a comprehensive and yet easy-to-pick-up tutorial for c++. I have been slacking off for several days since finishing coding the blackjack at the end of Chapter 6. It's super rewarding experience. Now ready to move on!

    Thank you!

  • b1

    excellent job Alex, please continue your great work
    i have a questions:

    1. i heard a lot about (Passing a functions AS parameters to another function)
    but i have never seen an example of it in web
    i imaging it like this (also so you know what i mean)

    is it even valid ?
    if yes, is it safe ?

    2. i want to use a variable from another function

    • Alex

      1) I talk about passing functions as parameters to other functions in the section on function pointers.
      2) There is no way to directly access the x1 from function foo from foo2. This is intentional, as variable x1 is destroyed as soon as function foo() is done. So when foo2() is executing, x1 doesn't even exist at that point.

  • Emmanuel Muniko

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

  • 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

  • Esraa Alkhalaila

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

  • 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!

  • 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?

  • 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).

  • 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??

  • 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.

  • Priscilla

    thanks...this helped alot

  • 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.

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

  • chesslover

    Thanks for the lessons!
    They are amazingly well explained!

  • ender

    This helped thanks

  • 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


      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.....

Leave a Comment

Put all code inside code tags: [code]your code here[/code]