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
  • Introduction
  • Function Declaration
  • Example Function
  • Function Call
  • Return Statement
  • Function Prototypes
  • Function Parameters
  • Void Functions
  • Scope of Variables

Was this helpful?

  1. Functions

Overview of Functions

Introduction

Functions are an essential concept in C programming, allowing you to break down a program into smaller, manageable pieces of code. A function is a self-contained block of code that performs a specific task. Understanding the basics of functions is crucial for writing modular and readable C code.

Function Declaration

The syntax for declaring a function in C is as follows:

returnType functionName(parameters) {
    // Function body
}
  • returnType: Specifies the type of data the function will return (e.g., int, void).

  • functionName: The name of the function.

  • parameters: Input values that the function accepts (optional).

Example Function

#include <stdio.h>

// Function declaration
int add(int a, int b);

int main() {
    // Function call
    int result = add(3, 5);

    // Display the result
    printf("Sum: %d\n", result);

    return 0;
}

// Function definition
int add(int a, int b) {
    return a + b;
}

In this example, the function add takes two parameters (a and b) and returns their sum.

Function Call

To use a function, you need to call it within the program. The syntax for calling a function is:

returnType result = functionName(arguments);

Return Statement

The return statement is used to send the result back to the calling code. It also terminates the execution of the function.

return expression;

Function Prototypes

Function prototypes provide a declaration of the function before its actual definition. They are usually placed at the beginning of the program or in a header file.

returnType functionName(parameters);

Function Parameters

Parameters are the input values that a function accepts. They are declared in the function declaration and used in the function body.

int multiply(int x, int y) {
    return x * y;
}

Void Functions

A function with a void return type does not return any value. It is typically used for tasks that don't produce a result.

void greet() {
    printf("Hello!\n");
}

Scope of Variables

Variables declared within a function have local scope, meaning they are accessible only within that function.

int globalVariable = 10; // Global variable

void exampleFunction() {
    int localVariable = 5; // Local variable
    printf("Global Variable: %d\n", globalVariable);
    printf("Local Variable: %d\n", localVariable);
}

Understanding the basics of functions is a foundational step in becoming proficient in C programming. As you gain more experience, you'll discover advanced concepts like function pointers, recursion, and variadic functions. If you have specific questions or if there are additional topics you'd like to explore, feel free to ask!

PreviousFunctionsNextDefining Functions

Was this helpful?