Deprecated: Optional parameter $i declared before required parameter $data is implicitly treated as a required parameter in /home4/aipsa9vw/public_html/blogs/wp-content/themes/voice/include/modules.php on line 4

Deprecated: Optional parameter $i declared before required parameter $data is implicitly treated as a required parameter in /home4/aipsa9vw/public_html/blogs/wp-content/themes/voice/include/modules.php on line 245

Deprecated: Optional parameter $output declared before required parameter $attr is implicitly treated as a required parameter in /home4/aipsa9vw/public_html/blogs/wp-content/themes/voice/include/snippets.php on line 431

Deprecated: Using ${var} in strings is deprecated, use {$var} instead in /home4/aipsa9vw/public_html/blogs/wp-content/themes/voice/include/mega-menu.php on line 147

Warning: Cannot modify header information - headers already sent by (output started at /home4/aipsa9vw/public_html/blogs/wp-content/themes/voice/include/modules.php:4) in /home4/aipsa9vw/public_html/blogs/wp-includes/rest-api/class-wp-rest-server.php on line 1902

Warning: Cannot modify header information - headers already sent by (output started at /home4/aipsa9vw/public_html/blogs/wp-content/themes/voice/include/modules.php:4) in /home4/aipsa9vw/public_html/blogs/wp-includes/rest-api/class-wp-rest-server.php on line 1902

Warning: Cannot modify header information - headers already sent by (output started at /home4/aipsa9vw/public_html/blogs/wp-content/themes/voice/include/modules.php:4) in /home4/aipsa9vw/public_html/blogs/wp-includes/rest-api/class-wp-rest-server.php on line 1902

Warning: Cannot modify header information - headers already sent by (output started at /home4/aipsa9vw/public_html/blogs/wp-content/themes/voice/include/modules.php:4) in /home4/aipsa9vw/public_html/blogs/wp-includes/rest-api/class-wp-rest-server.php on line 1902

Warning: Cannot modify header information - headers already sent by (output started at /home4/aipsa9vw/public_html/blogs/wp-content/themes/voice/include/modules.php:4) in /home4/aipsa9vw/public_html/blogs/wp-includes/rest-api/class-wp-rest-server.php on line 1902

Warning: Cannot modify header information - headers already sent by (output started at /home4/aipsa9vw/public_html/blogs/wp-content/themes/voice/include/modules.php:4) in /home4/aipsa9vw/public_html/blogs/wp-includes/rest-api/class-wp-rest-server.php on line 1902

Warning: Cannot modify header information - headers already sent by (output started at /home4/aipsa9vw/public_html/blogs/wp-content/themes/voice/include/modules.php:4) in /home4/aipsa9vw/public_html/blogs/wp-includes/rest-api/class-wp-rest-server.php on line 1902

