0
0
Cprogramming~5 mins

Function parameters

Choose your learning style9 modes available
Introduction

Function parameters let you send information into a function so it can use that information to do its job.

When you want a function to work with different values each time you call it.
When you want to reuse the same function for many tasks with different inputs.
When you want to organize your code by breaking it into smaller parts that need data.
When you want to make your program easier to read and maintain by avoiding repeated code.
Syntax
C
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.

Examples
A function with one parameter of type char array (string).
C
void greet(char name[]) {
    printf("Hello, %s!\n", name);
}
A function with two integer parameters that returns their sum.
C
int add(int a, int b) {
    return a + b;
}
A function with one integer parameter that prints it.
C
void printNumber(int number) {
    printf("Number: %d\n", number);
}
Sample Program

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.

C
#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;
}
OutputSuccess
Important Notes

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.

Summary

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.