What are function calls in C?

397 views

What are function calls in C?

In: 1

3 Answers

Anonymous 0 Comments

A function in a programming language is a set of instructions that you assign a name to, which allows you to use it in multiple places without retyping it. It also allows you to alter the input so the function becomes more versatile.

A function call is using that function in the main program.

For instance:

int sum(int x, int y) {

return(x + y);

}

Sum is a function which adds two inputs together.

You could then use this in the main function, which is automatically run when you start a C program.

int main(){

int a = 1, b = 2, c = 3, ans1, ans2;

ans1 = sum(a,b);

ans2 = sum(b,c);

…..

}

If you were to run that program, and display ans1 and ans2, you would get ans1=3 and ans2=5. The function remains the same, but the input is different, so the answer is different.

Think of a function as a formula. The inputs are variables. And a function call solves the formula for the variables given. You can do this to improve code readability, prevent repetitive code, reduce file size, and set up event based functions (e.g. when the user clicks this button, run this function).

You are viewing 1 out of 3 answers, click here to view all answers.