C
  • Introduction
    • Fundamentals of a Program
    • Overview of C
    • Features of C
  • Installing Required Software
    • Setting Up VSCode for Windows
    • Setting Up VSCode for macOS
    • Setting Up VSCode for Ubuntu
  • Starting to write code
    • Compiling and Running Your Code
    • Creating Our First C Program
    • Errors and Warnings
    • Program: Writing a C Program to Display Your Name
    • Structure of a C Program
  • Basic Concepts
    • Comments in C
    • Preprocessor in C
    • The #include Statement
    • Displaying Output
    • Reading Input from the Terminal
    • Enums and Chars
    • Data Types and Variables
    • Format Specifiers
    • Command Line Arguments
    • Program: Calculating the Area of a Triangle
  • Operators
    • Converting Minutes to Years and Days
    • Basic Operators
    • Bitwise Operators
    • Program: Byte Sizes of Basic Data Types
    • cast and sizeof Operators
    • Operator Precedence
  • Control Flow
    • If-Else Statements
    • Program: Weekly Pay Calculation
    • Switch Statement
    • For Loop
    • While and Do-While Loops
    • Nested Loops and Loop Control
    • Program: Guess the Number
  • Arrays
    • Introduction to Arrays
    • Program: Prime Number Generator
    • Multidimensional Arrays
    • Program: simple Weather Program
    • Variable Length Arrays (VLAs)
  • Functions
    • Overview of Functions
    • Defining Functions
    • Arguments and Parameters
    • Returning Data from Functions
    • Variable Scoping
    • Program: Tic Tac Toe Game
    • Recursion
  • Strings
    • Defining a String
    • Constant Strings in C
    • Common String Functions
    • Program: Bubble Sort
    • Searching, Tokenizing, and Analyzing Strings
    • Converting Strings
  • Debugging
    • What is Debugging
    • Understanding the Call Stack
    • Common C Mistakes
    • Understanding Compiler Errors
  • Pointer
    • Defining Pointers
    • Accessing Pointers
    • Program: Pointer Demonstration
    • Pointers and Const
    • Void Pointers
    • String Pointers
    • Array Pointers
    • Utilizing Pointers with Functions
    • Pointer Arithmetic
  • Dynamic Memory Allocation
    • malloc, calloc, and realloc
    • Program: User Input String
    • Memory Deallocation
  • Structure
    • Structures and Arrays
    • Nested Structures
    • Structures and Pointers
    • Structures and Functions
    • Program: Structure pointers and Functions
  • File Input and Output
    • Accessing Files
    • Reading from a File
    • Program: Finding the Total Number of Lines in a Text File
    • Writing to a Text File
    • Finding Your Position in a File
    • Program: Converting Characters in a File to Uppercase
    • Program: Printing the Contents of a File in Reverse Order
  • The Standard C Library
    • Various Functions in C
    • Math Functions in C
    • Utility Functions in C
Powered by GitBook
On this page
  • Parameters in Function Definition
  • Function with Multiple Parameters
  • Default Values (Not Directly Supported)

Was this helpful?

  1. Functions

Arguments and Parameters

In C programming, functions often utilize parameters to receive input values, and arguments are the actual values passed to the function when it is called. Let's explore the concepts of arguments and parameters in C functions.

Parameters in Function Definition

Parameters are placeholders in a function's definition that represent values to be supplied when the function is called. Here's an example:

#include <stdio.h>

// Function definition with parameters
void greetPerson(char name[]) {
    printf("Hello, %s!\n", name);
}

int main() {
    // Function call with arguments
    greetPerson("Alice");
    greetPerson("Bob");

    return 0;
}

In this example, the greetPerson function is defined with a parameter name. When the function is called, the arguments "Alice" and "Bob" are passed, and the %s format specifier is used to print the names.

Function with Multiple Parameters

Functions can have multiple parameters, allowing them to receive more than one input value. Example:

#include <stdio.h>

// Function definition with multiple parameters
int addNumbers(int a, int b) {
    return a + b;
}

int main() {
    // Function call with arguments
    int result = addNumbers(5, 3);
    printf("Sum: %d\n", result);

    return 0;
}

In this example, the addNumbers function takes two parameters (a and b) and returns their sum. When the function is called with arguments 5 and 3, it computes and prints the sum.

Default Values (Not Directly Supported)

C does not directly support default parameter values. However, you can achieve similar functionality by using function overloading or providing a function with multiple parameter sets.

#include <stdio.h>

// Function definition with default values
int addNumbers(int a, int b) {
    return a + b;
}

int addThreeNumbers(int a, int b, int c) {
    return a + b + c;
}

int main() {
    // Function calls with different parameter sets
    int result1 = addNumbers(5, 3);
    int result2 = addThreeNumbers(2, 4, 6);

    printf("Sum 1: %d\n", result1);
    printf("Sum 2: %d\n", result2);

    return 0;
}

In this example, we have two functions: addNumbers and addThreeNumbers. This mimics the behavior of default values by providing different parameter sets.

Understanding how to work with arguments and parameters is essential for designing flexible and reusable functions in C.

If you have specific questions or if there are additional topics you'd like to explore, feel free to ask!

PreviousDefining FunctionsNextReturning Data from Functions

Was this helpful?