Function parameters let you send information into a function so it can use that information to do its job.
Function parameters
return_type function_name(type1 param1, type2 param2, ...) {
// function body
}Parameters are listed inside the parentheses after the function name.
Each parameter has a type and a name, separated by a space.
void greet(char name[]) { printf("Hello, %s!\n", name); }
int add(int a, int b) { return a + b; }
void printNumber(int number) { printf("Number: %d\n", number); }
This program defines a function add that takes two numbers and returns their sum. In main, it calls add with 5 and 7 and prints the result.
#include <stdio.h> // Function that adds two numbers and returns the result int add(int x, int y) { return x + y; } int main() { int result = add(5, 7); printf("The sum of 5 and 7 is %d\n", result); return 0; }
Parameters act like placeholders for the values you give when calling the function.
The names of parameters are local to the function and can be different from argument names.
If you don't provide the right number or type of arguments when calling, the program may not work correctly.
Function parameters let you send data into functions to work with.
They are defined by type and name inside the parentheses after the function name.
Using parameters makes your functions flexible and reusable.
Practice
Solution
Step 1: Understand function parameters
Function parameters allow you to pass information into a function so it can use that data.Step 2: Identify the correct purpose
Creating loops inside functions, declaring variables inside the main function, and printing output on the screen describe other tasks, not the role of parameters.Final Answer:
To send data into functions so they can use it -> Option AQuick Check:
Function parameters = send data into functions [OK]
- Confusing parameters with variables declared inside main
- Thinking parameters print output
- Mixing parameters with loop control
Solution
Step 1: Check parameter syntax
Parameters must be separated by commas and each must have a type and name.Step 2: Identify correct syntax
void add(int a, int b); correctly uses commas and types. void add(int a; int b); uses semicolon instead of comma. void add(int a int b); misses comma. void add(int a, b); misses type for second parameter.Final Answer:
void add(int a, int b); -> Option DQuick Check:
Parameters need type and comma [OK]
- Using semicolons instead of commas between parameters
- Omitting type for parameters
- Missing commas between parameters
void printSum(int x, int y) {
printf("%d\n", x + y);
}
int main() {
printSum(3, 4);
return 0;
}Solution
Step 1: Understand function call and parameters
The function printSum receives 3 and 4 as parameters and adds them.Step 2: Calculate the sum and output
3 + 4 equals 7, so printf prints 7 followed by a newline.Final Answer:
7 -> Option AQuick Check:
3 + 4 = 7 [OK]
- Thinking parameters concatenate as strings
- Expecting zero output without return
- Assuming syntax error due to missing includes
int multiply(int a, b) {
return a * b;
}Solution
Step 1: Check parameter declarations
Each parameter must have a type. Here, 'b' lacks a type.Step 2: Confirm other parts are correct
Return type int and function name are valid. Return statement syntax is correct.Final Answer:
Missing type for parameter b -> Option CQuick Check:
Every parameter needs a type [OK]
- Omitting type for second or later parameters
- Assuming return type must be void
- Misreading return statement as error
Solution
Step 1: Understand swapping with pointers
To swap values in C, you pass pointers to the variables so the function can modify original data.Step 2: Identify correct pointer syntax
void swap(int *a, int *b) correctly declares two pointer parameters. void swap(int a, int b) passes by value (won't swap original). void swap(int &a, int &b) uses C++ reference syntax (invalid in C). void swap(int a*, int b*) has incorrect pointer syntax.Final Answer:
void swap(int *a, int *b) -> Option BQuick Check:
Use pointers to modify original variables [OK]
- Passing parameters by value when pointers are needed
- Using C++ reference syntax in C
- Incorrect pointer declaration syntax
Ready to prove it?
Pass the test and this pattern moves unknown
