How to use an integer variable as a parameter for a function?

function arguments in c
passing arguments to a function in c
pass by reference c
pass by reference c++
call by reference in c
call by value in c
function without argument and with return value in c
call by reference c++

I'm trying to make a piece of code that will count up votes for three different candidates, and I'm using a function that uses the variable name (either A, B or C) as a parameter.

I'm trying to have it so that whenever a vote is counted for that candidate, it will call the function to increase the variable for that candidate by 1. However, with every way I've tried all 3 candidates will always have 0 votes counted, unless I completely remove the function.

I've tried several different ways of making the variables global variables but they all gave the same results.

A = 0
B = 0
C = 0

def after_vote(letter):
    letter = letter + 1
    print("Thank you for your vote.")

def winner(winner_letter, winner_votes):
    print("The winner was", winner_letter, "with", str(winner_votes), "votes.")

while True:
    vote = input("Please vote for either Candidate A, B or C. ").upper()
    if vote == "A":
        after_vote(A)
    elif vote == "B":
        after_vote(B)
    elif vote == "C":
        after_vote(C)
    elif vote == "END":
        print("Cadidate A got", str(A), "votes, Candidate B got", str(B), "votes, and Candidate C got", str(C), "votes.")
        if A > B and A > C:
            winner("A", A)
            break
        elif B > A and B > C:
            winner("B", B)
            break
        elif C > A and C > B:
            winner("C", C)
            break
        else:
            print("There was no clear winner.")
            break
    else:
        print("Please input a valid option.")

Parameter (computer programming), Is it possible to have a function as a parameter in another function? You pass pointers to the variables: void assignvalues(int *v1, int *v2) { *v1 = 7; *v2 = 3; } But, in your snippet, valor1 and valor2 are not global. They are local to main. If they were global, you could just assign to them


In your situation, it is better to define 3 function, one for each candidate:

A = 0
B = 0
C = 0

def after_vote_A():
    global A
    A += 1
    print("Thank you for your vote.")

def after_vote_B():
    global B
    B += 1
    print("Thank you for your vote.")

def after_vote_C():
    global C
    C += 1
    print("Thank you for your vote.")

Don’t forget to use the keyword global or else you define a local variable.

An alternative is to store votes in a dict:

votes = {'A': 0, 'B': 0, 'C': 0}

def after_vote(letter):
    votes[letter] += 1
    print("Thank you for your vote.")

Démonstration:

after_vote('A')
after_vote('B')
after_vote('A')
after_vote('A')
print(votes)

You get:

Thank you for your vote.
Thank you for your vote.
Thank you for your vote.
Thank you for your vote.
{'A': 3, 'B': 1, 'C': 0}

How to pass function pointer as parameter in C?, single simulation, and is changed only when you need to adjust your model behavior. Parameters and Arguments. Information can be passed to functions as a parameter. Parameters act as variables inside the function. Parameters are specified after the function name, inside the parentheses. You can add as many parameters as you want, just separate them with a comma:


The easiest way to do this would be to use a dict, if no two candidates will share the same name.

candidates = {"A":0, "B":0, "C":0}

From there, you could set up a function to look up a value in the dictionary and increment it:

def update_vote(candidate):
    candidates[candidate] += 1

Or heck, you could just update the votes in-place rather than calling a function with:

candidates[candidate] += 1

A much more pythonic way of doing this would be to make each candidate an object that can update its own vote count.

class Candidate(object):
    def __init__(self):
        # This is the "constructor" for the object.
        # It is run whenever an instance of this object is created.
        # We want it to "spawn in" with zero votes.
        self.vote_count = 0
    def update_vote(self):
        self.vote_count += 1

This seems to be closer to what you were getting at in your initial post. You would create an object as follows:

A = Candidate()
B = Candidate()
C = Candidate()

And then, in your main loop, you would just tell each object to update itself:

if vote == "A":
    A.update_vote()

Finally, when you're tallying up the votes, you would just ask each object to give you their "vote" count:

if A.vote_count > B.vote_count and A.vote_count > C.vote_count

Why does a function with no parameters (compared to the actual , Except for functions with variable-length argument lists, the number of arguments Function definition and */ int z[10]; /* array defined before use */ fn(int f1(), int  The variable is created when the function is called or the block is entered and it will be demolished once after existing from block or while the call returns from the function. In the sample program, the variable “customer_age” is a local variable to the function GetAge(). The compiler will generate an error, once we apply the variable customer_age outside GetAge() function. Sample Program – Local Variables. using System; class CustomerEntry {Public void GetAge() {int customer_age=0


You need to pass a mutable data type into function, so that you can modify it. Try wrapping your candidates into a list or dictionary.

candidates = {'A':0, 'B':0, 'C':0}

def after_vote(letter):
    global candidates
    if letter in candidates:
        candidates[letter] += 1
        print("Thank you for your vote.")
    else:
        print("No such candidate: Your vote is invalid.")

Parameters or Variables: What to Use?, The Twice function takes two integer parameters, and multiplies each by 2. Note that the original variables passed into the function from main() are not affected  Here the method uses two parameters, both of type int, named min and max. Then, within the body of the method, these parameters can be used as though they were local variables. The names you use for parameters can be the same as the names you use for the variables you pass to the method when you call it, but they don’t have to be.


Parameters and Arguments, The call by reference method of passing arguments to a function copies the address function swap(), which exchanges the values of the two integer variables  Create a va_list type variable in the function definition. This type is defined in stdarg.h header file. Use int parameter and va_start macro to initialize the va_list variable to an argument list. The macro va_start is defined in stdarg.h header file. Use va_arg macro and va_list variable to access each item in argument list.


Pass By Reference, The call by reference method of passing arguments to a function copies the reference function swap(), which exchanges the values of the two integer variables  The out keyword can also be used with a generic type parameter to specify that the type parameter is covariant. For more information on the use of the out keyword in this context, see out (Generic Modifier) .


Function call by reference in C, C functions must be TYPED (the return type and the type of all parameters specified). Test code for passing by a variable by reference // int main() { int z = 27; zeroIt is a function that takes one argument and returns no value. The function stores the value 0 back into the parameter. x is an int variable that has been declared. Write a statement that sets the value stored in x to zero by invoking the function zeroIt .