Warning: Cannot modify header information - headers already sent by (output started at /home4/aipsa9vw/public_html/blogs/wp-content/themes/voice/include/modules.php:4) in /home4/aipsa9vw/public_html/blogs/wp-includes/rest-api/class-wp-rest-server.php on line 1902
{"id":350,"date":"2023-01-19T14:08:40","date_gmt":"2023-01-19T14:08:40","guid":{"rendered":"https:\/\/aipsacademy.com\/blogs\/?p=350"},"modified":"2023-01-19T14:08:42","modified_gmt":"2023-01-19T14:08:42","slug":"passing-arguments-in-functions","status":"publish","type":"post","link":"https:\/\/aipsacademy.com\/blogs\/2023\/01\/19\/passing-arguments-in-functions\/","title":{"rendered":"Passing Arguments in Functions"},"content":{"rendered":"\n

Passing arguments<\/a> in functions means value given to function<\/a> on which operation is to be performed.<\/p>\n\n\n\n

There are three ways of passing arguments in functions.<\/p>\n\n\n\n

\n
  1. Call by value \/ Pass by Value<\/li>
  2. Call by Reference \/ Pass by Reference<\/li>
  3. Call by Address \/ Pass by Address<\/li><\/ol>\n<\/div><\/div>\n\n\n\n

    Call by value \/ Pass by value<\/h3>\n\n\n\n

    In this type value is passed in function’s argument and operation is done on formal argument.<\/p>\n\n\n\n

    Any change made in formal argument<\/a> does not effect the actual argument.<\/a><\/p>\n\n\n\n

    Q. Write a program to represent call by value for swapping of two variables.<\/a><\/p><\/blockquote>\n\n\n\n

    #include<iostream.h>\n#include<conio.h>\nvoid swap(int,int);\nvoid main()  \n{\nclrscr();\nint a,b;\na=10;\nb=20;\ncout<<\u201dBefore swapping a=\u201d<<a<<\u201d\\tb=\u201d<<b;\nswap(a,b);\ncout<<\u201d\\nAfter swapping in main a=\u201d<<a<<\u201d\\tb=\u201d<<b;\ngetch();\n}\nvoid swap(int x,int y)\n{\nint z;\nz=x;\nx=y;\ny=z;\ncout<<\u201d\\n After swapping in function a=\u201d<<x<<\u201d\\tb=\u201d<<y;\n}\n<\/code><\/pre>\n\n\n\n

    Output<\/strong><\/p>\n\n\n\n

    \n
    \n

    Before swapping a=10 b=20<\/p>\n\n\n\n

    After swapping in function a= 20 b=10<\/p>\n\n\n\n

    After swapping in main a=10 b=20<\/p>\n<\/div><\/div>\n<\/div><\/div>\n\n\n\n

    OR (Run time)<\/p><\/blockquote>\n\n\n\n

    #include<iostream.h>\n#include<conio.h>\nvoid swap(int,int);\nvoid main()  \n{\nclrscr();\nint a,b;\ncout<<\u201dEnter value of a and b=\u201d;\ncin>>a>>b;\ncout<<\u201dBefore swapping a=\u201d<<a<<\u201d\\tb=\u201d<<b;\nswap(a,b);\ncout<<\u201d\\nAfter swapping in main a=\u201d<<a<<\u201d\\tb=\u201d<<b;\ngetch();\n}\nvoid swap(int x,int y)\n{\nint z;\nz=x;\nx=y;\ny=z;\ncout<<\u201d\\n After swapping in function a=\u201d<<x<<\u201d\\tb=\u201d<<y;\n}\n<\/code><\/pre>\n\n\n\n

    Output<\/strong><\/p>\n\n\n\n

    \n
    \n
    \n

    Enter value of a and b= 10<\/p>\n\n\n\n

    20<\/p>\n\n\n\n

    Before swapping a=10 b=20<\/p>\n\n\n\n

    After swapping in function a= 20 b=10<\/p>\n\n\n\n

    After swapping in main a=10 b=20<\/p>\n<\/div><\/div>\n<\/div><\/div>\n<\/div><\/div>\n\n\n\n

    Call by Reference \/ Pass by Reference<\/h3>\n\n\n\n

    In this type memory address is passed in function’s Argument Implicitly.<\/a><\/p>\n\n\n\n

    Any change made in formal argument is permanent.<\/p>\n\n\n\n

    Q. Write a program to represent call by Reference for swapping of two variables.<\/p><\/blockquote>\n\n\n\n

    #include<iostream.h>\n#include<conio.h>\nvoid swap(int &,int &);\nvoid main()  \n{\nclrscr();\nint a,b;\na=10;\nb=20;\ncout<<\u201dBefore swapping a=\u201d<<a<<\u201d\\tb=\u201d<<b;\nswap(a,b);\ncout<<\u201d\\nAfter swapping in main a=\u201d<<a<<\u201d\\tb=\u201d<<b;\ngetch();\n}\nvoid swap(int &x,int &y)\n{\nint z;\nz=x;\nx=y;\ny=z;\ncout<<\u201d\\n After swapping in function a=\u201d<<x<<\u201d\\tb=\u201d<<y;\n}\n<\/code><\/pre>\n\n\n\n

    Output<\/strong><\/p>\n\n\n\n

    \n

    Before swapping a=10 b=20<\/p>\n\n\n\n

    After swapping in function a= 20 b=10<\/p>\n\n\n\n

    After swapping in main a=20 b=10<\/p>\n<\/div><\/div>\n\n\n\n

    OR (Run time)<\/p><\/blockquote>\n\n\n\n

    #include<iostream.h>\n#include<conio.h>\nvoid swap(int &,int &);\nvoid main()  \n{\nclrscr();\nint a,b;\ncout<<\u201dEnter value of a and b=\u201d;\ncin>>a>>b;\ncout<<\u201dBefore swapping a=\u201d<<a<<\u201d\\tb=\u201d<<b;\nswap(a,b);\ncout<<\u201d\\nAfter swapping in main a=\u201d<<a<<\u201d\\tb=\u201d<<b;\ngetch();\n}\nvoid swap(int &x,int &y)\n{\nint z;\nz=x;\nx=y;\ny=z;\ncout<<\u201d\\n After swapping in function a=\u201d<<x<<\u201d\\tb=\u201d<<y;\n}\n<\/code><\/pre>\n\n\n\n

    Output<\/strong><\/p>\n\n\n\n

    \n
    \n

    Enter value of a and b= 10<\/p>\n\n\n\n

    20<\/p>\n\n\n\n

    Before swapping a=10 b=20<\/p>\n\n\n\n

    After swapping in function a= 20 b=10<\/p>\n\n\n\n

    After swapping in main a=20 b=10<\/p>\n<\/div><\/div>\n<\/div><\/div>\n\n\n\n

    Call by Address \/ Pass by Address<\/h3>\n\n\n\n

    In this type memory address is passed in function’s Argument Explicitly.<\/a><\/p>\n\n\n\n

    Any change made in formal argument is permanent.<\/p>\n\n\n\n

    Q. Write a program to represent call by Address for swapping of two variables.<\/p><\/blockquote>\n\n\n\n

    #include<iostream.h>\n#include<conio.h>\nvoid swap(int *, int *);\nvoid main()  \n{\nclrscr();\nint a,b;\na=10;\nb=20;\ncout<<\u201dBefore swapping a=\u201d<<a<<\u201d\\tb=\u201d<<b;\nswap(&a , &b);\ncout<<\u201d\\nAfter swapping in main a=\u201d<<a<<\u201d\\tb=\u201d<<b;\ngetch();\n}\nvoid swap(int *x , int *y)\n{\nint *z;\n*z=*x;\n*x=*y;\n*y=*z;\ncout<<\u201d\\n After swapping in function a=\u201d<<*x<<\u201d\\tb=\u201d<<*y;\n}\n<\/code><\/pre>\n\n\n\n

    Output<\/strong><\/p>\n\n\n\n

    Before swapping a=10 b=20<\/p>\n\n\n\n

    After swapping in function a= 20 b=10<\/p>\n\n\n\n

    After swapping in main a=20 b=10<\/p>\n\n\n\n

    OR (run time)<\/p><\/blockquote>\n\n\n\n

    #include<iostream.h>\n#include<conio.h>\nvoid swap(int *, int *);\nvoid main()  \n{\nclrscr();\nint a,b;\ncout<<\u201dEnter value of a and b=\u201d;\ncin>>a>>b;\ncout<<\u201dBefore swapping a=\u201d<<a<<\u201d\\tb=\u201d<<b;\nswap(&a , &b);\ncout<<\u201d\\nAfter swapping in main a=\u201d<<a<<\u201d\\tb=\u201d<<b;\ngetch();\n}\nvoid swap(int *x , int *y)\n{\nint *z;\n*z=*x;\n*x=*y;\n*y=*z;\ncout<<\u201d\\n After swapping in function a=\u201d<<*x<<\u201d\\tb=\u201d<<*y;\n}\n<\/code><\/pre>\n\n\n\n

    Output<\/strong><\/p>\n\n\n\n

    \n
    \n

    Enter value of a and b=10<\/p>\n\n\n\n

    20<\/p>\n\n\n\n

    Before swapping a=10 b=20<\/p>\n\n\n\n

    After swapping in function a= 20 b=10<\/p>\n\n\n\n

    After swapping in main a=20 b=10<\/p>\n<\/div><\/div>\n<\/div><\/div>\n\n\n\n

    <\/p>\n\n\n\n