Movatterモバイル変換


[0]ホーム

URL:


C Function Arguments

C Programming Tutorials

Overview of C LanguageC Language FundamentalsData Input and OutputDecision Control StatementsLoop Control StatementsFunctionsPreprocessors and Header FilesArrays and StringsPointersStructure and UnionFile Handling

When calling a function in C, arguments can be passed in two ways, by calling by value and by calling by reference. This tutorial guides you about these two ways to pass parameters to a function and demonstrates them with examples.

TypeDescription
Call by Value
  • The actual parameter is passed to a function.
  • A new memory area created for the given parameters can be used only within the function.
  • The actual parameters cannot be modified here.
Call by Reference
  • Instead of copying a variable, a memory address is passed to function as a parameter.
  • Address operator(&) is used in the parameter of the called function.
  • Changes in function reflect the change of the original variables.

Call by Value

Example:

#include<stdio.h>//function declarationint addition(int num1, int num2);int main(){    //local variable definition    int answer;    int num1 = 10;    int num2 = 5;        //calling a function to get addition value    answer = addition(num1,num2);    printf("The addition of two numbers is: %d\n",answer);    return 0;}//function returning the addition of two numbersint addition(int a,int b){    return a + b;}

Program Output:

The addition of two numbers is: 15

Call by Reference

Example:

#include<stdio.h>//function declarationint addition(int *num1, int *num2);int main(){    //local variable definition    int answer;    int num1 = 10;    int num2 = 5;        //calling a function to get addition value    answer = addition(&num1,&num2);    printf("The addition of two numbers is: %d\n",answer);    return 0;}//function returning the addition of two numbersint addition(int *a,int *b){    return *a + *b;}

Program Output:

The addition of two numbers is: 15


Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram

[8]ページ先頭

©2009-2025 Movatter.jp